Skip to content

Conversation

@yunlishao
Copy link
Contributor

Summary

Implements graceful shutdown handling to eliminate errors when closing TrafficLayer or SUMO in any order. Users can now close the TrafficLayer console window (X button), close SUMO first, or use Ctrl+C without encountering error dialogs or crashes.

Related Issues / Tasks

Closes #66

Type of Change

  • Bug fix
  • New feature
  • Maintenance / Refactor
  • Documentation
  • Test case / scenario update

Affected Modules / Components

  • TrafficLayer/TrafficLayer/mainTrafficLayer.cpp
    • Added ShutdownState struct for centralized shutdown coordination
    • Added performCleanup() function with proper shutdown sequence
    • Enhanced CtrlHandler() to catch all Windows console events
    • Protected all SUMO operations with connection state checks
    • Updated main loop and error handlers for graceful exit
  • tests/Python/SimpleEchoClient/simple_echo_client.py
    • Added detection of shutdown signal (state=0)
    • Improved error handling for connection reset

Test Cases

Scenario 1: Close TrafficLayer window (X button)

  • Start TrafficLayer with SUMO and Python client
  • Click X button on TrafficLayer console
  • ✅ No errors, Python client receives shutdown signal and exits cleanly

Scenario 2: Close SUMO first

  • Start TrafficLayer with SUMO and Python client
  • Close SUMO window
  • ✅ TrafficLayer detects disconnection and shuts down gracefully, no abort() error

Scenario 3: Ctrl+C shutdown

  • Start TrafficLayer with SUMO and Python client
  • Press Ctrl+C in TrafficLayer console
  • ✅ Clean shutdown with proper client notification

Scenario 4: Python client shutdown notification

  • Start TrafficLayer with SUMO and Python client
  • Close TrafficLayer any way
  • ✅ Python client prints "Received shutdown signal from server. Exiting gracefully..."

Environment

Only fill in the tools used for testing this PR:

  • Python version: 3.x
  • MATLAB/Simulink/dSPACE version:
  • SUMO version: 1.20+
  • VISSIM version:
  • IPG CarMaker version:

Checklist

  • Code compiles/runs as expected
  • Tests pass locally
  • Documentation is updated (if applicable)
  • Relevant issues are linked
  • Version-specific changes are noted (if any)

Additional Notes (optional)

Implementation Details

Shutdown Sequence:

  1. Notify all connected clients (state=0 signal)
  2. 100ms delay to ensure message transmission
  3. Close SUMO/VISSIM connection (skipped if already closed)
  4. Close all socket connections

Bidirectional Detection:

  • User closes TrafficLayer → Console events trigger cleanup
  • User closes SUMO → Socket/step errors set trafficSimulatorClosed flag

Key Design Decisions:

  • No blocking MessageBox popups (suitable for automated workflows)
  • Console logging preserved for debugging
  • All cleanup steps wrapped in try-catch for error resilience
  • Proper cleanup order maintained: clients → SUMO → sockets

This commit implements graceful shutdown handling to eliminate errors
when closing TrafficLayer or SUMO in any order.

Changes:
- Add ShutdownState struct to manage shutdown coordination and track
  connection state
- Implement performCleanup() function with proper shutdown sequence:
  1. Notify clients with shutdown signal (state=0)
  2. Close SUMO/VISSIM connection gracefully
  3. Close socket connections
- Enhance Windows console event handler to catch all events:
  CTRL_C, CTRL_BREAK, CTRL_CLOSE (X button), CTRL_LOGOFF, CTRL_SHUTDOWN
- Add bidirectional shutdown detection:
  - Detect when SUMO closes first via socket errors
  - Prevent calling SUMO functions after disconnection detected
- Change main loop from while(1) to while(!g_shutdown.shutdownRequested)
- Update all error handlers to trigger graceful shutdown instead of exit()
- Add 100ms delay after client notification to ensure message transmission
- Update Python SimpleEchoClient to handle shutdown signal gracefully

Benefits:
- Users can close TrafficLayer window (X button) without errors
- Users can close SUMO first without TrafficLayer showing errors
- Python clients exit cleanly when receiving shutdown notification
- No blocking popups - suitable for automated simulation workflows
- Proper resource cleanup in all shutdown scenarios

Related to #66
@yunlishao yunlishao merged commit 1459ef3 into main Oct 22, 2025
@yunlishao yunlishao deleted the feature/#66_graceful_shutdown branch October 22, 2025 13:57
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.

[FEATURE] Implement graceful shutdown for TrafficLayer

3 participants