Skip to content

fix: use asyncio.Event() for thread-safe initialization state#2383

Open
ixchio wants to merge 4 commits intoOpenHands:mainfrom
ixchio:fix/asyncio-event-initialization-state-1825
Open

fix: use asyncio.Event() for thread-safe initialization state#2383
ixchio wants to merge 4 commits intoOpenHands:mainfrom
ixchio:fix/asyncio-event-initialization-state-1825

Conversation

@ixchio
Copy link
Contributor

@ixchio ixchio commented Mar 11, 2026

Summary

Closes #1825

Replaces the plain boolean _initialization_complete flag in server_details_router.py with asyncio.Event(), making the initialization signal explicit and idiomatic for async code.

Changes

  • _initialization_complete = Falseasyncio.Event()
  • mark_initialization_complete() now calls .set() instead of assigning True
  • /ready endpoint now uses .is_set() instead of a truthiness check
  • Added tests for the /ready endpoint covering both pre- and post-initialization states

Why asyncio.Event()?

While Python's GIL protects simple boolean assignments, asyncio.Event() communicates intent clearly:

  • It's the standard primitive for signalling completion in async code
  • Enables future callers to await _initialization_complete.wait() if needed
  • Eliminates the footgun of accidentally awaiting a bool

Testing

All existing tests pass. New tests added in tests/agent_server/test_server_details_router.py:

tests/agent_server/test_server_details_router.py::test_ready_returns_503_before_init PASSED
tests/agent_server/test_server_details_router.py::test_ready_returns_200_after_init PASSED
tests/agent_server/test_server_details_router.py::test_ready_resets_after_new_event  PASSED

ixchio and others added 4 commits March 11, 2026 10:56
…outer

Replace the plain boolean _initialization_complete flag with asyncio.Event()
for more idiomatic async code in an async application.

Changes:
- _initialization_complete = False → asyncio.Event()
- mark_initialization_complete() → calls .set() instead of assigning True
- ready() endpoint → uses .is_set() instead of truthiness check
- Add tests for the /ready endpoint before and after initialization

This makes the initialization signal explicit and async-friendly, avoiding
any footgun from accidentally awaiting a bool in the future. No behaviour
change is expected from existing callers.

Closes OpenHands#1825

Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider using asyncio.Event() for thread-safe initialization state

1 participant