Skip to content

Commit a662c80

Browse files
author
Adam Hrbac
committed
Ensure async actions do not get traced
Since they run non-deterministically, the tests can randomly fail, and the debugger can jump into some unrelated weakref callback, making tracing them undesirable.
1 parent f7a2c06 commit a662c80

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_sys_settrace.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import unittest
4141
import difflib
4242
import sys
43+
import signal
4344

4445
import builtins
4546

@@ -198,3 +199,21 @@ def fun1():
198199
(12, 'fun1', 'line', None)]
199200
if self.events != events:
200201
self.fail('\n'+'\n'.join(difflib.ndiff([str(x) for x in events], [str(x) for x in self.events])))
202+
203+
def simpler_trace(self, fr, ev, arg):
204+
self.events.append(fr.f_code.co_name)
205+
206+
@unittest.skipIf(not hasattr(signal, 'SIGUSR1'), "User defined signal not present")
207+
def test_07_async_actions_not_traced(self):
208+
def handler(*_): handler.called = 1
209+
210+
def helper(): return 1
211+
signal.signal(signal.SIGUSR1, handler)
212+
self.events = []
213+
sys.settrace(self.simpler_trace)
214+
signal.raise_signal(signal.SIGUSR1)
215+
for i in range(1000): helper()
216+
sys.settrace(None)
217+
# handler.called is not checked, since it could cause a transient
218+
for name in self.events:
219+
self.assertEqual(name, 'helper')

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
204204
import com.oracle.truffle.api.Truffle;
205205
import com.oracle.truffle.api.TruffleLanguage;
206-
import com.oracle.truffle.api.TruffleSafepoint;
207206
import com.oracle.truffle.api.exception.AbstractTruffleException;
208207
import com.oracle.truffle.api.frame.Frame;
209208
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -1883,7 +1882,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18831882
}
18841883
}
18851884
}
1886-
TruffleSafepoint.poll(this);
1885+
PythonContext.triggerAsyncActions(this);
18871886
oparg = 0;
18881887
continue;
18891888
}
@@ -2332,6 +2331,7 @@ private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonCo
23322331
if (threadState.isTracing()) {
23332332
return;
23342333
}
2334+
assert event != PythonContext.TraceEvent.DISABLED;
23352335
threadState.tracingStart(event);
23362336
PFrame pyFrame = mutableData.setPyFrame(ensurePyFrame(virtualFrame, mutableData.getPyFrame()));
23372337
Object traceFn = useLocalFn ? pyFrame.getLocalTraceFun() : threadState.getTraceFun();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ protected boolean proceed() {
122122
@Override
123123
public final void execute(PythonContext context) {
124124
Debugger debugger = null;
125+
PythonContext.PythonThreadState threadState = null;
126+
PythonLanguage language = context.getLanguage();
125127
do {
126128
Object callable = callable();
127129
if (callable != null) {
@@ -136,6 +138,13 @@ public final void execute(PythonContext context) {
136138
if (debugger == null) {
137139
debugger = Debugger.find(context.getEnv());
138140
}
141+
if (threadState == null) {
142+
threadState = context.getThreadState(language);
143+
}
144+
boolean alreadyTracing = threadState.isTracing();
145+
if (!alreadyTracing) {
146+
threadState.tracingStart(PythonContext.TraceEvent.DISABLED);
147+
}
139148
debugger.disableStepping();
140149
try {
141150
GenericInvokeNode.getUncached().execute(context.getAsyncHandler().callTarget, args);
@@ -148,6 +157,9 @@ public final void execute(PythonContext context) {
148157
ExceptionUtils.printPythonLikeStackTrace(e);
149158
} finally {
150159
debugger.restoreStepping();
160+
if (!alreadyTracing) {
161+
threadState.tracingStop();
162+
}
151163
}
152164
}
153165
} while (proceed());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public enum TraceEvent {
191191
CALL("call"),
192192
EXCEPTION("exception"),
193193
LINE("line"),
194-
RETURN("return");
194+
RETURN("return"),
195+
DISABLED("");
195196

196197
public final TruffleString pythonName;
197198

0 commit comments

Comments
 (0)