dify icon indicating copy to clipboard operation
dify copied to clipboard

[Bug] KB import hangs forever at token counting (get_text_embedding_num_tokens) after upgrade to 1.20.0 (Weaviate + openai_api_compatible)

Open fangxicc opened this issue 2 weeks ago • 5 comments

Self Checks

  • [x] I have read the Contributing Guide
  • [x] This is a bug report
  • [x] I have searched existing issues (including closed)

Dify version

1.20.0 (self-hosted, Docker Compose), upgraded from a working 1.17.1.

Cloud or Self Hosted

Self Hosted (Docker Compose)

Deployment

  • Vector store: Weaviate 1.39.13 (VECTOR_STORE=weaviate)
  • plugin_daemon: official 0.6.15-local
  • api / worker / web: 1.20.0
  • Embedding provider: langgenius/openai_api_compatible/openai_api_compatible plugin (0.0.73), model bge-m3, served by a custom self-hosted OpenAI-compatible gateway on the internal network.

Steps to reproduce

  1. Upgrade from a working 1.17.1 deployment to 1.20.0 (git checkout 1.20.0, matching 1.20.0 images + plugin-daemon 0.6.15-local; DB migration runs clean; all 15 services healthy).
  2. Create/use a high-quality knowledge base, upload ANY file — even a small/empty .txt.
  3. The document stays at "嵌入处理中" (embedding processing) forever. The embedding gateway receives no request during import (confirmed in the new-api logs UI, and by direct probing).

Note: before the upgrade (1.17.1) the identical files import fine on the same server, and after the upgrade retrieval (召回) DOES reach the embedding gateway — so config / network / credentials are all healthy. This failure is import-specific.

Expected Behavior

Document finishes indexing — tokens counted, segments embedded, status completed.

Actual Behavior

  • Document hangs in splitting, word_count / tokens remain NULL forever.
  • Worker thread is permanently blocked (py-spy, with -P threads):
SyncStream.read (httpcore/_backends/sync.py:128)
HTTP11Connection._receive_event (httpcore/_sync/http11.py:217)
HTTPSConnection.handle_request ...
ConnectionPool.handle_request (httpcore/_sync/connection_pool.py:236)
HTTPTransport.handle_request (httpx/_transports/default.py:250)
Client._send_single_request / _send_handling_redirects / _send_handling_auth / send / stream
BasePluginClient._request_with_plugin_daemon_response_stream (base.py:371)
PluginModelClient.get_text_embedding_num_tokens (model.py:475)
PluginModelRuntime.get_text_embedding_num_tokens (model_runtime.py:591)
TextEmbeddingModel.get_num_tokens (graphon/model_runtime/model_providers/base/text_embedding_model.py:71)
ModelInstance.get_text_embedding_num_tokens (core/model_manager.py:376)
calculate_segment_token_counts (core/rag/embedding/token_counter.py:25)
IndexingExecutionAdapter.count_tokens (services/knowledge/indexing/adapters/execution.py:157)
DocumentIndexingService._run (services/knowledge/indexing/execution.py:132)
priority_document_indexing_task
  • plugin_daemon /health/check shows active_dispatch_requests climbing/stuck (observed 4) while all ~23 plugin subprocesses sit in ep_poll idle.
  • plugin_daemon log intermittently logs: no available node, plugin runtime not found (404) against /dispatch/text_embedding/num_tokens.

What we ruled out (measured, not guessed)

  • Gateway / embedding backend down: direct worker-to-gateway POST returns in ~3ms (unauthenticated 401, proves connectivity).
  • Worker <-> plugin_daemon connectivity: sockets ESTABLISHED; daemon root and /health/check answer in ~3ms.
  • Auth key mismatch: PLUGIN_DAEMON_KEY, PLUGIN_DIFY_INNER_API_KEY, DIFY_INNER_API_KEY all match and are injected into the worker container.
  • Celery pool type: reproduced identically with -P threads and -P gevent.
  • Plugin version leftovers: the daemon's active registry contained BOTH openai_api_compatible 0.0.68 and 0.0.73; after removing 0.0.68 the 0.0.73 runtime comes up ready, but indexing still hangs. (Many stale versions remain under cwd/langgenius/, we removed only 0.0.68.)
  • Full docker compose down && docker compose up -d of the entire stack: no change.

Related issues

  • #45365 — indexing hangs; with Milvus, gRPC calls block inside gevent's pool under the prefork Celery worker. Same "worker stops producing logs, task stalls forever" shape, but our case is Weaviate + the openai_api_compatible plugin, not Milvus.
  • #39560 / #39571 — unbatched token-counting during indexing. Our trace is exactly the token-counting call (get_text_embedding_num_tokens) blocking, but it blocks before the request reaches the daemon/gateway, not an HTTP 413.

Happy to provide more logs or a tcpdump if useful.

fangxicc avatar Nov 20 '26 07:11 fangxicc

Hi, I would like to take this on. From the report, the hang is specific to import: get_text_embedding_num_tokens blocks in the worker before any request reaches the embedding gateway, while retrieval works, so I would first reproduce the token counting path and trace where the dispatch to /dispatch/text_embedding/num_tokens stalls, including the plugin daemon node lookup that returns 404. My scoped fix would be in that dispatch and token counting path, with a timeout or fallback so an unavailable node cannot block indexing forever, plus a regression test for the token counting call. Please let me know if this direction looks right or if you would prefer a different approach.

techaurah avatar Nov 21 '26 06:11 techaurah

@fangxicc I reproduced the delay at get_text_embedding_num_tokens after upgrading a local deployment from 1.17.1 to 1.20.0. I found that the daemon image already contains the GPT-2 vocabulary, but the local plugin process loses TIKTOKEN_CACHE_DIR and downloads it again on the first import. This happens before any embedding request reaches the model gateway.

I opened dify-plugin-daemon#861 to pass the cache directory to the plugin. With Weaviate 1.39.13 and openai_api_compatible 0.0.73, the first token-count request for the same text took 15.982 s on stock 0.6.15-local and 171 ms with the patch. I used a different local embedding model. Both imports eventually completed, and I did not reproduce the intermittent 404.

If you get a chance to try the patch, does a fresh plugin container now finish the first import on your deployment? That would help establish whether the cache issue also explains the sustained hang you saw.

juemimgcd avatar Nov 21 '26 07:11 juemimgcd

Thanks @juemimgcd for reproducing the hang and finding the root cause: the local plugin process losing TIKTOKEN_CACHE_DIR and re-downloading the GPT-2 vocabulary on first import. The fix path now lives in dify-plugin-daemon PR #861, not in dify itself, so I will not duplicate work here. The remaining open question is fangxicc confirming whether a fresh plugin container finishes the first import, plus the intermittent 404, which was not reproduced.

techaurah avatar Nov 22 '26 06:11 techaurah

Thanks @juemimgcd and @techaurah — I verified the root cause on my deployment and it confirms the token-counting / tiktoken cache path, but with a nuance about the 404.

What reproduced (weaviate 1.39.13 + openai_api_compatible 0.0.73 + Dify 1.20.0, Docker Compose)

  • The import hangs in splitting forever, word_count/tokens NULL, and the embedding gateway receives no request.
  • plugin_daemon log when it finally gives up:
PluginDaemonInternalServerError error="killed by timeout"
GetTextEmbeddingNumTokens
POST /dispatch/text_embedding/num_tokens status=200 latency_ms=600005

So the num_tokens request is dispatched but the plugin subprocess doesn't respond until the daemon's 600 s hard timeout kills it — that looks like exactly the GPT-2 vocab / tiktoken stall on first import that you diagnosed. In our internal network the blob download cannot complete, so it hangs until the timeout.

The 404

The intermittent no available node, plugin runtime not found (404) on /dispatch/model/schema is separately observed, but it is not the dominant/always-repeated failure — the majority of calls (schema / num_tokens / invoke / rerank) return 200; the 404 appears rarely. So I'd treat it as a secondary symptom, not the main blocker.

Workaround that got imports completing immediately

The daemon image already ships the GPT-2 vocab files in /app/.tiktoken (hashes 6c7ea1a7e38e3a7f062df639a5b80947f075ffe6 = encoder.json, 6d1cbeee0f20b3d9449abfede4726ed8212e3aee = vocab.bpe). Because the plugin subprocess has no TIKTOKEN_CACHE_DIR, tiktoken looks in the default /tmp/data-gym-cache, keyed by the SHA1 of the download URL. Copying those two files to /tmp/data-gym-cache/ and restarting plugin_daemon made the first num_tokens drop from timeout (600 000 ms) to 368 ms and the document went to completed.

This is a container-/tmp workaround (lost on container recreate), so the upstream fix in dify-plugin-daemon#861 (pass TIKTOKEN_CACHE_DIR to plugin subprocesses) is the right long-term fix. Thanks for picking this up.

fangxicc avatar Nov 23 '26 06:11 fangxicc

Thanks for confirming this on your own deployment. Your report pins the root cause down: the first-import GPT-2 vocab download stalls on your internal network until the 600s timeout, because the plugin subprocess has no TIKTOKEN_CACHE_DIR to use. Your workaround is also useful evidence: copying the shipped vocab files into the default tiktoken cache brings the first num_tokens call from the timeout to 368ms, and I note your nuance that the intermittent 404 is not the dominant failure in your setup, with the majority of calls returning 200. The fix path stays dify-plugin-daemon PR 861, which is still open and unreviewed, and I am not planning duplicate work in dify itself.

techaurah avatar Nov 23 '26 06:11 techaurah