You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WIP: stream: Implement cancellation support for uv_write_t
The ability to request cancellation of a pending or in-progress write
has been requested several times in libuv (at least joyent/libuv#1393 and
libuv#2051), but was not yet implemented. I am currently on a mission to
make Ctrl-C work nicely and reliably in Julia (JuliaLang/julia#60281).
Of course, this would require the ability to cancel in-progress writes
for sane semantics, so I have a renewed interest in this feature, which
this PR attempts to implement.
One primary problem with the API is that the existing callback does not
provide support for passing in the number of written bytes (since it
is expected to always complete). I see two options:
1. Create a new uv_write3 that takes a new cb type that does take this
argument, or,
2. Create a new public `uv_write_t` field that this information can be
read from.
This PR takes the first approach, but as it turns out the number of
bytes written needs to be stored anyway, so maybe there is not much
point to this choice.
As mentioned, we do need to store two extra bits of state (the total
number of bytes written and which kind of callback we have). To maintain
ABI compatibility we steal two pointers from the generic req_t reserve
pool. This preserves ABI compatibility, but is a bit awkward, because
it puts these new private fields into a different place than they would
otherwise be. I don't see that we have much choice though, other than
creating a completely new req_t subtype (which does not seem worth it).
The `uv_cancel` function returns `0` on success or `UV_EBUSY` if the
request was submitted using the `uv_write`/`uv_write2` API that does
not take the new callback signature (unless no bytes have been written,
in which case cancellation succeeds).
It also returns `0` if the request is already done. The thought here
was that it would be too racy to return an error code here, but I
think it would be fine to return EALREADY (although I don't know what
the caller would do with that information.
`uv_write3` also takes a flags parameter reserved for future use (just
in case).
The windows side is relatively straightforward in that the only thing
we really need to do is call `CancelIoEx` on the overlapped structure
that's already inside of our req. We do of course need the appropriate
accounting for the number of bytes written.
Discloure: Claude Code was used in the creation of this PR, although
dumb design decisions are probably by me.
I have done some minimal integrated testing of this on Linux.
On windows, I have run the included test, but have not tested
in-situ. I consider this WIP until I've had a chance to run our
full test suite on all platforms, but I wanted to make sure to
open this early for API feedback/concerns.
0 commit comments