mirror of https://github.com/python/cpython
bpo-43962: Fix _PyInterpreterState_IDIncref() (GH-25683)
_PyInterpreterState_IDIncref() now calls _PyInterpreterState_IDInitref() and always increments id_refcount.
This commit is contained in:
parent
21b02b5f40
commit
32c5a17444
|
@ -338,7 +338,7 @@ struct _xidregitem {
|
||||||
PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(int64_t);
|
PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(int64_t);
|
||||||
|
|
||||||
PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *);
|
PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *);
|
||||||
PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *);
|
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(struct _is *);
|
||||||
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
|
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
_PyInterpreterState_IDIncref() now calls _PyInterpreterState_IDInitref() and
|
||||||
|
always increments id_refcount. Previously, calling
|
||||||
|
_xxsubinterpreters.get_current() could create an id_refcount inconsistency
|
||||||
|
when a _xxsubinterpreters.InterpreterID object was deallocated. Patch by
|
||||||
|
Victor Stinner.
|
|
@ -24,15 +24,21 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (interp != NULL) {
|
||||||
|
if (_PyInterpreterState_IDIncref(interp) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interpid *self = PyObject_New(interpid, cls);
|
interpid *self = PyObject_New(interpid, cls);
|
||||||
if (self == NULL) {
|
if (self == NULL) {
|
||||||
|
if (interp != NULL) {
|
||||||
|
_PyInterpreterState_IDDecref(interp);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
self->id = id;
|
self->id = id;
|
||||||
|
|
||||||
if (interp != NULL) {
|
|
||||||
_PyInterpreterState_IDIncref(interp);
|
|
||||||
}
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -538,24 +538,25 @@ _PyInterpreterState_IDInitref(PyInterpreterState *interp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
int
|
||||||
_PyInterpreterState_IDIncref(PyInterpreterState *interp)
|
_PyInterpreterState_IDIncref(PyInterpreterState *interp)
|
||||||
{
|
{
|
||||||
if (interp->id_mutex == NULL) {
|
if (_PyInterpreterState_IDInitref(interp) < 0) {
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
|
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
|
||||||
interp->id_refcount += 1;
|
interp->id_refcount += 1;
|
||||||
PyThread_release_lock(interp->id_mutex);
|
PyThread_release_lock(interp->id_mutex);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
|
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
|
||||||
{
|
{
|
||||||
if (interp->id_mutex == NULL) {
|
assert(interp->id_mutex != NULL);
|
||||||
return;
|
|
||||||
}
|
|
||||||
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
|
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
|
||||||
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
|
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
|
||||||
assert(interp->id_refcount != 0);
|
assert(interp->id_refcount != 0);
|
||||||
|
|
Loading…
Reference in New Issue