Windows: Fallback for overlapped I/O by ChrisDenton · Pull Request #98950 · rust...
source link: https://github.com/rust-lang/rust/pull/98950
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
added O-windows Operating system: Windows beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. T-libs Relevant to the library team, which will review and decide on the PR/issue. A-io Area: std::io, std::fs, std::net and std::path
labels
Collaborator
rustbot commented 14 days ago
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment has been hidden.
added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label
This comment has been hidden.
Member
wesleywiser commented 14 days ago
cc @jstarks in case you want to take a look as well |
jstarks commented 14 days ago
jstarks commented 14 days ago
Might be good to add a test, perhaps by creating (and connecting to) an overlapped named pipe and reading from it. |
Contributor
Author
ChrisDenton commented 14 days ago
I added a test (note that the |
jstarks commented 14 days ago
Thanks. On a side note, I am trying to get NtCreateNamedPipeFile documented, which would allow you to create an anonymous pipe pair with the properties you want without the retry dance. I'll let you know when this happens. |
Contributor
Author
ChrisDenton commented 14 days ago
Contributor
Author
ChrisDenton commented 14 days ago
Ok, from what I can tell via observer behaviour, However, with this information I can use |
added beta-accepted Accepted for backporting to the compiler in the beta channel. stable-accepted Accepted for backporting to the compiler in the stable channel.
labels
So... this is pretty subtle. I think it's ultimately fine, but it took a lot of discussion with @ChrisDenton on Discord to convince me (but to be clear: I am on board with it now).
That said, for future reference, this is usually not how you're supposed to do it. For example, the documentation for GetOverlappedResult
states:
Use of file, named pipe, or communications-device handles for this purpose is discouraged.
My understanding is the "right" way to solve this is to first call GetOverlappedResult
with bWait == FALSE
, to reset the event, and then call GetOverlappedResult
with bWait == TRUE
to actually wait. Failing to do this would result in it not actually waiting, which is what was observed when we used a single GetOverlappedResult(TRUE)
call rather than WaitForSingleObject
. I believe this was also required prior to Windows 7, and in many cases it is still true, such as when a manual-reset event is used in the OVERLAPPED
(although my experience has certainly been that you still sometimes need to do this even if you don't!)
In most cases, I still think that is how it should be handled (notably it is sometimes required, but correct and harmless even in the case where you don't need to go through with it). However, in this case, I think it would actually be pretty weird, and we're justified:
- We don't actually have an event, we're using the one in the file handle (well, that would often be true either way). At least in theory this means we should not need to do the double GOR dance.
- We're also not using
ReadFile
, we're usingNtReadFile
, and as a result, we don't even have anOVERLAPPED
in the first place. We would need to fabricate one with the state we expect it to have afterReadFile
which returned pending. This is a bit of an abstraction violation, to say the least. - We don't need any of the other information provided by
GetOverlappedResult
. What we need it to do is wait, so we should just perform the wait.
Anyway, that's all to say I think this is fine. @ChrisDenton has asked me to wait on the R+ for a bit (for a response from @jstarks), so I'll give that a day or so before letting bors know
jstarks commented 13 days ago
Wow, I did not expect that wait behavior out of The new approach is a good one, both for this reason and to avoid the abstraction violation. Thanks @ChrisDenton and @thomcc. |
Contributor
Author
ChrisDenton commented 13 days ago
Yeah, I find that really surprising. At first I was convinced I'd made a mistake somewhere. I guess it really is intended to be used with a dedicated event object. |
Member
thomcc commented 11 days ago
Alright, let's go! @bors r+ |
Contributor
bors commented 11 days ago
added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
@bors p=1 since we're targeting beta backport |
Contributor
bors commented 10 days ago
Contributor
bors commented 10 days ago
Test successful - checks-actions |
Collaborator
rust-timer commented 10 days ago
Finished benchmarking commit (17355a3): comparison url. Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results CyclesThis benchmark run did not return any relevant results for this metric. If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes
|
Member
cuviper commented 4 days ago
The documentation on |
Personally I think we should still warn against using files opened for async I/O here. This PR may help mitigate the issue if users are careful (esp. with regard to threads) but these methods are still not intended for async I/O. I guess the "must" language could be made slightly weaker. But this PR is purely a "best effort" attempt at a workaround in that case so I do feel we should still strongly suggest people use other methods for async I/O. |
removed the stable-nominated Nominated for backporting to the compiler in the stable channel. label
removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Successfully merging this pull request may close these issues.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK