Skip to content

Commit 9e4ea98

Browse files
committed
pythongh-119333: Back up exception before calling PyContext_WatchCallback
I believe that the value of a simpler API (and defense against poorly written callbacks) outweighs the cost of backing up and restoring the thread's exception state.
1 parent 6d0d26e commit 9e4ea98

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

Doc/c-api/contextvars.rst

+2-5
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ Context object management functions:
139139
exception will be printed as an unraisable exception using
140140
:c:func:`PyErr_FormatUnraisable`. Otherwise it should return ``0``.
141141
142-
There may already be a pending exception set on entry to the callback. In
143-
this case, the callback should return ``0`` with the same exception still
144-
set. This means the callback may not call any other API that can set an
145-
exception unless it saves and clears the exception state first, and restores
146-
it before returning.
142+
Any pending exception is cleared before the callback is called and restored
143+
after the callback returns.
147144
148145
.. versionadded:: 3.14
149146

Include/cpython/context.h

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef enum {
4040
*
4141
* if the callback returns with an exception set, it must return -1. Otherwise
4242
* it should return 0
43+
*
44+
* Any pending exception is cleared before the callback is called and restored
45+
* after the callback returns.
4346
*/
4447
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
4548

Lib/test/test_capi/test_watchers.py

+10
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,16 @@ def _in_context(stack):
640640
ctx.run(_in_context, stack)
641641
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
642642

643+
def test_exception_save(self):
644+
with self.context_watcher(2):
645+
with catch_unraisable_exception() as cm:
646+
def _in_context():
647+
raise RuntimeError("test")
648+
649+
with self.assertRaisesRegex(RuntimeError, "test"):
650+
contextvars.copy_context().run(_in_context)
651+
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
652+
643653
def test_clear_out_of_range_watcher_id(self):
644654
with self.assertRaisesRegex(ValueError, r"Invalid context watcher ID -1"):
645655
_testcapi.clear_context_watcher(-1)

Python/context.c

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static void notify_context_watchers(PyContextEvent event, PyContext *ctx, PyThre
119119
assert(interp->_initialized);
120120
uint8_t bits = interp->active_context_watchers;
121121
int i = 0;
122+
PyObject *exc = _PyErr_GetRaisedException(ts);
122123
while (bits) {
123124
assert(i < CONTEXT_MAX_WATCHERS);
124125
if (bits & 1) {
@@ -133,6 +134,7 @@ static void notify_context_watchers(PyContextEvent event, PyContext *ctx, PyThre
133134
i++;
134135
bits >>= 1;
135136
}
137+
_PyErr_SetRaisedException(ts, exc);
136138
}
137139

138140

0 commit comments

Comments
 (0)