Skip to content

win,pipe: fix race with concurrent readers#4470

Merged
vtjnash merged 3 commits intolibuv:v1.xfrom
vtjnash:jn/read-pipe-async
Aug 2, 2024
Merged

win,pipe: fix race with concurrent readers#4470
vtjnash merged 3 commits intolibuv:v1.xfrom
vtjnash:jn/read-pipe-async

Conversation

@vtjnash
Copy link
Copy Markdown
Member

@vtjnash vtjnash commented Jul 25, 2024

This fixes a race condition if multiple threads are reading from the same NamedPipe, which could previously lead to a deadlock situation. We could also substantially improve performance now also, since the PeekFile call is unnecessary overhead with this change.

Related to #4467, though doesn't address any of the problems there. I believe that someone could now implement uv__pipe_try_write using exactly this same code pattern however.

The various *= 2; are to prove that this works and fixes the bug, but should be removed before merging. A small bit of additional code cleanup may be possible as well now?

vtjnash added 2 commits July 25, 2024 12:05
We could also substantially improve performance now also, since the PeekFile call is unnecessary overhead with this change.
…ct results in uv__pipe_read_exactly due to missing IOCP argument
@vtjnash
Copy link
Copy Markdown
Member Author

vtjnash commented Jul 25, 2024

Okay, I think this is finished now. The good thing is that this also seems to roughly double throughput performance

$ ./before/uv_run_benchmarks_a pipe_pump100_client
ok 1 - pipe_pump100_client
# pipe_pump100_server: 9.2 gbit/s
# pipe_pump100_client: 9.2 gbit/s

$ ./after/uv_run_benchmarks_a pipe_pump100_client
ok 1 - pipe_pump100_client
# pipe_pump100_server: 17.7 gbit/s
# pipe_pump100_client: 17.7 gbit/s

@vtjnash vtjnash marked this pull request as ready for review July 25, 2024 21:11
Copy link
Copy Markdown
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with an 85% confidence rate. :-)

Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
@vtjnash vtjnash merged commit 727ee72 into libuv:v1.x Aug 2, 2024
@vtjnash vtjnash deleted the jn/read-pipe-async branch August 2, 2024 14:51
vtjnash added a commit to vtjnash/libuv that referenced this pull request Aug 19, 2024
In libuv#4470, I accidentally copied the bug from unix, where calling
uv_stream_set_blocking can cause the whole process to hang on a read.
However, unlike unix, where libuv attempts to set the O_NONBLOCK flag in
uv_pipe_open (as long as the handle never gets passed to uv_spawn), the
NT kernel is not capable of enabling OVERLAPPED operation later (but
fortunately, it also cannot disable it later too).

This implementation might be good to copy to unix (using FIONREAD) to
address the same bug that happens there if the user has called uv_spawn
or uv_stream_set_non_blocking on this handle in the past.
vtjnash added a commit that referenced this pull request Aug 22, 2024
In #4470, I accidentally copied the bug from unix, where calling
uv_stream_set_blocking can cause the whole process to hang on a read.
However, unlike unix, where libuv attempts to set the O_NONBLOCK flag in
uv_pipe_open (as long as the handle never gets passed to uv_spawn), the
NT kernel is not capable of enabling OVERLAPPED operation later (but
fortunately, it also cannot disable it later too).

This implementation might be good to copy to unix (using FIONREAD) to
address the same bug that happens there if the user has called uv_spawn
or uv_stream_set_non_blocking on this handle in the past.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants