Skip to content

[FEATURE] Implement graceful shutdown for TrafficLayer #66

@yunlishao

Description

@yunlishao

Is your feature request related to a problem?
Yes. Currently, shutting down TrafficLayer causes errors depending on the shutdown order:

  • Close TrafficLayer.exe first → SUMO pops error (abrupt socket disconnection via libtraci)
  • Close SUMO first → TrafficLayer pops error (socket error when trying to communicate)

Users need to know a specific shutdown order to avoid error popups, which is not user-friendly.

Describe the solution you'd like
Implement graceful shutdown that handles both shutdown scenarios without errors:

  1. Dedicated cleanup function performCleanup(bool emergencyShutdown) with proper sequence:

    • Notify clients (send shutdown signal with state=0)
    • Call libtraci::Simulation::close() to close SUMO connection gracefully
    • Call socketShutdown() to close sockets
  2. Enhanced Ctrl handler to catch all Windows console events:

    • CTRL_C_EVENT - Ctrl+C pressed
    • CTRL_BREAK_EVENT - Ctrl+Break pressed
    • CTRL_CLOSE_EVENT - Console window closed (X button) ← Currently missing!
    • CTRL_LOGOFF_EVENT - User logs off
    • CTRL_SHUTDOWN_EVENT - System shutdown
  3. Bidirectional shutdown detection:

    • User closes TrafficLayer (any method) → triggers graceful SUMO close
    • User closes SUMO first → detected via socket error → triggers graceful cleanup
  4. Main loop with shutdown flag: Change while(1) to while(!g_shutdownRequested)

Describe alternatives you've considered

  • Current approach: Documenting the "correct" shutdown order for users (not ideal)
  • Ignoring errors: Suppress error messages but don't actually clean up properly
  • Watchdog process: External process to coordinate shutdown (too complex)

Environment (if feature is environment-specific)

  • C++ compiler/version: MSVC (Windows-specific console events)
  • SUMO version: Using libtraci/libsumo API

Additional context

Implementation Checklist

Files to modify:

  • TrafficLayer/TrafficLayer/mainTrafficLayer.cpp

Steps:

  1. Add global variables at top of file (before CtrlHandler):

    volatile bool g_shutdownRequested = false;
    TrafficHelper* g_Traffic_c = nullptr;
    SocketHelper* g_Sock_c = nullptr;
    MsgHelper* g_MsgClient_c = nullptr;
    vector<int>* g_actualClientSock = nullptr;
    float* g_simTime = nullptr;
    bool g_initialized = false;
  2. Add performCleanup(bool emergencyShutdown) function with try-catch protection

  3. Update CtrlHandler() at line ~40:

    • Add cases for CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT
    • Call performCleanup(true) for emergency events
    • Set g_shutdownRequested = true for Ctrl+C/Break
  4. In main() around line ~548:

    • Change while(1) to while(!g_shutdownRequested)
  5. In main() after connection setup:

    • Initialize global pointers (g_Traffic_c, g_Sock_c, etc.)
    • Set g_initialized = true
  6. In recv error handling (line ~610-620):

    • Check for socket errors indicating SUMO disconnection
    • Set g_shutdownRequested = true and break instead of immediate exit(-1)
  7. After main loop exits (line ~960):

    • Call performCleanup(false) for normal graceful shutdown
    • Replace existing Traffic_c.close() call

Critical Points

  • CTRL_CLOSE_EVENT handling is essential (window X button)
  • Windows gives ~5 seconds for close events before force-kill
  • Must call libtraci::Simulation::close() BEFORE closing sockets
  • Order matters: clients → SUMO → sockets
  • All cleanup steps need try-catch for error resilience

Current Code Context

  • Current CtrlHandler only handles CTRL_C_EVENT (line 45)
  • Main loop is while(1) starting at line 548
  • Traffic_c.close() exists at line 960 but sockets close first (wrong order)
  • Multiple exit points call Sock_c.socketShutdown() then exit(-1) (scattered throughout)

Related to issue #55 (libsumo/libtraci migration)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions