From 8955bf1aa886c1299158f58b78daabf664cd5ac0 Mon Sep 17 00:00:00 2001 From: toy0116 Date: Sat, 2 May 2026 10:14:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(embedding):=20OpenAIEmbedder.doRequestWithR?= =?UTF-8?q?etry=20shadows=20err=20=C2=B7=20returns=20(nil,=20nil)=20on=20c?= =?UTF-8?q?onnection=20failure=20=C2=B7=20caller=20nil-derefs=20and=20SIGS?= =?UTF-8?q?EGVs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reproduce: 1. Run WeKnora pointing OPENAI_BASE_URL at any OpenAI-compatible embedding endpoint. 2. Stop that endpoint. 3. Issue any RAG-mode chat / knowledge_search query — the pipeline reaches chunk_search_parallel → knowledgeBaseService.GetQueryEmbedding → OpenAIEmbedder.Embed → OpenAIEmbedder.BatchEmbed. 4. Observe the entire WeKnora process SIGSEGV instead of returning a graceful 5xx. Stack (real, observed in production): panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x40] internal/models/embedding/openai.go:195 +0x458 BatchEmbed internal/models/embedding/openai.go:93 +0xb0 Embed internal/application/service/knowledgebase_search.go:35 GetQueryEmbedding internal/application/service/chat_pipeline/search.go:371 searchByTargets.func1 Root cause — variable shadowing in doRequestWithRetry: func (e *OpenAIEmbedder) doRequestWithRetry(...) (*http.Response, error) { var resp *http.Response var err error // outer err ... for i := 0; i <= e.maxRetries; i++ { ... req, err := http.NewRequestWithContext(...) // ← `:=` shadows // outer `err` // because `req` // is new in // the loop scope if err != nil { ... continue } ... resp, err = e.httpClient.Do(req) // writes to the // shadowed // loop-local err if err == nil { return resp, nil } logger.Errorf("attempt %d failed: %v", i+1, err) // logs the inner err } return nil, err // returns OUTER // err — never set } When httpClient.Do returns connection errors on every attempt, the loop logs them faithfully but the outer err stays nil. The function returns (nil, nil). Caller in BatchEmbed reads: resp, err := e.doRequestWithRetry(ctx, jsonData) if err != nil { return nil, ... } // skipped (err is nil) if resp.Body != nil { // ← line 195 · resp itself is nil defer resp.Body.Close() // nil pointer dereference } // → SIGSEGV → process dies Fix — declare `req` separately so the shared `err` variable is not shadowed: var req *http.Request req, err = http.NewRequestWithContext(...) // outer err After the fix, when all retries fail, doRequestWithRetry returns the real error (e.g. `dial tcp 127.0.0.1:11434: connect: connection refused`), the caller propagates it as a regular error, the chat pipeline returns a 5xx to the client, and the process keeps running. Verified by stopping the embedding upstream while WeKnora was serving queries · process stayed up · returned graceful errors · resumed normal operation when the upstream came back. Without the patch the same sequence killed the process within one query. --- internal/models/embedding/openai.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/models/embedding/openai.go b/internal/models/embedding/openai.go index 0c70bfd8..901a0a55 100644 --- a/internal/models/embedding/openai.go +++ b/internal/models/embedding/openai.go @@ -122,8 +122,20 @@ func (e *OpenAIEmbedder) doRequestWithRetry(ctx context.Context, jsonData []byte } } - // Rebuild request each time to ensure Body is valid - req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData)) + // Rebuild request each time to ensure Body is valid. + // IMPORTANT: declare `req` separately (var) so the assignment to `err` + // below uses the outer-scope variable, not a fresh loop-local one. + // Previously this read `req, err := http.NewRequestWithContext(...)`, + // where `:=` introduced a new `err` shadowing the outer one. The + // `resp, err = httpClient.Do(req)` line then wrote to the shadowed + // `err` only, so when all retries failed with connection errors the + // outer `err` stayed nil. The function returned `(nil, nil)`, and + // callers (BatchEmbed line 195) blindly dereferenced `resp.Body` → + // SIGSEGV nil-pointer panic that took down the whole process. + // Reproduce: stop the embedding upstream (e.g. localhost:3130), make + // any RAG query → backend SIGSEGV instead of returning HTTP 500. + var req *http.Request + req, err = http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData)) if err != nil { logger.GetLogger(ctx).Errorf("OpenAIEmbedder failed to create request: %v", err) continue