This infinite loop: def f(): a=b=0 while 1: if a<b: pass f() doesn't respond to a Ctrl-C interrupt. If you modify the loop in various ways, like removing the function wrapper, then Ctrl-C works fine. It can be interrupted with Ctrl-\. This occurs if the program is run from a file or in the interactive interpreter. Tested on Python 2.7.3 / Linux 3.2.0-64 x86_64, and Python 2.7.6 / Linux 3.13.0-27. Reproducible: always Expected behaviour: Ctrl-C interrupts program Actual beviour: Ctrl-C prints ^C
The problem is in the ceval.c, the core of Python bytecode interpreter. For performances, it doesn't check if pending calls should be called for each instructio. It uses "fast dispatch" which doesn't check pending calls. The problem is that a signal schedules a pending call. The scheduled call is never executed on Python 2. Python 3.2 introduced an atomic eval_breaker variable which fixes this issue. It is part of the huge change "new GIL": --- changeset: 57175:fdd6484f1210 parent: 57172:6d91aaadddd0 user: Antoine Pitrou <solipsis@pitrou.net> date: Tue Nov 10 19:50:40 2009 +0000 files: Include/ceval.hInclude/pystate.hInclude/sysmodule.hLib/test/test_sys.py Makefile.pre.in Objects/longob description: Merge in the new GIL. --- I'm not sure that it would be possible to only backport the "eval_breaker" variable. Anyway, changing ceval.c in minor Python 2.7 release is risky. I would prefer to close the bug as wontfix. IMO the safe solution is to upgrade to Python 3.2 or later.
It's not a major usability issue for me, and I wouldn't be too distressed by a WONTFIX, though I don't know how much it affects other people. I've just noticed that this is a smaller version: while 1: if 0<0: pass I'm curious as to why the above is not interruptible, but things like while 1: if 0+0: pass are.
History
Date
User
Action
Args
2022-04-11 14:58:05
admin
set
github: 66069
2014-06-27 02:29:56
r.david.murray
set
status: open -> closedresolution: wont fixstage: resolved