The MilvusClient client fails to initialize in the Celery worker. #21395

Closed
opened 2026-02-21 20:12:22 -05:00 by yindo · 7 comments
Owner

Originally created by @NieRonghua on GitHub (Dec 31, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.11.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

My Milvus configuration works fine in Dify version 1.8.1.
However, after upgrading to version 1.11.1, my knowledge base documents remained stuck in the "indexing" state. I added two log entries to the code and discovered the problem was in that section of the code.
https://github.com/langgenius/dify/blob/2058186f22b4e4d4e155f380c130f4e8f21622fa/api/core/rag/datasource/vdb/milvus/milvus_vector.py#L376-L384

my debug log:

logger.info(f"MilvusVector._init_client started. URI: {config.uri}, User: {config.user}, DB: {config.database}, Token: {'***' if config.token else 'None'}")
        try:
            if config.token:
                logger.info("MilvusVector._init_client: Connecting with token.")
                client = MilvusClient(uri=config.uri, token=config.token, db_name=config.database)
            else:
                logger.info("MilvusVector._init_client: Connecting with user/password.")
                client = MilvusClient(
                    uri=config.uri,
                    user=config.user or "",
                    password=config.password or "",
                    db_name=config.database,
                )
            logger.info("MilvusVector._init_client successfully created MilvusClient object. Testing connection...")

the celery worker log:

2025-12-31 20:15:16,631.631 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:418]  - MilvusVectorFactory.init_vector started for dataset e35e0df4-58c8-4d96-bc14-d77046be41e7
2025-12-31 20:15:16,632.632 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:381]  - MilvusVector._init_client started. URI: http://milvus-sample.sit.sf-express.com:30118, User: aiplat, DB: bbbb, Token: None
2025-12-31 20:15:16,632.632 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:387]  - MilvusVector._init_client: Connecting with user/password.

Once the program reaches this point, there are no more logs.
This has resulted in my knowledge base documents remaining in the "indexing" state.

Image

My Milvus configuration is correct; I can connect to Milvus from the Python shell inside the container.

Image

Some code helpers have analyzed that the problem lies with the gevent package.

✔️ Expected Behavior

The knowledge base is functioning normally.

Actual Behavior

The MilvusClient client fails to initialize in the Celery worker.

Originally created by @NieRonghua on GitHub (Dec 31, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.11.1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce My Milvus configuration works fine in Dify version 1.8.1. However, after upgrading to version 1.11.1, my knowledge base documents remained stuck in the "indexing" state. I added two log entries to the code and discovered the problem was in that section of the code. https://github.com/langgenius/dify/blob/2058186f22b4e4d4e155f380c130f4e8f21622fa/api/core/rag/datasource/vdb/milvus/milvus_vector.py#L376-L384 my debug log: ``` logger.info(f"MilvusVector._init_client started. URI: {config.uri}, User: {config.user}, DB: {config.database}, Token: {'***' if config.token else 'None'}") try: if config.token: logger.info("MilvusVector._init_client: Connecting with token.") client = MilvusClient(uri=config.uri, token=config.token, db_name=config.database) else: logger.info("MilvusVector._init_client: Connecting with user/password.") client = MilvusClient( uri=config.uri, user=config.user or "", password=config.password or "", db_name=config.database, ) logger.info("MilvusVector._init_client successfully created MilvusClient object. Testing connection...") ``` the celery worker log: ``` 2025-12-31 20:15:16,631.631 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:418] - MilvusVectorFactory.init_vector started for dataset e35e0df4-58c8-4d96-bc14-d77046be41e7 2025-12-31 20:15:16,632.632 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:381] - MilvusVector._init_client started. URI: http://milvus-sample.sit.sf-express.com:30118, User: aiplat, DB: bbbb, Token: None 2025-12-31 20:15:16,632.632 INFO [ThreadPoolExecutor-3_0] [milvus_vector.py:387] - MilvusVector._init_client: Connecting with user/password. ``` Once the program reaches this point, there are no more logs. This has resulted in my knowledge base documents remaining in the "indexing" state. <img width="898" height="148" alt="Image" src="https://github.com/user-attachments/assets/6baab460-cf4a-4538-bab9-364a57d4be87" /> My Milvus configuration is correct; I can connect to Milvus from the Python shell inside the container. <img width="2172" height="388" alt="Image" src="https://github.com/user-attachments/assets/cd32fbec-72e2-4f99-b664-75d22cbea62f" /> Some code helpers have analyzed that the problem lies with the gevent package. ### ✔️ Expected Behavior The knowledge base is functioning normally. ### ❌ Actual Behavior The MilvusClient client fails to initialize in the Celery worker.
yindo added the 🐞 bug label 2026-02-21 20:12:22 -05:00
yindo closed this issue 2026-02-21 20:12:22 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 31, 2025):

This is a known issue: MilvusClient initialization hangs in the Celery worker due to a conflict between gevent's monkey-patching and gRPC (used by pymilvus). This happens because Dify's Celery workers typically use gevent for concurrency, but gRPC (and thus pymilvus) is not compatible with gevent's monkey-patched sockets, leading to the worker stalling with no error or logs when trying to connect to Milvus. Your configuration and Milvus server are fine—this is an environment compatibility problem.

Temporary workaround:
Set the Celery worker class to prefork to avoid gevent monkey-patching. You can do this by setting the environment variable:

CELERY_WORKER_CLASS=prefork

and restarting your worker containers. This forces Celery to use separate processes instead of gevent greenlets, which avoids the monkey-patching conflict and allows gRPC to work normally in each worker process. This fix is confirmed to resolve the issue for Milvus users in Dify workers [details].

Permanent solution:
Consider migrating to AsyncMilvusClient with asyncio if your workflow supports it. Milvus documentation provides guidance for async client usage [details].

Other notes:

  • Make sure your Milvus URI uses the container name or IP (not localhost or 127.0.0.1) and that all relevant containers are on the same Docker network [details].
  • If you see recursion errors or instability after upgrading to Dify 1.11.1, check your gevent version (downgrade to 24.11.1 if using Python 3.12), and ensure no modules using SSL are imported before gevent monkey-patching [details].

Let me know if you need step-by-step instructions for changing the worker class or want to discuss async migration!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Dec 31, 2025): <!-- Answer --> This is a known issue: MilvusClient initialization hangs in the Celery worker due to a conflict between gevent's monkey-patching and gRPC (used by pymilvus). This happens because Dify's Celery workers typically use gevent for concurrency, but gRPC (and thus pymilvus) is not compatible with gevent's monkey-patched sockets, leading to the worker stalling with no error or logs when trying to connect to Milvus. Your configuration and Milvus server are fine—this is an environment compatibility problem. **Temporary workaround:** Set the Celery worker class to `prefork` to avoid gevent monkey-patching. You can do this by setting the environment variable: ``` CELERY_WORKER_CLASS=prefork ``` and restarting your worker containers. This forces Celery to use separate processes instead of gevent greenlets, which avoids the monkey-patching conflict and allows gRPC to work normally in each worker process. This fix is confirmed to resolve the issue for Milvus users in Dify workers [[details](https://github.com/langgenius/dify/issues/17641)]. **Permanent solution:** Consider migrating to `AsyncMilvusClient` with asyncio if your workflow supports it. Milvus documentation provides guidance for async client usage [[details](https://github.com/langgenius/dify/issues/17641)]. **Other notes:** - Make sure your Milvus URI uses the container name or IP (not `localhost` or `127.0.0.1`) and that all relevant containers are on the same Docker network [[details](https://github.com/langgenius/dify/issues/20119)]. - If you see recursion errors or instability after upgrading to Dify 1.11.1, check your gevent version (downgrade to 24.11.1 if using Python 3.12), and ensure no modules using SSL are imported before gevent monkey-patching [[details](https://github.com/langgenius/dify/issues/29599)]. Let me know if you need step-by-step instructions for changing the worker class or want to discuss async migration! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/bef19d63-9022-4aa4-83dc-4838b0384136?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20known%20issue%3A%20MilvusClient%20initialization%20hangs%20in%20the%20Celery%20worker%20due%20to%20a%20conflict%20between%20gevent%27s%20monkey-patching%20and%20gRPC%20%28used%20by%20pymilvus%29.%20This%20happens%20because%20Dify%27s%20Celery%20workers%20typically%20use%20gevent%20for%20concurrency%2C%20but%20gRPC%20%28and%20thus%20pymilvus%29%20is%20not%20compatible%20with%20gevent%27s%20monkey-patched%20sockets%2C%20leading%20to%20the%20worker%20stalling%20with%20no%20error%20or%20logs%20when%20trying%20to%20connect%20to%20Milvus.%20Your%20configuration%20and%20Milvus%20server%20are%20fine%E2%80%94this%20is%20an%20environment%20compatibility%20problem.%0A%0A%2A%2ATemporary%20workaround%3A%2A%2A%20%20%0ASet%20the%20Celery%20worker%20class%20to%20%60prefork%60%20to%20avoid%20gevent%20monkey-patching.%20You%20can%20do%20this%20by%20setting%20the%20environment%20variable%3A%0A%60%60%60%0ACELERY_WORKER_CLASS%3Dprefork%0A%60%60%60%0Aand%20restarting%20your%20worker%20containers.%20This%20forces%20Celery%20to%20use%20separate%20processes%20instead%20of%20gevent%20greenlets%2C%20which%20avoids%20the%20monkey-patching%20conflict%20and%20allows%20gRPC%20to%20work%20normally%20in%20each%20worker%20process.%20This%20fix%20is%20confirmed%20to%20resolve%20the%20issue%20for%20Milvus%20users%20in%20Dify%20workers%20%5B%5Bdetails%5D%28https%3A//github.com/langgenius/dify/issues/17641%29%5D.%0A%0A%2A%2APermanent%20solution%3A%2A%2A%20%20%0AConsider%20migrating%20to%20%60AsyncMilvusClient%60%20with%20asyncio%20if%20your%20workflow%20supports%20it.%20Milvus%20documentation%20provides%20guidance%20for%20async%20client%20usage%20%5B%5Bdetails%5D%28https%3A//github.com/langgenius/dify/issues/17641%29%5D.%0A%0A%2A%2AOther%20notes%3A%2A%2A%20%20%0A-%20Make%20sure%20your%20Milvus%20URI%20uses%20the%20container%20name%20or%20IP%20%28not%20%60localhost%60%20or%20%60127.0.0.1%60%29%20and%20that%20all%20relevant%20containers%20are%20on%20the%20same%20Docker%20network%20%5B%5Bdetails%5D%28https%3A//github.com/langgenius/dify/issues/20119%29%5D.%0A-%20If%20you%20see%20recursion%20errors%20or%20instability%20after%20upgrading%20to%20Dify%201.11.1%2C%20check%20your%20gevent%20version%20%28downgrade%20to%2024.11.1%20if%20using%20Python%203.12%29%2C%20and%20ensure%20no%20modules%20using%20SSL%20are%20imported%20before%20gevent%20monkey-patching%20%5B%5Bdetails%5D%28https%3A//github.com/langgenius/dify/issues/29599%29%5D.%0A%0ALet%20me%20know%20if%20you%20need%20step-by-step%20instructions%20for%20changing%20the%20worker%20class%20or%20want%20to%20discuss%20async%20migration%21)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/30438)
Author
Owner

@NieRonghua commented on GitHub (Dec 31, 2025):

@dosu,Setting CELERY_WORKER_CLASS to prefork is definitely not a good practice, as it will significantly degrade the performance of production services. My service runs stably in Dify version 1.8.1. Can changing the versions of pymilvus and grpcio solve this problem in version 1.11.1?

@NieRonghua commented on GitHub (Dec 31, 2025): @dosu,Setting CELERY_WORKER_CLASS to prefork is definitely not a good practice, as it will significantly degrade the performance of production services. My service runs stably in Dify version 1.8.1. Can changing the versions of pymilvus and grpcio solve this problem in version 1.11.1?
Author
Owner

@NieRonghua commented on GitHub (Dec 31, 2025):

@crazywoola
@JohnJyong
@Yeuoly

need some help, please

@NieRonghua commented on GitHub (Dec 31, 2025): @crazywoola @JohnJyong @Yeuoly need some help, please
Author
Owner

@fatelei commented on GitHub (Dec 31, 2025):

@crazywoola @JohnJyong @Yeuoly

need some help, please

config MILVUS_URI to http://localhost:18080, let it use http instead of grpc, localhost maybe changed

@fatelei commented on GitHub (Dec 31, 2025): > [@crazywoola](https://github.com/crazywoola) [@JohnJyong](https://github.com/JohnJyong) [@Yeuoly](https://github.com/Yeuoly) > > need some help, please config MILVUS_URI to http://localhost:18080, let it use http instead of grpc, localhost maybe changed
Author
Owner

@fatelei commented on GitHub (Dec 31, 2025):

make sure milvus server support http

@fatelei commented on GitHub (Dec 31, 2025): make sure milvus server support http
Author
Owner

@NieRonghua commented on GitHub (Jan 3, 2026):

make sure milvus server support http

The Milvus server is working properly and supports HTTP connections.
Image

I changed the Celery worker's queue configuration from -P gevent to -P eventlet, and the problem was solved.

@NieRonghua commented on GitHub (Jan 3, 2026): > make sure milvus server support http The Milvus server is working properly and supports HTTP connections. <img width="940" height="812" alt="Image" src="https://github.com/user-attachments/assets/30dd018a-47f3-480d-bb75-f42cdad37366" /> I changed the Celery worker's queue configuration from -P gevent to -P eventlet, and the problem was solved.
Author
Owner

@WeiAugust commented on GitHub (Jan 20, 2026):

In /datasets/<uuid:dataset_id>/hit-testing ,the same issue may occur. Are there any good ways to resolve it?

@WeiAugust commented on GitHub (Jan 20, 2026): In `/datasets/<uuid:dataset_id>/hit-testing` ,the same issue may occur. Are there any good ways to resolve it?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21395