Skip to content

fix: clean shutdown without hanging on Ctrl+C#118

Open
m-mcgowan wants to merge 1 commit intoJakeler:mainfrom
m-mcgowan:fix/clean-shutdown
Open

fix: clean shutdown without hanging on Ctrl+C#118
m-mcgowan wants to merge 1 commit intoJakeler:mainfrom
m-mcgowan:fix/clean-shutdown

Conversation

@m-mcgowan
Copy link

Summary

When pressing Ctrl+C to exit, the application would sometimes hang because BLE notifications continued to queue data during shutdown, preventing the run loop from reaching its exit sentinel.

Problem

  1. User presses Ctrl+C during active BLE data transfer
  2. stop_loop() adds None sentinel to the end of the queue
  3. BLE notifications keep arriving and adding data to the queue
  4. The run loop processes queued data but never reaches the None sentinel
  5. Application hangs indefinitely

Solution

linux_pty.py:

  • Add _stopping flag to reject new writes during shutdown
  • Clear any pending queue items before adding the exit sentinel

main.py:

  • Reorder shutdown sequence: stop loops before cleanup
  • Add timeouts to asyncio.gather() and bt.disconnect() to prevent hanging

Testing

Tested on macOS with ESP32-S3 device using Nordic UART Service. Before the fix, Ctrl+C would hang when data was being received. After the fix, shutdown completes promptly.

When pressing Ctrl+C to exit, the application would sometimes hang
because BLE notifications continued to queue data during shutdown,
preventing the run loop from reaching its exit sentinel.

Changes:
- linux_pty.py: Add _stopping flag to reject writes during shutdown
- linux_pty.py: Clear pending queue items before adding exit sentinel
- main.py: Reorder shutdown to stop loops before cleanup
- main.py: Add timeouts to prevent hanging on disconnect

This ensures a clean, prompt shutdown even when BLE data is still
being received.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@m-mcgowan m-mcgowan marked this pull request as ready for review February 1, 2026 01:22
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.

1 participant