Skip to content

Fix: tolerate non-main-thread event loops in signal-handler setup - #92

Merged
andre-merzky merged 2 commits into
mainfrom
fix/nonmain-thread-loop
Aug 19, 2026
Merged

Fix: tolerate non-main-thread event loops in signal-handler setup#92
andre-merzky merged 2 commits into
mainfrom
fix/nonmain-thread-loop

Conversation

@andre-merzky

Copy link
Copy Markdown
Member

Problem

WorkflowEngine.create() crashes with a RuntimeError when the engine is created on an event loop that does not run on the main thread: loop.add_signal_handler() raises (signal handlers can only be installed from the main thread), and _setup_signal_handlers() only tolerated the narrower exception raised on some platforms. This blocks embedding the engine in host applications that run plugins on worker-thread event loops (e.g. radical.orbit plugin hosts).

Fix

  • Broaden the exception handling in _setup_signal_handlers() so the non-main-thread RuntimeError is tolerated as well.
  • Emit a one-time warning when signal-handler setup is skipped, so the degraded signal handling is visible but not noisy.
  • No new parameters, no API changes.

Testing

  • New regression test that creates the engine on a non-main-thread event loop; it fails on unmodified main with the exact RuntimeError this change addresses.
  • tox -e py313: 92 passed / 2 skipped (baseline on main: 91 passed / 2 skipped).
  • ruff check clean; the ruff format complaint on workflow_manager.py (~line 794) is pre-existing on main and untouched by this change.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents WorkflowEngine.create() from crashing when instantiated on an asyncio event loop running in a non-main thread, by tolerating add_signal_handler() failures in that environment and adding a regression test to cover the scenario.

Changes:

  • Broaden _setup_signal_handlers() exception handling to skip signal registration when add_signal_handler() fails off the main thread, and emit a warning when handlers are skipped.
  • Add a unit test that creates and shuts down an engine from an event loop running in a secondary thread.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/unit/test_termination.py Adds regression coverage for creating/shutting down an engine on a non-main-thread event loop.
src/radical/asyncflow/workflow_manager.py Updates signal-handler setup to tolerate non-main-thread loops and warn when signal handlers are not installed.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/radical/asyncflow/workflow_manager.py Outdated
WorkflowEngine.create() crashed when the event loop was running in a
secondary thread: _setup_signal_handlers() calls loop.add_signal_handler()
which raises RuntimeError ("set_wakeup_fd only works in main thread of the
main interpreter") off the main thread, while only NotImplementedError was
caught.

Skip signal handler installation upfront when the engine is not created on
the main thread, and warn once that the host process has to manage shutdown
itself.  Exception handling inside the registration loop is left unchanged,
so genuine errors still surface.  There is no matching
remove_signal_handler() site, so no teardown change is needed.

Adds a unit test creating and shutting down an engine on a loop in a
secondary thread.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@AymenFJA AymenFJA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me thanks @andre-merzky

@AymenFJA

Copy link
Copy Markdown
Collaborator

@andre-merzky feel free to merge assuming this is ready :)

@andre-merzky
andre-merzky merged commit 90b4b4c into main Aug 19, 2026
12 checks passed
@andre-merzky
andre-merzky deleted the fix/nonmain-thread-loop branch August 19, 2026 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants