Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(docparser): 云解析超时改为跟随既有配置,不再硬编码 (#3891)

linus-liu-web1826f46b1217
diff --git a/.env.example b/.env.example
index 13e9d634f..5381246bd 100644
--- a/.env.example
+++ b/.env.example
@@ -518,6 +518,10 @@ OLLAMA_BASE_URL=http://host.docker.internal:11434
# WEKNORA_MINERU_TIMEOUT=1000s
# MinerU 云端(mineru.net)轮询解析结果的最长时间(正数 Go duration,默认 600s)。
# WEKNORA_MINERU_CLOUD_TIMEOUT=600s
+# PaddleOCR-VL 云端(AI Studio)轮询解析结果的最长时间(正数 Go duration,默认 600s)。
+# WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT=600s
+# WeKnoraCloud 托管 docreader 轮询任务结果的最长时间(正数 Go duration,默认 20m;应小于 WEKNORA_DOCREADER_CALL_TIMEOUT)。
+# WEKNORA_WEKNORACLOUD_TIMEOUT=20m
# 知识库巡检回收(回收卡在 processing 的脏数据;false 禁用,不建议)。
# WEKNORA_HOUSEKEEPING_ENABLED=true
# 聊天附件解析产物保留时长(小时,默认 24)。
diff --git a/docker-compose.yml b/docker-compose.yml
index 3d7b50fad..68e3ba00d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -336,6 +336,10 @@ services:
- WEKNORA_MINERU_TIMEOUT=${WEKNORA_MINERU_TIMEOUT:-}
# MinerU 云端轮询解析结果的最长时间(Go duration,默认 600s)
- WEKNORA_MINERU_CLOUD_TIMEOUT=${WEKNORA_MINERU_CLOUD_TIMEOUT:-}
+ # PaddleOCR-VL 云端轮询解析结果的最长时间(Go duration,默认 600s)
+ - WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT=${WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT:-}
+ # WeKnoraCloud 托管 docreader 轮询任务结果的最长时间(Go duration,默认 20m,应小于 DocReader RPC 超时)
+ - WEKNORA_WEKNORACLOUD_TIMEOUT=${WEKNORA_WEKNORACLOUD_TIMEOUT:-}
# 知识库巡检回收(回收卡在 processing 的脏数据;false 禁用,不建议)
- WEKNORA_HOUSEKEEPING_ENABLED=${WEKNORA_HOUSEKEEPING_ENABLED:-}
- WEKNORA_CHAT_ATTACHMENT_TTL_HOURS=${WEKNORA_CHAT_ATTACHMENT_TTL_HOURS:-24}
diff --git a/internal/infrastructure/docparser/cloud_poll_timeout_test.go b/internal/infrastructure/docparser/cloud_poll_timeout_test.go
new file mode 100644
index 000000000..430b7b754
--- /dev/null
+++ b/internal/infrastructure/docparser/cloud_poll_timeout_test.go
@@ -0,0 +1,162 @@
+package docparser
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestPaddleOCRVLCloudTimeoutConfig(t *testing.T) {
+ for _, tt := range []struct {
+ name, value string
+ want time.Duration
+ }{
+ {"empty", "", paddleOCRVLCloudTimeout},
+ {"whitespace", " ", paddleOCRVLCloudTimeout},
+ {"minutes", "90m", 90 * time.Minute},
+ {"trimmed", " 5400s ", 90 * time.Minute},
+ {"invalid", "invalid", paddleOCRVLCloudTimeout},
+ {"unitless", "5400", paddleOCRVLCloudTimeout},
+ {"zero", "0s", paddleOCRVLCloudTimeout},
+ {"negative", "-1s", paddleOCRVLCloudTimeout},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Setenv("WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT", tt.value)
+ assert.Equal(t, tt.want, NewPaddleOCRVLCloudReader(nil).timeout)
+ })
+ }
+}
+
+// The configured timeout, not the old hardcoded 600s, must end a job that the
+// cloud keeps reporting as running.
+func TestPaddleOCRVLCloudPollStopsAtConfiguredTimeout(t *testing.T) {
+ allowLoopbackSSRF(t)
+ t.Setenv("WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT", "200ms")
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = io.WriteString(w, `{"code":0,"data":{"state":"running"}}`)
+ }))
+ defer server.Close()
+
+ reader := NewPaddleOCRVLCloudReader(nil)
+ reader.baseURL = server.URL
+ reader.pollInterval = 10 * time.Millisecond
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ // Watchdog: without the configured budget this loop keeps polling for the
+ // old hardcoded 600s, which must fail the test instead of hanging it.
+ done := make(chan error, 1)
+ start := time.Now()
+ go func() {
+ _, pollErr := reader.pollJob(ctx, "job-1")
+ done <- pollErr
+ }()
+
+ select {
+ case err := <-done:
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), fmt.Sprintf("timed out after %s", 200*time.Millisecond))
+ assert.Less(t, time.Since(start), 5*time.Second)
+ case <-time.After(5 * time.Second):
+ t.Fatalf("poll loop ignored WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT")
+ }
+}
+
+func TestWeKnoraCloudPollTimeoutConfig(t *testing.T) {
+ for _, tt := range []struct {
+ name, value string
+ want time.Duration
+ }{
+ {"empty", "", defaultWeKnoraCloudPollTimeout},
+ {"minutes", "90m", 90 * time.Minute},
+ {"trimmed", " 3600s ", time.Hour},
+ {"invalid", "invalid", defaultWeKnoraCloudPollTimeout},
+ {"zero", "0s", defaultWeKnoraCloudPollTimeout},
+ {"negative", "-1s", defaultWeKnoraCloudPollTimeout},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Setenv("WEKNORA_WEKNORACLOUD_TIMEOUT", tt.value)
+ reader, err := NewWeKnoraCloudSignedDocumentReader("app-id", "api-key")
+ require.NoError(t, err)
+ assert.Equal(t, tt.want, reader.pollTimeout)
+ })
+ }
+}
+
+// weKnoraCloudPollTransport answers every poll with "still running" so the
+// configured poll budget is the only thing that can end the loop.
+type weKnoraCloudPollTransport struct {
+ polls atomic.Int64
+}
+
+func (t *weKnoraCloudPollTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ t.polls.Add(1)
+ body := `{"task_id":"task-1","status":"processing","progress":0.1}`
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"application/json"}},
+ Body: io.NopCloser(strings.NewReader(body)),
+ Request: req,
+ }, nil
+}
+
+// The configured budget, not the old hardcoded 20m, must end the poll loop
+// whether or not the caller has a deadline of its own. Ingestion always calls
+// with a (much longer) DocReader deadline, so that case must not bypass it.
+func TestWeKnoraCloudPollStopsAtConfiguredTimeout(t *testing.T) {
+ for _, tt := range []struct {
+ name string
+ parentCtx func() (context.Context, context.CancelFunc)
+ }{
+ {"no parent deadline", func() (context.Context, context.CancelFunc) {
+ return context.WithCancel(context.Background())
+ }},
+ {"longer parent deadline", func() (context.Context, context.CancelFunc) {
+ return context.WithTimeout(context.Background(), time.Minute)
+ }},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Setenv("WEKNORA_WEKNORACLOUD_TIMEOUT", "300ms")
+ reader, err := NewWeKnoraCloudSignedDocumentReader("app-id", "api-key")
+ require.NoError(t, err)
+
+ transport := &weKnoraCloudPollTransport{}
+ reader.client = &http.Client{Transport: transport}
+ reader.initialPollInterval = 5 * time.Millisecond
+ reader.maxPollInterval = 10 * time.Millisecond
+
+ ctx, cancel := tt.parentCtx()
+ defer cancel()
+
+ done := make(chan error, 1)
+ start := time.Now()
+ go func() {
+ _, pollErr := reader.pollTaskResult(ctx, "task-1")
+ done <- pollErr
+ }()
+
+ select {
+ case pollErr := <-done:
+ require.Error(t, pollErr)
+ assert.True(t, errors.Is(pollErr, context.DeadlineExceeded), "err = %v", pollErr)
+ assert.Less(t, time.Since(start), 5*time.Second)
+ assert.Greater(t, transport.polls.Load(), int64(1),
+ "the job must actually be polled before the budget ends it")
+ case <-time.After(5 * time.Second):
+ t.Fatalf("poll loop ignored WEKNORA_WEKNORACLOUD_TIMEOUT after %d polls", transport.polls.Load())
+ }
+ })
+ }
+}
diff --git a/internal/infrastructure/docparser/paddleocr_vl_cloud_converter.go b/internal/infrastructure/docparser/paddleocr_vl_cloud_converter.go
index 3546a7e96..e7bf0aa44 100644
--- a/internal/infrastructure/docparser/paddleocr_vl_cloud_converter.go
+++ b/internal/infrastructure/docparser/paddleocr_vl_cloud_converter.go
@@ -23,28 +23,37 @@ const (
paddleOCRVLCloudDefaultBaseURL = "https://paddleocr.aistudio-app.com/api/v2/ocr/jobs"
paddleOCRVLCloudDefaultModel = "PaddleOCR-VL-1.6"
paddleOCRVLCloudPollInterval = 5 * time.Second
- paddleOCRVLCloudTimeout = 600 * time.Second
+ // paddleOCRVLCloudTimeout bounds how long one job is polled;
+ // WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT overrides it for documents that take
+ // longer, mirroring WEKNORA_MINERU_CLOUD_TIMEOUT on the MinerU cloud reader.
+ paddleOCRVLCloudTimeout = 600 * time.Second
)
// PaddleOCRVLCloudReader calls the PaddleOCR-VL AI Studio cloud API.
// Flow: POST /jobs (multipart) → poll GET /jobs/{id} → download result JSONL,
// then fetch each referenced image URL.
type PaddleOCRVLCloudReader struct {
- token string
- baseURL string
- model string
- useSeal bool
- useChart bool
+ token string
+ baseURL string
+ model string
+ useSeal bool
+ useChart bool
+ timeout time.Duration
+ pollInterval time.Duration
}
// NewPaddleOCRVLCloudReader creates a reader from ParserEngineOverrides.
func NewPaddleOCRVLCloudReader(overrides map[string]string) *PaddleOCRVLCloudReader {
return &PaddleOCRVLCloudReader{
- token: strings.TrimSpace(overrides["paddleocr_vl_cloud_token"]),
- baseURL: strings.TrimRight(stringOr(overrides["paddleocr_vl_cloud_base_url"], paddleOCRVLCloudDefaultBaseURL), "/"),
- model: stringOr(overrides["paddleocr_vl_cloud_model"], paddleOCRVLCloudDefaultModel),
- useSeal: parseBoolOr(overrides["paddleocr_vl_cloud_use_seal_recognition"], true),
- useChart: parseBoolOr(overrides["paddleocr_vl_cloud_use_chart_recognition"], false),
+ token: strings.TrimSpace(overrides["paddleocr_vl_cloud_token"]),
+ baseURL: strings.TrimRight(stringOr(
+ overrides["paddleocr_vl_cloud_base_url"], paddleOCRVLCloudDefaultBaseURL,
+ ), "/"),
+ model: stringOr(overrides["paddleocr_vl_cloud_model"], paddleOCRVLCloudDefaultModel),
+ useSeal: parseBoolOr(overrides["paddleocr_vl_cloud_use_seal_recognition"], true),
+ useChart: parseBoolOr(overrides["paddleocr_vl_cloud_use_chart_recognition"], false),
+ timeout: requestTimeoutFromEnv("WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT", paddleOCRVLCloudTimeout),
+ pollInterval: paddleOCRVLCloudPollInterval,
}
}
@@ -185,7 +194,7 @@ type paddleOCRVLCloudPollResponse struct {
}
func (c *PaddleOCRVLCloudReader) pollJob(ctx context.Context, jobID string) (string, error) {
- deadline := time.Now().Add(paddleOCRVLCloudTimeout)
+ deadline := time.Now().Add(c.timeout)
pollCount := 0
url := c.baseURL + "/" + jobID
@@ -209,7 +218,7 @@ func (c *PaddleOCRVLCloudReader) pollJob(ctx context.Context, jobID string) (str
resp, err := client.Do(httpReq)
if err != nil {
logger.Errorf(context.Background(), "[PaddleOCR-VL Cloud] poll #%d failed: %v", pollCount, err)
- sleepCtx(ctx, paddleOCRVLCloudPollInterval)
+ sleepCtx(ctx, c.pollInterval)
continue
}
respBody, _ := io.ReadAll(resp.Body)
@@ -217,14 +226,14 @@ func (c *PaddleOCRVLCloudReader) pollJob(ctx context.Context, jobID string) (str
if resp.StatusCode != http.StatusOK {
logger.Errorf(context.Background(), "[PaddleOCR-VL Cloud] poll #%d status %d: %s", pollCount, resp.StatusCode, string(respBody))
- sleepCtx(ctx, paddleOCRVLCloudPollInterval)
+ sleepCtx(ctx, c.pollInterval)
continue
}
var pollResp paddleOCRVLCloudPollResponse
if err := json.Unmarshal(respBody, &pollResp); err != nil {
logger.Errorf(context.Background(), "[PaddleOCR-VL Cloud] poll #%d decode error: %v", pollCount, err)
- sleepCtx(ctx, paddleOCRVLCloudPollInterval)
+ sleepCtx(ctx, c.pollInterval)
continue
}
@@ -244,10 +253,10 @@ func (c *PaddleOCRVLCloudReader) pollJob(ctx context.Context, jobID string) (str
return "", fmt.Errorf("task failed: %s", pollResp.Data.ErrorMsg)
}
- sleepCtx(ctx, paddleOCRVLCloudPollInterval)
+ sleepCtx(ctx, c.pollInterval)
}
- return "", fmt.Errorf("task timed out after %d polls", pollCount)
+ return "", fmt.Errorf("PaddleOCR-VL Cloud task timed out after %s (%d polls)", c.timeout, pollCount)
}
// --- result parsing ---
diff --git a/internal/infrastructure/docparser/weknoracloud_http_reader.go b/internal/infrastructure/docparser/weknoracloud_http_reader.go
index f19ef7106..3dc0f7066 100644
--- a/internal/infrastructure/docparser/weknoracloud_http_reader.go
+++ b/internal/infrastructure/docparser/weknoracloud_http_reader.go
@@ -21,6 +21,11 @@ import (
const (
weKnoraCloudReaderBaseURL = "https://weknora.weixin.qq.com/api/v1/doc"
+ // defaultWeKnoraCloudPollTimeout bounds how long one async task is polled;
+ // WEKNORA_WEKNORACLOUD_TIMEOUT overrides it. It must stay below
+ // WEKNORA_DOCREADER_CALL_TIMEOUT (default 30m) or the hardcoded value, not
+ // the configured one, is what fails a slow parse.
+ defaultWeKnoraCloudPollTimeout = 20 * time.Minute
)
// WeKnoraCloudSignedDocumentReader implements the docreader HTTP protocol with WeKnoraCloud signing.
@@ -47,7 +52,7 @@ func NewWeKnoraCloudSignedDocumentReader(appID, apiKey string) (*WeKnoraCloudSig
apiKey: apiKey,
initialPollInterval: 500 * time.Millisecond,
maxPollInterval: 10 * time.Second,
- pollTimeout: 20 * time.Minute,
+ pollTimeout: requestTimeoutFromEnv("WEKNORA_WEKNORACLOUD_TIMEOUT", defaultWeKnoraCloudPollTimeout),
client: secutils.NewSSRFSafeHTTPClient(clientCfg),
}, nil
}
@@ -138,7 +143,7 @@ type weKnoraCloudAsyncTaskResponse struct {
func (p *WeKnoraCloudSignedDocumentReader) pollTaskResult(ctx context.Context, taskID string) (*types.ReadResult, error) {
pollCtx := ctx
- if _, ok := ctx.Deadline(); !ok && p.pollTimeout > 0 {
+ if p.pollTimeout > 0 {
var cancel context.CancelFunc
pollCtx, cancel = context.WithTimeout(ctx, p.pollTimeout)
defer cancel()
diff --git a/website-docs/01-getting-started/04-configuration.md b/website-docs/01-getting-started/04-configuration.md
index 80bcd3eaa..6780be473 100644
--- a/website-docs/01-getting-started/04-configuration.md
+++ b/website-docs/01-getting-started/04-configuration.md
@@ -296,6 +296,8 @@ AWS S3 的 `S3_ACCESS_KEY` / `S3_SECRET_KEY` 可以**同时留空**,此时走
| `WEKNORA_PADDLEOCR_VL_TIMEOUT` | 1000s | 自建 PaddleOCR-VL HTTP 请求超时,支持正数 Go duration(如 `5400s`、`90m`);空值、无效值或非正数使用默认值。外层超时需留余量,例如本项 `90m`、DocReader `100m`、文档任务 `2h` |
| `WEKNORA_MINERU_TIMEOUT` | 1000s | 自建 MinerU 单次解析超时(V1 API 为整个解析任务,旧版为 `/file_parse` 请求),格式与默认值规则同上;超大 PDF 同样需要外层超时留余量 |
| `WEKNORA_MINERU_CLOUD_TIMEOUT` | 600s | MinerU 云端(mineru.net)轮询解析结果的最长时间,格式与默认值规则同上 |
+| `WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT` | 600s | PaddleOCR-VL 云端(AI Studio)轮询解析结果的最长时间,格式与默认值规则同上 |
+| `WEKNORA_WEKNORACLOUD_TIMEOUT` | 20m | WeKnoraCloud 托管 docreader 轮询任务结果的最长时间,格式与默认值规则同上;应小于 `WEKNORA_DOCREADER_CALL_TIMEOUT` |
沙箱后端、网络策略、脚本开关与个人环境变量使用空间配置/API 管理,见[技能与沙箱](../03-features/22-skills-sandbox.md)。长期记忆与自动标签均默认关闭,分别使用租户 memory_config 和知识库 auto_tag_config,不用全局环境变量替代各空间配置。
diff --git a/website-docs/03-features/03-document-parsing.md b/website-docs/03-features/03-document-parsing.md
index 1ea2752d6..e59fd7817 100644
--- a/website-docs/03-features/03-document-parsing.md
+++ b/website-docs/03-features/03-document-parsing.md
@@ -394,6 +394,8 @@ gRPC 响应中不再返回 chunks(`ReadResponse` 没有 chunk 字段);`Exc
| `WEKNORA_PADDLEOCR_VL_TIMEOUT` | Go duration | `1000s` | 自建 PaddleOCR-VL 引擎的 HTTP 请求超时;空值、无效值或非正数使用默认值 |
| `WEKNORA_MINERU_TIMEOUT` | Go duration | `1000s` | 自建 MinerU 引擎的单次解析超时(V1 API 覆盖上传到下载的整个任务,旧版覆盖 `/file_parse` 请求);空值、无效值或非正数使用默认值 |
| `WEKNORA_MINERU_CLOUD_TIMEOUT` | Go duration | `600s` | MinerU 云端(mineru.net)轮询解析结果的最长时间;空值、无效值或非正数使用默认值 |
+| `WEKNORA_PADDLEOCR_VL_CLOUD_TIMEOUT` | Go duration | `600s` | PaddleOCR-VL 云端(AI Studio)轮询解析结果的最长时间;空值、无效值或非正数使用默认值 |
+| `WEKNORA_WEKNORACLOUD_TIMEOUT` | Go duration | `20m` | WeKnoraCloud 托管 docreader 轮询任务结果的最长时间,应小于 `WEKNORA_DOCREADER_CALL_TIMEOUT`;空值、无效值或非正数使用默认值 |
处理大文件时从内到外逐级留出余量,例如 PaddleOCR-VL 或 MinerU `90m`、docreader `100m`、文档处理 `2h`。