Staging
v0.5.1
https://github.com/python/cpython
Revision e89f95bfd0881a9b80c3b1430d154a77bdf5a824 authored by Yury Selivanov on 09 June 2017, 21:06:39 UTC, committed by GitHub on 09 June 2017, 21:06:39 UTC
If we have a chain of generators/coroutines that are 'yield from'ing
each other, then resuming the stack works like:

- call send() on the outermost generator
- this enters _PyEval_EvalFrameDefault, which re-executes the
  YIELD_FROM opcode
- which calls send() on the next generator
- which enters _PyEval_EvalFrameDefault, which re-executes the
  YIELD_FROM opcode
- ...etc.

However, every time we enter _PyEval_EvalFrameDefault, the first thing
we do is to check for pending signals, and if there are any then we
run the signal handler. And if it raises an exception, then we
immediately propagate that exception *instead* of starting to execute
bytecode. This means that e.g. a SIGINT at the wrong moment can "break
the chain" – it can be raised in the middle of our yield from chain,
with the bottom part of the stack abandoned for the garbage collector.

The fix is pretty simple: there's already a special case in
_PyEval_EvalFrameEx where it skips running signal handlers if the next
opcode is SETUP_FINALLY. (I don't see how this accomplishes anything
useful, but that's another story.) If we extend this check to also
skip running signal handlers when the next opcode is YIELD_FROM, then
that closes the hole – now the exception can only be raised at the
innermost stack frame.

This shouldn't have any performance implications, because the opcode
check happens inside the "slow path" after we've already determined
that there's a pending signal or something similar for us to process;
the vast majority of the time this isn't true and the new check
doesn't run at all..
(cherry picked from commit ab4413a7e9bda95b6fcd517073e2a51dafaa1624)
1 parent b757745
History
Tip revision: e89f95bfd0881a9b80c3b1430d154a77bdf5a824 authored by Yury Selivanov on 09 June 2017, 21:06:39 UTC
[3.6] bpo-30039: Don't run signal handlers while resuming a yield from stack (GH-1081) (#1640)
Tip revision: e89f95b
File Mode Size
.github
Doc
Grammar
Include
Lib
Mac
Misc
Modules
Objects
PC
PCbuild
Parser
Programs
Python
Tools
.bzrignore -rw-r--r-- 582 bytes
.gitattributes -rw-r--r-- 255 bytes
.gitignore -rw-r--r-- 1.3 KB
.hgeol -rw-r--r-- 1.0 KB
.hgignore -rw-r--r-- 1.3 KB
.hgtags -rw-r--r-- 8.7 KB
.mention-bot -rw-r--r-- 39 bytes
.travis.yml -rw-r--r-- 3.9 KB
LICENSE -rw-r--r-- 12.5 KB
Makefile.pre.in -rw-r--r-- 57.9 KB
README.rst -rw-r--r-- 9.1 KB
aclocal.m4 -rw-r--r-- 10.7 KB
config.guess -rwxr-xr-x 43.2 KB
config.sub -rwxr-xr-x 35.7 KB
configure -rwxr-xr-x 473.8 KB
configure.ac -rw-r--r-- 156.6 KB
install-sh -rwxr-xr-x 7.0 KB
pyconfig.h.in -rw-r--r-- 40.4 KB
setup.py -rw-r--r-- 98.8 KB

README.rst

back to top