-
Notifications
You must be signed in to change notification settings - Fork 5
Description
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:
-
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
-
Enhanced Ctrl handler to catch all Windows console events:
CTRL_C_EVENT- Ctrl+C pressedCTRL_BREAK_EVENT- Ctrl+Break pressedCTRL_CLOSE_EVENT- Console window closed (X button) ← Currently missing!CTRL_LOGOFF_EVENT- User logs offCTRL_SHUTDOWN_EVENT- System shutdown
-
Bidirectional shutdown detection:
- User closes TrafficLayer (any method) → triggers graceful SUMO close
- User closes SUMO first → detected via socket error → triggers graceful cleanup
-
Main loop with shutdown flag: Change
while(1)towhile(!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:
-
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;
-
Add
performCleanup(bool emergencyShutdown)function with try-catch protection -
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 = truefor Ctrl+C/Break
- Add cases for
-
In
main()around line ~548:- Change
while(1)towhile(!g_shutdownRequested)
- Change
-
In
main()after connection setup:- Initialize global pointers (g_Traffic_c, g_Sock_c, etc.)
- Set
g_initialized = true
-
In recv error handling (line ~610-620):
- Check for socket errors indicating SUMO disconnection
- Set
g_shutdownRequested = trueandbreakinstead of immediateexit(-1)
-
After main loop exits (line ~960):
- Call
performCleanup(false)for normal graceful shutdown - Replace existing
Traffic_c.close()call
- Call
Critical Points
CTRL_CLOSE_EVENThandling 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()thenexit(-1)(scattered throughout)
Related to issue #55 (libsumo/libtraci migration)