Commit fd2744a
Refactor signal handler in mbpoll.c to use volatile flag
The previous signal handler implementation called unsafe functions (printf, exit, free) directly from the signal context, which is undefined behavior and can lead to deadlocks or crashes.
This commit refactors the signal handling logic:
- Introduces a global `volatile sig_atomic_t g_bExitSignal` flag.
- Updates `vSigIntHandler` to only set this flag.
- Renames the old handler logic to `vCleanup` and calls it from `main` after the loop exits.
- Updates the main polling loop to check `g_bExitSignal` periodically and break the loop on signal.
- Suppresses error messages from blocking Modbus calls if the exit signal is set, ensuring a clean exit experience.
This change ensures async-signal-safety while preserving the existing behavior of printing statistics and exit messages on SIGINT.
Co-authored-by: zknpr <96851588+zknpr@users.noreply.github.com>1 parent 0d5b8ec commit fd2744a
1 file changed
Lines changed: 36 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| 269 | + | |
| 270 | + | |
269 | 271 | | |
270 | 272 | | |
271 | 273 | | |
| |||
367 | 369 | | |
368 | 370 | | |
369 | 371 | | |
| 372 | + | |
370 | 373 | | |
371 | 374 | | |
372 | 375 | | |
| |||
932 | 935 | | |
933 | 936 | | |
934 | 937 | | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
935 | 941 | | |
936 | 942 | | |
937 | 943 | | |
| |||
985 | 991 | | |
986 | 992 | | |
987 | 993 | | |
988 | | - | |
989 | | - | |
990 | | - | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
991 | 999 | | |
992 | 1000 | | |
993 | 1001 | | |
| |||
996 | 1004 | | |
997 | 1005 | | |
998 | 1006 | | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
999 | 1010 | | |
1000 | 1011 | | |
1001 | 1012 | | |
| |||
1016 | 1027 | | |
1017 | 1028 | | |
1018 | 1029 | | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
1019 | 1034 | | |
1020 | 1035 | | |
1021 | 1036 | | |
| |||
1050 | 1065 | | |
1051 | 1066 | | |
1052 | 1067 | | |
1053 | | - | |
1054 | | - | |
1055 | | - | |
1056 | | - | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
1057 | 1074 | | |
1058 | 1075 | | |
1059 | | - | |
| 1076 | + | |
1060 | 1077 | | |
1061 | 1078 | | |
1062 | 1079 | | |
1063 | 1080 | | |
1064 | 1081 | | |
1065 | 1082 | | |
1066 | 1083 | | |
1067 | | - | |
| 1084 | + | |
1068 | 1085 | | |
1069 | 1086 | | |
1070 | | - | |
1071 | | - | |
| 1087 | + | |
| 1088 | + | |
1072 | 1089 | | |
1073 | 1090 | | |
1074 | 1091 | | |
| |||
1341 | 1358 | | |
1342 | 1359 | | |
1343 | 1360 | | |
1344 | | - | |
| 1361 | + | |
1345 | 1362 | | |
1346 | 1363 | | |
1347 | 1364 | | |
| |||
1366 | 1383 | | |
1367 | 1384 | | |
1368 | 1385 | | |
1369 | | - | |
| 1386 | + | |
1370 | 1387 | | |
1371 | 1388 | | |
1372 | 1389 | | |
1373 | 1390 | | |
1374 | 1391 | | |
1375 | 1392 | | |
1376 | | - | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
1377 | 1399 | | |
1378 | 1400 | | |
1379 | 1401 | | |
| |||
0 commit comments