diff --git a/source/http_stream.c b/source/http_stream.c index cfd6223eb..6843e0ea8 100644 --- a/source/http_stream.c +++ b/source/http_stream.c @@ -204,8 +204,9 @@ static void s_on_stream_complete(struct aws_http_stream *native_stream, int erro /* DECREF python self, we don't need to force it to stay alive any longer. */ PyObject *self = aws_py_weakref_get_ref(stream->self_proxy); + /* DECREF twice because `aws_py_weakref_get_ref` returns a strong reference */ + Py_XDECREF(self); Py_XDECREF(self); - aws_py_weakref_release_ref(self); PyGILState_Release(state); /*************** GIL RELEASE ***************/ diff --git a/source/module.c b/source/module.c index dc38c30cf..23d77cda9 100644 --- a/source/module.c +++ b/source/module.c @@ -525,18 +525,11 @@ PyObject *aws_py_weakref_get_ref(PyObject *object) { #else /* PyWeakref_GetObject is deprecated since python 3.13 */ self = PyWeakref_GetObject(object); /* borrowed reference */ + Py_XINCREF(self); #endif return self; } -void aws_py_weakref_release_ref(PyObject *object) { - (void)object; - /* Python versions before 3.13 returns a borrowed reference */ -#if PY_VERSION_HEX >= 0x030D0000 - Py_XDECREF(object); -#endif -} - int aws_py_gilstate_ensure(PyGILState_STATE *out_state) { if (AWS_LIKELY(Py_IsInitialized())) { *out_state = PyGILState_Ensure(); diff --git a/source/module.h b/source/module.h index b2cfc8a85..7777ab8bc 100644 --- a/source/module.h +++ b/source/module.h @@ -107,10 +107,8 @@ PyObject *aws_py_get_error_message(PyObject *self, PyObject *args); PyObject *aws_py_memory_view_from_byte_buffer(struct aws_byte_buf *buf); /* Python 3.13+ changed the function to get a reference from WeakRef. This function is an abstraction over two different - * APIs since we support Python versions before 3.13.*/ + * APIs since we support Python versions before 3.13. Returns a strong reference if non-null, which you must release. */ PyObject *aws_py_weakref_get_ref(PyObject *object); -/* Release the weakwef provided by the `aws_py_weakref_get_ref`. */ -void aws_py_weakref_release_ref(PyObject *object); /* Allocator that calls into PyObject_[Malloc|Free|Realloc] */ struct aws_allocator *aws_py_get_allocator(void); diff --git a/source/mqtt_client_connection.c b/source/mqtt_client_connection.c index 668d6eed0..fe629a959 100644 --- a/source/mqtt_client_connection.c +++ b/source/mqtt_client_connection.c @@ -151,7 +151,7 @@ static void s_on_connection_success( } } - aws_py_weakref_release_ref(self); + Py_XDECREF(self); PyGILState_Release(state); } @@ -178,7 +178,7 @@ static void s_on_connection_failure(struct aws_mqtt_client_connection *connectio } } - aws_py_weakref_release_ref(self); + Py_XDECREF(self); PyGILState_Release(state); } @@ -206,7 +206,7 @@ static void s_on_connection_interrupted(struct aws_mqtt_client_connection *conne } } - aws_py_weakref_release_ref(self); + Py_XDECREF(self); PyGILState_Release(state); } @@ -240,7 +240,7 @@ static void s_on_connection_resumed( PyErr_WriteUnraisable(PyErr_Occurred()); } } - aws_py_weakref_release_ref(self); + Py_XDECREF(self); PyGILState_Release(state); } @@ -271,7 +271,7 @@ static void s_on_connection_closed( } } - aws_py_weakref_release_ref(self); + Py_XDECREF(self); PyGILState_Release(state); } @@ -598,7 +598,7 @@ static void s_ws_handshake_transform( done:; /* Save off error code, so it doesn't got stomped before we pass it to callback*/ int error_code = aws_last_error(); - aws_py_weakref_release_ref(connection_py); + Py_XDECREF(connection_py); if (ws_transform_capsule) { Py_DECREF(ws_transform_capsule);