File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -108,10 +108,33 @@ class LEDs(IntEnum):
108
108
def __init__ (self ) -> None :
109
109
if IS_PI :
110
110
LOGGER .debug ("Configuring LED controller" )
111
+ self ._register_exit ()
111
112
atexit .register (GPIO .cleanup ) # type: ignore[attr-defined]
112
113
GPIO .setmode (GPIO .BCM )
113
114
GPIO .setup ([led .value for led in self .LEDs ], GPIO .OUT , initial = GPIO .LOW )
114
115
116
+ def _register_exit (self ) -> None :
117
+ """
118
+ Ensure `atexit` triggers on `SIGTERM`.
119
+
120
+ > The functions registered via [`atexit`] are not called when the program is
121
+ killed by a signal not handled by Python
122
+ """
123
+
124
+ if signal .getsignal (signal .SIGTERM ) != signal .SIG_DFL :
125
+ # If a signal handler is already present for SIGTERM,
126
+ # this is sufficient for `atexit` to trigger, so do nothing.
127
+ return
128
+
129
+ def handle_signal (handled_signum : int , frame ) -> None :
130
+ """Semi-default signal handler for SIGTERM, enough for atexit."""
131
+ USERCODE_LOGGER .error (signal .strsignal (handled_signum ))
132
+ exit (128 + handled_signum ) # 143 for SIGTERM
133
+
134
+ # Add the null-ish signal handler
135
+ signal .signal (signal .SIGTERM , handle_signal )
136
+
137
+
115
138
def mark_start (self ) -> None :
116
139
if IS_PI :
117
140
GPIO .output (self .LEDs .BOOT_100 , GPIO .HIGH )
You can’t perform that action at this time.
0 commit comments