Skip to content

Commit 4c0f668

Browse files
committed
Fix corner case in traceback frame handling for stack overflow
1 parent e3c2686 commit 4c0f668

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorTryExceptNode.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public void executeVoid(VirtualFrame frame) {
8989
getBody().executeVoid(frame);
9090
} catch (PException exception) {
9191
gen.setActive(frame, exceptFlag, true);
92-
catchExceptionInGeneratorFirstTime(frame, exception);
92+
if (!catchExceptionInGeneratorFirstTime(frame, exception)) {
93+
throw exception;
94+
}
9395
reset(frame);
9496
return;
9597
} catch (ControlFlowException e) {
@@ -98,7 +100,9 @@ public void executeVoid(VirtualFrame frame) {
98100
PException pe = wrapJavaExceptionIfApplicable(e);
99101
if (pe != null) {
100102
gen.setActive(frame, exceptFlag, true);
101-
catchExceptionInGeneratorFirstTime(frame, pe);
103+
if (!catchExceptionInGeneratorFirstTime(frame, pe)) {
104+
throw pe.getExceptionForReraise();
105+
}
102106
reset(frame);
103107
return;
104108
} else {
@@ -112,7 +116,7 @@ public void executeVoid(VirtualFrame frame) {
112116
}
113117

114118
@ExplodeLoop
115-
private void catchExceptionInGeneratorFirstTime(VirtualFrame frame, PException exception) {
119+
private boolean catchExceptionInGeneratorFirstTime(VirtualFrame frame, PException exception) {
116120
boolean wasHandled = false;
117121
ExceptNode[] exceptNodes = getExceptNodes();
118122
// we haven't found the matching node, yet, start searching
@@ -132,10 +136,7 @@ private void catchExceptionInGeneratorFirstTime(VirtualFrame frame, PException e
132136
}
133137
}
134138
}
135-
if (!wasHandled) {
136-
// we tried and haven't found a matching except node
137-
throw exception;
138-
}
139+
return wasHandled;
139140
}
140141

141142
@ExplodeLoop
@@ -173,7 +174,7 @@ private void runExceptionHandler(VirtualFrame frame, PException exception, Excep
173174
throw e;
174175
}
175176
tryChainExceptionFromHandler(handlerException, exception);
176-
throw handlerException;
177+
throw handlerException.getExceptionForReraise();
177178
} finally {
178179
restoreExceptionState(frame, savedExceptionState);
179180
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorTryFinallyNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void executeVoid(VirtualFrame frame) {
112112
if (activeException != null) {
113113
tryChainExceptionFromHandler(handlerException, activeException);
114114
}
115-
throw handlerException;
115+
throw handlerException.getExceptionForReraise();
116116
} finally {
117117
if (activeException != null) {
118118
restoreExceptionState(frame, savedExceptionState);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ExceptionHandlingStatementNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public abstract class ExceptionHandlingStatementNode extends StatementNode {
7070
@Child private PythonObjectFactory ofactory;
7171
@CompilationFinal private LoopConditionProfile contextChainHandledProfile;
7272
@CompilationFinal private LoopConditionProfile contextChainContextProfile;
73-
@CompilationFinal private Boolean shouldCatchAllExceptions;
7473
@CompilationFinal private ContextReference<PythonContext> contextRef;
7574
@CompilationFinal private LanguageReference<PythonLanguage> languageRef;
7675

@@ -199,8 +198,7 @@ public static PException wrapJavaException(Throwable e, Node node, PBaseExceptio
199198
pe.setHideLocation(true);
200199
// Re-attach truffle stacktrace
201200
moveTruffleStackTrace(e, pe);
202-
// Create a new traceback chain, because the current one has been finalized by Truffle
203-
return pe.getExceptionForReraise();
201+
return pe;
204202
}
205203

206204
protected final PException wrapJavaExceptionIfApplicable(Throwable e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/TryExceptNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void executeVoid(VirtualFrame frame) {
106106
if (handled) {
107107
return;
108108
} else {
109-
throw pe;
109+
throw pe.getExceptionForReraise();
110110
}
111111
}
112112
throw e;
@@ -144,7 +144,7 @@ private boolean catchException(VirtualFrame frame, TruffleException exception) {
144144
throw e;
145145
}
146146
tryChainExceptionFromHandler(handlerException, exception);
147-
throw handlerException;
147+
throw handlerException.getExceptionForReraise();
148148
}
149149
return false;
150150
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/TryFinallyNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void handleException(VirtualFrame frame, PException handledException) {
8484
throw e;
8585
}
8686
tryChainExceptionFromHandler(handlerException, handledException);
87-
throw handlerException;
87+
throw handlerException.getExceptionForReraise();
8888
} finally {
8989
restoreExceptionState(frame, exceptionState);
9090
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/WithNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected void handleException(VirtualFrame frame, Object withObject, Object exi
190190
throw e;
191191
}
192192
tryChainExceptionFromHandler(handlerException, pException);
193-
throw handlerException;
193+
throw handlerException.getExceptionForReraise();
194194
} finally {
195195
restoreExceptionState(frame, savedExceptionState);
196196
}

0 commit comments

Comments
 (0)