@@ -313,16 +313,23 @@ traverse_module_state(module_state *state, visitproc visit, void *arg)
313313static void
314314clear_xid_types (module_state * state )
315315{
316+ clear_xid_class_registry (& state -> xid_classes );
317+
318+ /* external types */
319+ if (state -> send_channel_type != NULL ) {
320+ (void )_PyCrossInterpreterData_UnregisterClass (state -> send_channel_type );
321+ Py_CLEAR (state -> send_channel_type );
322+ }
323+ if (state -> recv_channel_type != NULL ) {
324+ (void )_PyCrossInterpreterData_UnregisterClass (state -> recv_channel_type );
325+ Py_CLEAR (state -> recv_channel_type );
326+ }
327+
316328 /* heap types */
317329 if (state -> ChannelInfoType != NULL ) {
318330 (void )_PyCrossInterpreterData_UnregisterClass (state -> ChannelInfoType );
319331 Py_CLEAR (state -> ChannelInfoType );
320332 }
321-
322- /* external types */
323- clear_xid_class_registry (& state -> xid_classes );
324- Py_CLEAR (state -> send_channel_type );
325- Py_CLEAR (state -> recv_channel_type );
326333}
327334
328335static int
@@ -2703,7 +2710,6 @@ set_channelend_types(PyObject *mod, PyTypeObject *send, PyTypeObject *recv)
27032710 if (state == NULL ) {
27042711 return -1 ;
27052712 }
2706- struct xid_class_registry * xid_classes = & state -> xid_classes ;
27072713
27082714 if (state -> send_channel_type != NULL
27092715 || state -> recv_channel_type != NULL )
@@ -2713,11 +2719,15 @@ set_channelend_types(PyObject *mod, PyTypeObject *send, PyTypeObject *recv)
27132719 }
27142720 state -> send_channel_type = (PyTypeObject * )Py_NewRef (send );
27152721 state -> recv_channel_type = (PyTypeObject * )Py_NewRef (recv );
2716-
2717- if (register_xid_class (send , _channelend_shared , xid_classes )) {
2722+ if (ensure_xid_class (send , _channelend_shared ) < 0 ) {
2723+ Py_CLEAR (state -> send_channel_type );
2724+ Py_CLEAR (state -> recv_channel_type );
27182725 return -1 ;
27192726 }
2720- if (register_xid_class (recv , _channelend_shared , xid_classes )) {
2727+ if (ensure_xid_class (recv , _channelend_shared ) < 0 ) {
2728+ (void )_PyCrossInterpreterData_UnregisterClass (state -> send_channel_type );
2729+ Py_CLEAR (state -> send_channel_type );
2730+ Py_CLEAR (state -> recv_channel_type );
27212731 return -1 ;
27222732 }
27232733
0 commit comments