Skip to content

Commit

Permalink
bpo-43390: Set SA_ONSTACK in PyOS_setsig (pythonGH-24730)
Browse files Browse the repository at this point in the history
This is friendlier to other in-process code that an extension module or
embedding use could pull in such as CGo where tiny stacks are the norm
and sigaltstack() has been used to provide for signal handlers.

Without this, signals received by a process using tiny stacks may lead
to stack overflow crashes.
  • Loading branch information
gpshead committed Mar 5, 2021
1 parent 2122e48 commit 02ac6f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CPython now sets the ``SA_ONSTACK`` flag in ``PyOS_setsig`` for the VM's
default signal handlers. This is friendlier to other in-process code that
an extension module or embedding use could pull in (such as Golang's cgo)
where tiny thread stacks are the norm and ``sigaltstack()`` has been used to
provide for signal handlers. This is a no-op change for the vast majority
of processes that don't use sigaltstack.
5 changes: 4 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2884,7 +2884,10 @@ PyOS_setsig(int sig, PyOS_sighandler_t handler)
struct sigaction context, ocontext;
context.sa_handler = handler;
sigemptyset(&context.sa_mask);
context.sa_flags = 0;
/* Using SA_ONSTACK is friendlier to other C/C++/Golang-VM code that
* extension module or embedding code may use where tiny thread stacks
* are used. https://bugs.python.org/issue43390 */
context.sa_flags = SA_ONSTACK;
if (sigaction(sig, &context, &ocontext) == -1)
return SIG_ERR;
return ocontext.sa_handler;
Expand Down

0 comments on commit 02ac6f4

Please sign in to comment.