Skip to content

fix: skip SIGINT handler registration outside the main thread - #1710

Merged
SAKURA-CAT merged 2 commits into
SwanHubX:mainfrom
Tensionteng:fix/sigint-main-thread-only
Jul 24, 2026
Merged

fix: skip SIGINT handler registration outside the main thread#1710
SAKURA-CAT merged 2 commits into
SwanHubX:mainfrom
Tensionteng:fix/sigint-main-thread-only

Conversation

@Tensionteng

@Tensionteng Tensionteng commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Description

signal.signal(signal.SIGINT, ...) can only be called from the main thread. In non-main-thread contexts such as Ray actors, registering the handler during Run.__init__ raises ValueError and crashes initialization.

Closes: #1708

This PR:

  • Only registers the SIGINT handler when Run.__init__ is called on the main thread.
  • Tracks whether the handler was actually registered via _sigint_handler_registered.
  • Only restores the original SIGINT handler in finish() if it was previously registered.
  • Adds unit tests covering main-thread registration, non-main-thread skipping, and conditional restoration.
UV_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple uv run pytest tests/unit -q
1561 passed, 21 skipped, 8 warnings in 59.17s

Tensionteng and others added 2 commits July 22, 2026 20:41
Replace the `_sigint_handler_registered` boolean with a single
`_is_main_thread`
check computed at init time.  This avoids a superfluous field and makes
it
impossible for the restore logic to get out of sync with the
registration
decision.

Also clean up test fixtures: use setup/teardown methods for global state
restoration and rename the final test to reflect the new invariant.
@SAKURA-CAT

Copy link
Copy Markdown
Member

Thx!

@SAKURA-CAT
SAKURA-CAT merged commit 5fdc8c1 into SwanHubX:main Jul 24, 2026
19 checks passed
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.

swanlab.init() 在非主线程(Ray actor)中崩溃:ValueError: signal only works in main thread of the main interpreter

2 participants