Skip to content

Commit 173ba3b

Browse files
committed
Add some locking assertions.
1 parent 77a33bb commit 173ba3b

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

Objects/genobject.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ static PySendResult
184184
gen_send_ex2_lock_held(PyGenObject *gen, PyObject *arg, PyObject **presult,
185185
int exc, int closing)
186186
{
187+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(gen);
187188
PyThreadState *tstate = _PyThreadState_GET();
188189
_PyInterpreterFrame *frame = &gen->gi_iframe;
189190

@@ -374,6 +375,7 @@ is_resume(_Py_CODEUNIT *instr)
374375
static PyObject *
375376
_PyGen_yf_lock_held(PyGenObject *gen)
376377
{
378+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(gen);
377379
if (gen->gi_frame_state == FRAME_SUSPENDED_YIELD_FROM) {
378380
_PyInterpreterFrame *frame = &gen->gi_iframe;
379381
// GH-122390: These asserts are wrong in the presence of ENTER_EXECUTOR!
@@ -406,6 +408,7 @@ gen_close_meth_impl(PyGenObject *self)
406408
/*[clinic end generated code: output=b6f489da23415c60 input=f90b586ecfb6d2fa]*/
407409
{
408410
PyGenObject *gen = _PyGen_CAST(self);
411+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(gen);
409412

410413
if (gen->gi_frame_state == FRAME_CREATED) {
411414
gen->gi_frame_state = FRAME_COMPLETED;
@@ -492,6 +495,7 @@ static PyObject *
492495
gen_throw_lock_held(PyGenObject *gen, int close_on_genexit,
493496
PyObject *typ, PyObject *val, PyObject *tb)
494497
{
498+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(gen);
495499
PyObject *yf = _PyGen_yf(gen);
496500

497501
if (yf) {
@@ -529,8 +533,10 @@ gen_throw_lock_held(PyGenObject *gen, int close_on_genexit,
529533
'yield from' or awaiting on with 'await'. */
530534
PyFrameState state = gen->gi_frame_state;
531535
gen->gi_frame_state = FRAME_EXECUTING;
536+
Py_BEGIN_CRITICAL_SECTION(yf);
532537
ret = gen_throw_lock_held((PyGenObject *)yf, close_on_genexit,
533538
typ, val, tb);
539+
Py_END_CRITICAL_SECTION();
534540
gen->gi_frame_state = state;
535541
tstate->current_frame = prev;
536542
frame->previous = NULL;
@@ -815,6 +821,7 @@ gen_getrunning_get_impl(PyGenObject *self)
815821
static PyObject *
816822
gen_getsuspended_lock_held(PyGenObject *self)
817823
{
824+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
818825
return PyBool_FromLong(FRAME_STATE_SUSPENDED(self->gi_frame_state));
819826
}
820827

@@ -835,6 +842,7 @@ static PyObject *
835842
gen_getframe_lock_held(PyGenObject *self, const char *name)
836843
{
837844
PyGenObject *gen = _PyGen_CAST(self);
845+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(gen);
838846
if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
839847
return NULL;
840848
}
@@ -854,6 +862,7 @@ static PyObject *
854862
gen_getframe_get_impl(PyGenObject *self)
855863
/*[clinic end generated code: output=69a961dad790fd48 input=0d906f30ab99e1e4]*/
856864
{
865+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
857866
return gen_getframe_lock_held(self, "gi_frame");
858867
}
859868

@@ -1240,6 +1249,7 @@ static PyObject *
12401249
cr_getframe_get_impl(PyCoroObject *self)
12411250
/*[clinic end generated code: output=300f3facb67ebc50 input=32fca6ffc44085b7]*/
12421251
{
1252+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
12431253
return gen_getframe_lock_held(_PyGen_CAST(self), "cr_frame");
12441254
}
12451255

@@ -1669,6 +1679,7 @@ static PyObject *
16691679
ag_getframe_get_impl(PyAsyncGenObject *self)
16701680
/*[clinic end generated code: output=7a31c7181090a4fb input=b059ce210436a682]*/
16711681
{
1682+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
16721683
return gen_getframe_lock_held(_PyGen_CAST(self), "ag_frame");
16731684
}
16741685

@@ -1688,6 +1699,7 @@ static PyObject *
16881699
ag_getsuspended_get_impl(PyAsyncGenObject *self)
16891700
/*[clinic end generated code: output=f9c6d455edce4c50 input=e463d3db950251a3]*/
16901701
{
1702+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
16911703
return gen_getsuspended_lock_held(_PyGen_CAST(self));
16921704
}
16931705

@@ -2188,8 +2200,9 @@ async_gen_athrow_traverse(PyObject *self, visitproc visit, void *arg)
21882200

21892201

21902202
static PyObject *
2191-
async_gen_athrow_send(PyObject *self, PyObject *arg)
2203+
async_gen_athrow_send_lock_held(PyObject *self, PyObject *arg)
21922204
{
2205+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
21932206
PyAsyncGenAThrow *o = _PyAsyncGenAThrow_CAST(self);
21942207
PyGenObject *gen = _PyGen_CAST(o->agt_gen);
21952208
PyObject *retval;
@@ -2321,10 +2334,20 @@ async_gen_athrow_send(PyObject *self, PyObject *arg)
23212334
return NULL;
23222335
}
23232336

2337+
static PyObject *
2338+
async_gen_athrow_send(PyObject *self, PyObject *arg)
2339+
{
2340+
PyObject *res;
2341+
Py_BEGIN_CRITICAL_SECTION(self);
2342+
res = async_gen_athrow_send_lock_held(self, arg);
2343+
Py_END_CRITICAL_SECTION();
2344+
return res;
2345+
}
23242346

23252347
static PyObject *
2326-
async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2348+
async_gen_athrow_throw_lock_held(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
23272349
{
2350+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
23282351
PyAsyncGenAThrow *o = _PyAsyncGenAThrow_CAST(self);
23292352

23302353
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
@@ -2391,6 +2414,15 @@ async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
23912414
}
23922415
}
23932416

2417+
static PyObject *
2418+
async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2419+
{
2420+
PyObject *res;
2421+
Py_BEGIN_CRITICAL_SECTION(self);
2422+
res = async_gen_athrow_throw_lock_held(self, args, nargs);
2423+
Py_END_CRITICAL_SECTION();
2424+
return res;
2425+
}
23942426

23952427
static PyObject *
23962428
async_gen_athrow_iternext(PyObject *agt)
@@ -2400,8 +2432,9 @@ async_gen_athrow_iternext(PyObject *agt)
24002432

24012433

24022434
static PyObject *
2403-
async_gen_athrow_close(PyObject *self, PyObject *args)
2435+
async_gen_athrow_close_lock_held(PyObject *self, PyObject *args)
24042436
{
2437+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
24052438
PyAsyncGenAThrow *agt = _PyAsyncGenAThrow_CAST(self);
24062439
if (agt->agt_state == AWAITABLE_STATE_CLOSED) {
24072440
Py_RETURN_NONE;
@@ -2424,6 +2457,15 @@ async_gen_athrow_close(PyObject *self, PyObject *args)
24242457
}
24252458
}
24262459

2460+
static PyObject *
2461+
async_gen_athrow_close(PyObject *self, PyObject *args)
2462+
{
2463+
PyObject *res;
2464+
Py_BEGIN_CRITICAL_SECTION(self);
2465+
res = async_gen_athrow_close_lock_held(self, args);
2466+
Py_END_CRITICAL_SECTION();
2467+
return res;
2468+
}
24272469

24282470
static void
24292471
async_gen_athrow_finalize(PyAsyncGenAThrow *o)

0 commit comments

Comments
 (0)