Commit 4796607
committed
Reduce chance of crashes in CTRL-C handler, rework handler example
Inside a signal handler, you cannot allocate memory because the signal
handler, being implemented with a C
[`signal`](https://en.cppreference.com/w/c/program/signal) call, can be
called _during_ a memory allocation - when that happens, the CTRL-C
handler causes a segfault and/or other inconsistent state.
Similarly, the call can happen from a non-nim thread or inside a C
library function call etc, most of which do not support reentrancy and
therefore cannot be called _from_ a signal handler.
The stack trace facility used in the default handler is unfortunately
beyond fixing without more significant refactoring since it uses
garbage-collected types in its API and implementation, but we can at
least allocate the buffer outside of the signal handler itself -
hopefully, this should reduce the frequency of crashes, if nothing else.
It will still crash from time to time on Windows in particular, but
since we're about to quit without cleanup, the loss of functionality is
simited (ie the stack trace will not show and a crash dump will happen
which the OS will notice).
Finally, the example of a ctrl-c handler performs the same mistake of
calling `echo` which is not well-defined - replace it with an example
that is mostly correct (except maybe for the lack of `volatile` for the
`stop` variable).1 parent c6352ce commit 4796607
2 files changed
+38
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2192 | 2192 | | |
2193 | 2193 | | |
2194 | 2194 | | |
2195 | | - | |
| 2195 | + | |
2196 | 2196 | | |
2197 | 2197 | | |
2198 | | - | |
2199 | 2198 | | |
2200 | | - | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
2201 | 2213 | | |
2202 | | - | |
2203 | | - | |
2204 | | - | |
| 2214 | + | |
| 2215 | + | |
2205 | 2216 | | |
2206 | 2217 | | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
2207 | 2222 | | |
2208 | 2223 | | |
2209 | 2224 | | |
2210 | | - | |
| 2225 | + | |
2211 | 2226 | | |
2212 | 2227 | | |
2213 | 2228 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
619 | 620 | | |
620 | 621 | | |
621 | 622 | | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
622 | 635 | | |
623 | 636 | | |
624 | 637 | | |
| |||
643 | 656 | | |
644 | 657 | | |
645 | 658 | | |
646 | | - | |
647 | | - | |
648 | | - | |
649 | | - | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
650 | 662 | | |
651 | 663 | | |
652 | 664 | | |
| |||
0 commit comments