Commit e245e31
committed
fix: remove deadlocks by making the scheduler signal handler signal-safe
See relevant comments - we can get deadlocks with logging and threading
primitives rarely which causes the process to hang around 5% of the time
on a SIGINT/SIGTERM, but we also want to be able to have the signal
interrupt our poll wait/sleep without busy waiting (for performance),
which means we also cannot use time.sleep (an early signal will not
interrupt this, and a pre-check leads to ToC-ToU races), nor
signal.sigtimedwait (registers its own handlers to handle signals inside
the wait, but misses signals outside).
This leaves us with one clear solution - use an OS pipe and define a
selector on the read file descriptor, and have the signal handler set a
flag with the signal number and write to the write file descriptor. By
querying the flag we always know if we have handled a signal in our main
loop, and by using a fd we reliably skip the wait on a signal, where the
wait is blocking (i.e. not a busy wait). The signal handler is then
minimal and async-signal-safe, just setting a flag and writing to the
pipe. The relevant logging logic is moved to be dispatched by the main
loop instead.
Signed-off-by: Alex Jones <[email protected]>1 parent 6c32f12 commit e245e31
1 file changed
+48
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
164 | 166 | | |
165 | 167 | | |
166 | 168 | | |
167 | | - | |
168 | | - | |
| 169 | + | |
| 170 | + | |
169 | 171 | | |
170 | | - | |
171 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
172 | 175 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
178 | 180 | | |
179 | | - | |
| 181 | + | |
180 | 182 | | |
181 | | - | |
182 | | - | |
| 183 | + | |
| 184 | + | |
183 | 185 | | |
184 | 186 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
| 187 | + | |
| 188 | + | |
190 | 189 | | |
191 | | - | |
192 | | - | |
193 | | - | |
| 190 | + | |
194 | 191 | | |
195 | | - | |
| 192 | + | |
| 193 | + | |
196 | 194 | | |
197 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
198 | 199 | | |
199 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
200 | 206 | | |
201 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
202 | 220 | | |
203 | 221 | | |
204 | 222 | | |
205 | 223 | | |
206 | 224 | | |
207 | 225 | | |
208 | 226 | | |
209 | | - | |
210 | | - | |
211 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
212 | 230 | | |
213 | 231 | | |
214 | 232 | | |
215 | 233 | | |
216 | 234 | | |
217 | 235 | | |
218 | 236 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 237 | + | |
| 238 | + | |
224 | 239 | | |
225 | 240 | | |
226 | 241 | | |
| |||
0 commit comments