Are libuv handles active after close but before close_cb?

Viewed 243

libuv has a void uv_close(uv_handle_t* handle, uv_close_cb close_cb) method to close handles which takes a callback.

As the title says, is the handle active (in terms of I/O) before close_cb is called? For example, can a UDP handle fire a receive callback and a timer handle fire a timer callback before close_cb?

The closest thing in the documentation I could find is "Handles that wrap file descriptors are closed immediately but close_cb will still be deferred to the next iteration of the event loop." However, I'm not sure which handles fall into this criteria and more importantly, what "closed immediately" means exactly (stops all callbacks? stops only new callbacks? removed from the event loop entirely?).

1 Answers

The type of a libuv handle determines whether the handle would fire its callback even after un_close(handle) completed and before the uv_close_cb is invoked, so there's no general answer for that. For example uv_tcp_t and uv_udp_t may fire the callback again while many other types don't (see source).

All handles are no longer able to use again as soon as uv_close() cleared the flag UV_HANDLE_ACTIVE of the given handle and set the flag UV_HANDLE_CLOSING. You are NOT allowed to use the closing handle by invoking corresponding functions immediately after uv_close() , if you still do so, the corresponding functions may return error or give you assertion failure (e.g. uv_timer_start() and uv_poll_start())

uv_close() doesn't remove the handle from the event loop, instead it moves the closing handle to the internal list of the loop (only for later closing process), then you call uv_run() again, which will actually process all closing handles, depending on the type of closing handle, the closing handle fires its callback whenever necessary (e.g. uv__stream_destroy() in uv__finish_close()) , finally uv_run() invoke the callback uv_close_cb (see here)

Related