fix(ws): per-instance event loop instead of module-level global (fixes #119, fixes #133) - #153
Open
Xuxchloris wants to merge 1 commit into
Open
fix(ws): per-instance event loop instead of module-level global (fixes #119, fixes #133)#153Xuxchloris wants to merge 1 commit into
Xuxchloris wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #119, fixes #133
Problem
lark_oapi/ws/client.pycaptured an event loop in a module-level global at import time:This broke two real scenarios:
ws.Clientinstances share the single globalloop. When several bots run in separate threads, they overwrite each other's loop andstart()fails withRuntimeError: This event loop is already running.asyncio.run(...), the global captures the already-running loop and the subsequentloop.run_until_complete(...)fails immediately.Changes
lark_oapi/ws/client.pyloopglobal entirely._get_loop()(created lazily on first use, per instance) — distinct clients never share a loop._get_lock()) from a coroutine running on the client's own loop, so theasyncio.Locknever binds to a foreign loop.loop.create_task(...)inside async methods replaced withasyncio.get_running_loop().create_task(...).lark_oapi/channel/tests/test_ws_client_loop.py(new): 4 regression tests — distinct clients get distinct loops (stable per instance), no module-levelloopattribute remains, a client constructed insideasyncio.rungets a dedicated loop (not the running one), and the lock is created lazily.Verification
python -m pytest lark_oapi/channel/tests— 666 passed; the single failure (test_upload_error_propagation.py::test_gather_buffer_missing_local_file_raises_upload_failed) is a pre-existing Windows-only path-escaping assertion unrelated to this change (CI runs on Linux).start()creates the client's loop and blocks on it exactly as before.