joyent / libuv

Go to
https://github.com/libuv/libuv
3.27k stars 653 forks source link

Make it possible to immediately close a handle in a callback. #1562

Closed rainbow-alex closed 10 years ago

rainbow-alex commented 10 years ago

I am using libuv in combination with coroutines. What I am trying to do is:

  1. In some coroutine, I put a one-off handle on the stack, and start it. I then yield control to the scheduler coroutine.
  2. The scheduler coroutine loops and (eventually) my handle's event occurs. The callback simply switches back to coroutine A.
  3. Coroutine A closes the handle.

The problem with the last step is the closing happens asynchronously. By the time the loop gets to closing my handle, the allocation is invalid. I suppose I could work around this by allocating the handle on the heap and using the close callback to free it, but it would be nice if I could just uv_close_sync( &my_handle );. Intuitively it seems feasible.

saghul commented 10 years ago

This is by design and even if intuitively feels possible it's not really the case. Specifically on Windows some close operations take time. Also this makes the interface more consistent across libuv.

You'll have to allocate the handle on the heap and free it on the close callback.