Fix double Ctrl+C signal handling in wssshd

- Replaced counter-based signal handling with flag-based approach
- Added sigint_handled flag to prevent multiple signal handler executions
- First Ctrl+C: Sets flag and initiates graceful shutdown
- Second Ctrl+C: Forces immediate exit with os._exit(1)
- Prevents race conditions and multiple signal handler calls
- Ensures proper single-signal behavior for graceful shutdown

This resolves the issue where a single Ctrl+C was triggering both graceful shutdown and immediate exit messages.
parent 26930a9b
......@@ -237,20 +237,19 @@ async def run_server():
# Set up signal handling for clean exit
shutdown_event.clear()
sigint_count = 0
sigint_handled = False
def signal_handler(signum, frame):
nonlocal sigint_count
sigint_count += 1
if sigint_count == 1:
print("\nReceived SIGINT, attempting graceful shutdown...")
shutdown_event.set()
elif sigint_count >= 2:
nonlocal sigint_handled
if sigint_handled:
# Already handling a signal, force exit
print("\nReceived second SIGINT, exiting immediately...")
# Force exit without cleanup
os._exit(1)
sigint_handled = True
print("\nReceived SIGINT, attempting graceful shutdown...")
shutdown_event.set()
# Register signal handler for SIGINT (Ctrl+C)
signal.signal(signal.SIGINT, signal_handler)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment