Skip to content

Commit 87de6f7

Browse files
habermancopybara-github
authored andcommitted
Fixed asserts that could SEGV during shutdown.
During the shutdown sequence, it's possible for the module to have been removed prior to dealloc calls. Our dealloc calls were accessing the module state, but only for logic assertions. We can just bail on the assertion if the module state has been destroyed. This probably never affected OSS, because OSS builds always disable asserts. PiperOrigin-RevId: 770333761
1 parent 3cca08d commit 87de6f7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

python/descriptor_containers.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ static PyObject* PyUpb_DescriptorMap_Repr(PyObject* _self) {
2424
return ret;
2525
}
2626

27+
#define CHECK_TYPE(obj, state_member) \
28+
assert(PyUpb_ModuleState_MaybeGet() == NULL || /* During shutdown. */ \
29+
Py_TYPE(obj) == PyUpb_ModuleState_Get()->state_member)
30+
2731
// -----------------------------------------------------------------------------
2832
// ByNameIterator
2933
// -----------------------------------------------------------------------------
@@ -39,7 +43,7 @@ typedef struct {
3943
} PyUpb_ByNameIterator;
4044

4145
static PyUpb_ByNameIterator* PyUpb_ByNameIterator_Self(PyObject* obj) {
42-
assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_name_iterator_type);
46+
CHECK_TYPE(obj, by_name_iterator_type);
4347
return (PyUpb_ByNameIterator*)obj;
4448
}
4549

@@ -101,7 +105,7 @@ typedef struct {
101105
} PyUpb_ByNumberIterator;
102106

103107
static PyUpb_ByNumberIterator* PyUpb_ByNumberIterator_Self(PyObject* obj) {
104-
assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_number_iterator_type);
108+
CHECK_TYPE(obj, by_number_iterator_type);
105109
return (PyUpb_ByNumberIterator*)obj;
106110
}
107111

@@ -162,7 +166,7 @@ typedef struct {
162166
} PyUpb_GenericSequence;
163167

164168
PyUpb_GenericSequence* PyUpb_GenericSequence_Self(PyObject* obj) {
165-
assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->generic_sequence_type);
169+
CHECK_TYPE(obj, generic_sequence_type);
166170
return (PyUpb_GenericSequence*)obj;
167171
}
168172

@@ -357,7 +361,7 @@ typedef struct {
357361
} PyUpb_ByNameMap;
358362

359363
PyUpb_ByNameMap* PyUpb_ByNameMap_Self(PyObject* obj) {
360-
assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_name_map_type);
364+
CHECK_TYPE(obj, by_name_map_type);
361365
return (PyUpb_ByNameMap*)obj;
362366
}
363367

@@ -573,7 +577,7 @@ typedef struct {
573577
} PyUpb_ByNumberMap;
574578

575579
PyUpb_ByNumberMap* PyUpb_ByNumberMap_Self(PyObject* obj) {
576-
assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_number_map_type);
580+
CHECK_TYPE(obj, by_number_map_type);
577581
return (PyUpb_ByNumberMap*)obj;
578582
}
579583

0 commit comments

Comments
 (0)