fix: add SIGINT signal handler for reliable Ctrl+C experiment abort#1504
Conversation
sys.excepthook alone cannot reliably catch KeyboardInterrupt when the main thread is blocked in C extensions (NumPy/PyTorch). Register a signal.SIGINT handler as an additional safety net to ensure experiments are always marked as "aborted" on Ctrl+C. Fixes SwanHubX#1329
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of experiment termination in SwanLab by introducing a dedicated Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a SIGINT signal handler to reliably mark experiments as 'aborted' when interrupted with Ctrl+C. This is a solid improvement, especially for cases where the main thread is blocked in C extensions. The implementation is clean and includes appropriate cleanup and unit tests.
I've found one potential issue in the signal handler's logic concerning how it chains to the original handler, specifically for the SIG_IGN case. I've left a specific comment with a suggested fix. Overall, this is a valuable change that enhances the robustness of the experiment lifecycle management.
When the original handler was SIG_IGN, the handler incorrectly raised KeyboardInterrupt instead of silently returning. Simplify the logic to explicitly handle SIG_IGN, callable handlers, and SIG_DFL separately. Add test for SIG_IGN scenario. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
LGTM! |
Invoke the saved original excepthook (self._sys_origin_excepthook) instead of directly calling sys.__excepthook__, and make the KeyboardInterrupt log message clearer ("aborting run..."). Update unit tests to set and assert against the saved hook, rename tests for clarity, and add coverage to ensure an outer framework's hook is used (and the builtin is not called) as well as that internal errors still result in calling the saved hook.
Summary
signal.SIGINThandler alongside the existingsys.excepthookto reliably detect Ctrl+Csys.excepthookalone fails when the main thread is blocked in C extensions (NumPy, PyTorch, etc.), causing experiments to miss the "aborted" statefinish(state="aborted"), restores the original handler, and re-raises the signal for normal process termination_cleanup()to avoid leaking into user code after the run endsChanges
swanlab/sdk/internal/run/__init__.py: Added_sigint_handlermethod, SIGINT registration in__init__, and restoration in_cleanuptests/unit/sdk/internal/run/test_finish_hook.py: Added 3 test cases covering running state, non-running state, and original callable handler delegationTesting
Fixes #1329