mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
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. As an alternative to https://github.com/nim-lang/Nim/pull/25110, this PR removes the most problematic signal handler, namely the one for SIGINT (ctrl-c) - SIGINT is special because it's meant to cause a regular shutdown of the application and crashes during SIGINT handling are both confusing and, if turned into SIGSEGV, have downstream effects like core dumps and OS crash reports. The signal handlers for the various crash scenarios remain as-is - they may too cause their own crashes but we're already going down in a bad way, so the harm is more limited - in particular, crashing during a crash handler corrupts `core`/crash dumps. Users wanting to keep their core files pristine should continue to use `-d:noSignalHandler` - this is usually the better option for production applications since they carry more detail than the Nim stack trace that gets printed. 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).