Skip to content

Fix signal handler re-entrancy and hang on Ctrl+C - #14852

Merged
Nir-Az merged 1 commit into
realsenseai:developmentfrom
Nir-Az:fix-signal-cleanup-hang
Mar 23, 2026
Merged

Nir-Az merged 1 commit into
realsenseai:developmentfrom
Nir-Az:fix-signal-cleanup-hang

Conversation

@Nir-Az

@Nir-Az Nir-Az commented Mar 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Prevent re-entrant signal handling: a _cleanup_in_progress guard ensures a second Ctrl+C (or SIGTERM) during cleanup force-exits immediately via os._exit(1) instead of recursing into _cleanup_devices again
  • Use os._exit(1) instead of sys.exit(1) after cleanup to avoid hanging on BrainStem USB hub threads that block SystemExit

Test plan

  • On Windows: run pytest with an Acroname hub, press Ctrl+C — verify ports disable and process exits cleanly
  • On Linux: same test with Ctrl+C
  • Jenkins abort (SIGTERM): verify graceful shutdown without hang
  • Press Ctrl+C twice rapidly — verify second signal force-exits immediately

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the Python unit-test signal handling utilities to avoid re-entrant cleanup and to prevent hangs on Ctrl+C/SIGTERM by forcing a hard process exit once cleanup begins.

Changes:

  • Add a module-level _cleanup_in_progress guard to prevent re-entrant signal handling and force-exit on a second signal during cleanup.
  • Replace sys.exit(1) with os._exit(1) to avoid hangs caused by threads blocking normal interpreter shutdown.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


from rspy import log
import sys, signal
import os, sys, signal

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

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

sys is no longer used in this module after switching from sys.exit to os._exit. Please remove the unused import (or, if you keep os._exit, use sys to flush stdio before exiting).

Copilot uses AI. Check for mistakes.
Comment on lines 19 to +21
log.w("got signal", signum, "aborting... ")
signal_handler()
sys.exit(1)
os._exit(1)

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

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

os._exit() bypasses Python exception propagation and stdio flushing. If signal_handler() raises or logs buffered output, the process may continue unexpectedly or drop the last log lines. Consider wrapping signal_handler() in a try/finally (or try/except + finally) that always flushes output and then calls os._exit(1) so abort is guaranteed even if cleanup fails.

Copilot uses AI. Check for mistakes.

@AviaAv AviaAv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@Nir-Az
Nir-Az merged commit 75a1732 into realsenseai:development Mar 23, 2026
30 of 31 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.

3 participants