Commit 9627622
fix(async): include event loop ID in connection alias to prevent reusing closed connections (#3086)
# Description
This PR fixes a critical issue where `AsyncMilvusClient` could reuse a
connection associated with a closed event loop, causing `RuntimeError:
Event loop is closed` when running multiple async tasks or tests
sequentially.
## Problem
* The `create_connection` utility generates a connection alias based on
URI, user, and other parameters but **ignores the running event loop**.
* In `AsyncMilvusClient`, connections are cached globally by this alias.
* If an event loop is closed (e.g., after `asyncio.run()` finishes) and
a new loop is started, the client reuses the **cached connection** which
is bound to the **old, closed** loop.
* This results in errors like: `RuntimeError: Event loop is closed` when
trying to use the client in the new loop.
## Solution
* Updated `create_connection` in `pymilvus/milvus_client/_utils.py` to
include the **current event loop ID** in the connection alias when
`use_async=True`.
* **New Alias Format:** `async-{uri}-{hash}-loop{loop_id}`
* This ensures that each event loop gets its own dedicated connection
instance.
* Added a check to raise `RuntimeError` if no running loop is found in
an async context (fail-fast instead of silent failure).
## Verification
* Added a new unit test `tests/test_async_milvus_client_reuse.py` to
reproduce the issue and verify the fix.
* The test confirms that different event loops generate different
connection aliases, preventing invalid reuse.
* Existing tests pass.
## Related Issue
#3087
Co-authored-by: silas.jiang <silas.jiang@zilliz.com>1 parent ffeedbd commit 9627622
File tree
2 files changed
+58
-2
lines changed- pymilvus/milvus_client
- tests
2 files changed
+58
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | | - | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
38 | 46 | | |
39 | 47 | | |
40 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
0 commit comments