Skip to content

Commit 6b6b8e6

Browse files
committed
Make generators thread safe.
1 parent 61fa342 commit 6b6b8e6

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Objects/genobject.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ gen_dealloc(PyObject *self)
175175
}
176176

177177
static PySendResult
178-
gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
178+
gen_send_ex2_lock_held(PyGenObject *gen, PyObject *arg, PyObject **presult,
179179
int exc, int closing)
180180
{
181181
PyThreadState *tstate = _PyThreadState_GET();
@@ -273,6 +273,17 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
273273
return result ? PYGEN_RETURN : PYGEN_ERROR;
274274
}
275275

276+
static inline PySendResult
277+
gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
278+
int exc, int closing)
279+
{
280+
PySendResult result;
281+
Py_BEGIN_CRITICAL_SECTION(gen);
282+
result = gen_send_ex2_lock_held(gen, arg, presult, exc, closing);
283+
Py_END_CRITICAL_SECTION();
284+
return result;
285+
}
286+
276287
static PySendResult
277288
PyGen_am_send(PyObject *self, PyObject *arg, PyObject **result)
278289
{

0 commit comments

Comments
 (0)