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: tracing and tests that raise an exception in a SIGALRM handler
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: vstinner, xdegaye
Priority: normal Keywords: patch

Created on 2014-02-11 17:34 by xdegaye, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
no_tracing.diff xdegaye, 2014-02-11 17:34 review
tracing_signals.patch xdegaye, 2014-02-13 15:28 review
signal_test.py xdegaye, 2014-02-13 15:29
tracing_signals_2.patch xdegaye, 2014-02-14 17:44 review
Messages (6)
msg210984 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-02-11 17:34
After an alarm handler raises an exception while a tracing function is being invoked and when this exception is not caught by the tracing function, the call_trampoline() function in sysmodule.c returns NULL and its caller, trace_trampoline(), removes the trace function.  Therefore, tests that raise an exception in an alarm handler should use the support.no_tracing decorator as it is done in test_io.py at check_reentrant_write().

Patch attached.
msg211099 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-02-12 16:25
The following code shows that the trace function is removed when an alarm raises an exception and prints: 'trace function: None'

import sys, signal, time

def trace(frame, event, arg):
    if frame.f_code.co_name == 'foo':
        while 1:
            time.sleep(.02)
    return trace

def foo(): pass

def handler(*args):
    1/0

sys.settrace(trace)
signal.signal(signal.SIGALRM, handler)
signal.alarm(1)
try:
    foo()
except ZeroDivisionError:
    pass

print('trace function:', sys.gettrace())
msg211100 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-02-12 16:28
> Therefore, tests that raise an exception in an alarm handler should use the support.no_tracing decorator as it is done in test_io.py at check_reentrant_write().

I would prefer to fix the issue instead. A signal should not remove the trace function.
msg211159 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-02-13 15:28
> A signal should not remove the trace function.

And a signal should be traced.
The attached patch postpones all signals except SIGINT while the trace function is being invoked, and thus the signal is traced after returning from this function.
My previous test with a little improvement has been attached too and outputs now, with this patch:

tracing foo
tracing handler
Got ZeroDivisionError.
trace function: <function trace at 0x7fada2776c90>

I did not test the patch against WITH_THREAD not being defined.
msg211227 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-02-14 17:44
With the previous patch, after the signal arrives and the first call to PyErr_CheckSignals, PyErr_CheckSignals is called for each bytecode processed in the eval loop
of the tracing function (and the eval loop of functions called by the tracing function, and also functions called in the other threads).
This is annoying. The attached patch fixes this and adds a test case.
msg211247 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-02-14 22:59
Hum hum, and now with this last patch PyErr_CheckSignals is called for each traced line even when no signal is pending, which is worse than the previous one as far as performance is concerned.
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64800
2014-02-14 22:59:26xdegayesetmessages: + msg211247
2014-02-14 17:44:22xdegayesetfiles: + tracing_signals_2.patch

messages: + msg211227
2014-02-13 15:29:32xdegayesetfiles: + signal_test.py
2014-02-13 15:28:18xdegayesetfiles: + tracing_signals.patch

messages: + msg211159
2014-02-12 16:28:27vstinnersetnosy: + vstinner
messages: + msg211100
2014-02-12 16:25:06xdegayesetmessages: + msg211099
2014-02-11 17:34:36xdegayecreate