@@ -3267,7 +3267,7 @@ ERL_NIF_TERM nif_reactor_init_connection(ErlNifEnv *env, int argc,
32673267}
32683268
32693269/**
3270- * reactor_close_fd(FdRef) -> ok | {error, Reason}
3270+ * reactor_close_fd(ContextRef, FdRef) -> ok | {error, Reason}
32713271 *
32723272 * Close an FD and clean up the protocol handler.
32733273 * Calls Python's erlang_reactor.close_connection(fd) if registered.
@@ -3276,8 +3276,13 @@ ERL_NIF_TERM nif_reactor_close_fd(ErlNifEnv *env, int argc,
32763276 const ERL_NIF_TERM argv []) {
32773277 (void )argc ;
32783278
3279+ py_context_t * ctx ;
3280+ if (!enif_get_resource (env , argv [0 ], PY_CONTEXT_RESOURCE_TYPE , (void * * )& ctx )) {
3281+ return make_error (env , "invalid_context" );
3282+ }
3283+
32793284 fd_resource_t * fd_res ;
3280- if (!enif_get_resource (env , argv [0 ], FD_RESOURCE_TYPE , (void * * )& fd_res )) {
3285+ if (!enif_get_resource (env , argv [1 ], FD_RESOURCE_TYPE , (void * * )& fd_res )) {
32813286 return make_error (env , "invalid_fd_ref" );
32823287 }
32833288
@@ -3293,20 +3298,21 @@ ERL_NIF_TERM nif_reactor_close_fd(ErlNifEnv *env, int argc,
32933298
32943299 /* Call Python to clean up protocol handler */
32953300 if (fd >= 0 ) {
3296- gil_guard_t guard = gil_acquire ( );
3297-
3298- PyObject * reactor_module = PyImport_ImportModule ("erlang.reactor" );
3299- if (reactor_module != NULL ) {
3300- PyObject * result = PyObject_CallMethod (reactor_module ,
3301- "close_connection" , "i" , fd );
3302- Py_XDECREF (result );
3303- Py_DECREF (reactor_module );
3304- PyErr_Clear (); /* Ignore errors during cleanup */
3305- } else {
3306- PyErr_Clear ();
3307- }
3301+ py_context_guard_t guard = py_context_acquire ( ctx );
3302+ if ( guard . acquired ) {
3303+ PyObject * reactor_module = PyImport_ImportModule ("erlang.reactor" );
3304+ if (reactor_module != NULL ) {
3305+ PyObject * result = PyObject_CallMethod (reactor_module ,
3306+ "close_connection" , "i" , fd );
3307+ Py_XDECREF (result );
3308+ Py_DECREF (reactor_module );
3309+ PyErr_Clear (); /* Ignore errors during cleanup */
3310+ } else {
3311+ PyErr_Clear ();
3312+ }
33083313
3309- gil_release (guard );
3314+ py_context_release (& guard );
3315+ }
33103316 }
33113317
33123318 /* Take ownership for cleanup */
0 commit comments