-
Notifications
You must be signed in to change notification settings - Fork 6
Update handling of event loop life cycle #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,14 @@ def __init__( | |
| ): | ||
| self._controller = controller | ||
| self._transports = transports | ||
| self._loop = loop or asyncio.get_event_loop() | ||
|
|
||
| if loop is not None: | ||
| self._loop = loop | ||
| self._close_loop_on_stop = False | ||
| else: | ||
| self._loop = asyncio.new_event_loop() | ||
| asyncio.set_event_loop(self._loop) | ||
| self._close_loop_on_stop = True | ||
|
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Inspecting lifecycle sites:"
rg -n -C2 'new_event_loop|set_event_loop|get_event_loop|close\(' src/fastcs/control_system.py src/fastcs/launch.py
echo
echo "Checking what happens after closing a loop that is still current:"
python - <<'PY'
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.close()
try:
current = asyncio.get_event_loop()
print("get_event_loop returned closed loop:", current.is_closed())
except Exception as exc:
print(type(exc).__name__, exc)
finally:
try:
asyncio.set_event_loop(None)
except Exception:
pass
PYRepository: DiamondLightSource/fastcs Length of output: 1808 🌐 Web query:
💡 Result:
So, if you do this in a thread: loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) # bind loop to this thread
loop.close()
loop2 = asyncio.get_event_loop() # <- often returns the same (now closed) loop…then What to do instead
Sources: Python event-loop docs (loop close semantics; get/set loop behavior) [1][2], Python policy docs (per-thread-by-default context; default policy behavior changes) [3], CPython issue discussing the deprecation/behavior changes around 🌐 Web query:
💡 Result: Best practice (recommended): don’t touch thread-local loops—use
|
||
|
|
||
| self._scan_coros: list[ScanCallback] = [] | ||
| self._initial_coros: list[ScanCallback] = [] | ||
|
|
@@ -158,3 +165,5 @@ async def interactive_shell( | |
|
|
||
| def __del__(self): | ||
| self._stop_scan_tasks() | ||
| if self._close_loop_on_stop and not self._loop.is_closed(): | ||
| self._loop.close() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: DiamondLightSource/fastcs
Length of output: 1612
Bind
serve()toself._loopexplicitly.When an external loop is passed, it is not set as the thread-local current event loop. However,
run()still createsserveviaasyncio.ensure_future(), which implicitly binds the task to the current thread-local loop. If they differ,self._loop.run_until_complete(serve)raisesValueError: The future belongs to a different loop than the one specified as the loop argument. Useself._loop.create_task()instead.Proposed fix
🤖 Prompt for AI Agents