Skip to content

Commit f1b792a

Browse files
authored
Fix signal handler restoration on exit (cyberbotics#6945)
* Fix signal handler restoration on exit * Change log
1 parent 2ff0367 commit f1b792a

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

docs/reference/changelog-r2025.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Fixed Python controllers on Windows ([#6933](https://github.com/cyberbotics/webots/pull/6933)).
2626
- OSM importer no longer crashes when run in 3d mode ([#6935](https://github.com/cyberbotics/webots/pull/6935)).
2727
- Fixed Java compilation deprecation warning and run-time warning ([#6936](https://github.com/cyberbotics/webots/pull/6936)).
28+
- Fixed controller signal handlers not restoring the original handler (e.g. CPython's) on exit ([#6945](https://github.com/cyberbotics/webots/pull/6945)).
2829

2930
## Webots R2025a
3031
Released on January 31st, 2025.

src/controller/c/robot.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,35 @@ static void init_robot_window_library() {
181181
fprintf(stderr, "Error: Cannot load the \"%s\" robot window library.\n", robot.window_filename);
182182
}
183183

184+
#ifdef _WIN32
185+
typedef void (*signal_handler_t)(int);
186+
static signal_handler_t original_sigint;
187+
#else
188+
static struct sigaction original_sigint;
189+
static struct sigaction original_sigterm;
190+
static struct sigaction original_sigquit;
191+
static struct sigaction original_sighup;
192+
#endif
193+
184194
static void quit_controller(int signal_number) {
185195
should_abort_simulation_waiting = true;
186-
signal(signal_number, SIG_DFL);
196+
#ifdef _WIN32
197+
signal(signal_number, original_sigint);
198+
#else
199+
struct sigaction *original = NULL;
200+
if (signal_number == SIGINT)
201+
original = &original_sigint;
202+
else if (signal_number == SIGTERM)
203+
original = &original_sigterm;
204+
else if (signal_number == SIGQUIT)
205+
original = &original_sigquit;
206+
else if (signal_number == SIGHUP)
207+
original = &original_sighup;
208+
if (original)
209+
sigaction(signal_number, original, NULL);
210+
else
211+
signal(signal_number, SIG_DFL);
212+
#endif
187213
raise(signal_number);
188214
}
189215

@@ -1414,11 +1440,17 @@ int wb_robot_init() { // API initialization
14141440
// one uint8 giving the number of devices n
14151441
// n \0-terminated strings giving the names of the devices 0 .. n-1
14161442

1417-
signal(SIGINT, quit_controller); // this signal is working on Windows when Ctrl+C from cmd.exe.
1418-
#ifndef _WIN32
1419-
signal(SIGTERM, quit_controller);
1420-
signal(SIGQUIT, quit_controller);
1421-
signal(SIGHUP, quit_controller);
1443+
#ifdef _WIN32
1444+
original_sigint = signal(SIGINT, quit_controller); // this signal is working on Windows when Ctrl+C from cmd.exe.
1445+
#else
1446+
struct sigaction sa;
1447+
sa.sa_handler = quit_controller;
1448+
sigemptyset(&sa.sa_mask);
1449+
sa.sa_flags = 0;
1450+
sigaction(SIGINT, &sa, &original_sigint);
1451+
sigaction(SIGTERM, &sa, &original_sigterm);
1452+
sigaction(SIGQUIT, &sa, &original_sigquit);
1453+
sigaction(SIGHUP, &sa, &original_sighup);
14221454
#endif
14231455

14241456
robot.configure = 0;

0 commit comments

Comments
 (0)