This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Ctrl-C doesn't interrupt simple loop
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: apsaras, pitrou, r.david.murray, vstinner
Priority: normal Keywords:

Created on 2014-06-25 14:13 by apsaras, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg221550 - (view) Author: Alex (apsaras) Date: 2014-06-25 14:13
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
msg221552 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-25 14:42
I can duplicate this.  ctl-c works fine in 3.5, however.
msg221654 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-06-26 22:41
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.h Include/pystate.h Include/sysmodule.h Lib/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.
msg221658 - (view) Author: Alex (apsaras) Date: 2014-06-26 23:05
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:05adminsetgithub: 66069
2014-06-27 02:29:56r.david.murraysetstatus: open -> closed
resolution: wont fix
stage: resolved
2014-06-26 23:05:17apsarassetmessages: + msg221658
2014-06-26 22:41:18vstinnersetnosy: + vstinner, pitrou
messages: + msg221654
2014-06-25 14:42:48r.david.murraysetnosy: + r.david.murray
messages: + msg221552
2014-06-25 14:27:53apsarassettype: behavior
2014-06-25 14:26:20apsarassetcomponents: - Interpreter Core
2014-06-25 14:13:05apsarascreate