Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid closing invalid handles #119029

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

dylni
Copy link
Contributor

@dylni dylni commented Dec 16, 2023

Documentation for HandleOrInvalid has this note:

If holds a handle other than INVALID_HANDLE_VALUE, it will close the handle on drop.

Documentation for HandleOrNull has this note:

If this holds a non-null handle, it will close the handle on drop.

Currently, both will call CloseHandle on their invalid handles as a result of using OwnedHandle internally, contradicting the above paragraphs. This PR adds destructors that match the documentation.

@rustbot label A-io O-windows T-libs

@rustbot
Copy link
Collaborator

rustbot commented Dec 16, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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 Dec 16, 2023
@rust-log-analyzer

This comment has been minimized.

@dylni dylni force-pushed the avoid-closing-invalid-handles branch 3 times, most recently from ed08061 to 599b092 Compare December 18, 2023 00:48
@thomcc
Copy link
Member

thomcc commented Feb 1, 2024

I'm going to be away for a few months, so I'm rerolling my PRs so that folks don't have to wait for me. Sorry/thanks.

r? libs

@rustbot rustbot assigned m-ou-se and unassigned thomcc Feb 1, 2024
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

That's a very good catch! Though I think this could be written a bit simpler without ManuallyDrop.

@@ -91,7 +92,7 @@ pub struct OwnedHandle {
#[repr(transparent)]
#[stable(feature = "io_safety", since = "1.63.0")]
#[derive(Debug)]
pub struct HandleOrNull(OwnedHandle);
pub struct HandleOrNull(ManuallyDrop<OwnedHandle>);
Copy link
Member

Choose a reason for hiding this comment

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

I'd simply use RawHandle here, so that we don't have to do the ManuallyDrop::take dance below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Since it's not possible to move out the inner field with a custom Drop implementation, I was looking for a decent way to implement TryFrom. However, I can just forget the struct altogether.

#[stable(feature = "io_safety", since = "1.63.0")]
impl TryFrom<HandleOrNull> for OwnedHandle {
type Error = NullHandleError;
macro_rules! impl_handle_or {
Copy link
Member

Choose a reason for hiding this comment

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

Especially when combined with the suggestion above, I don't think that a macro is really necessary here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would likely still use a macro due to the shared code between the implementations, but I don't have strong opinions if the preference is to avoid macros here.

@m-ou-se m-ou-se assigned ChrisDenton and unassigned m-ou-se Feb 14, 2024
@ChrisDenton
Copy link
Member

Hi @dylni could you address the comments? Use @rustbot ready when you're done, thanks!

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 15, 2024
@dylni dylni force-pushed the avoid-closing-invalid-handles branch from 599b092 to 24556e0 Compare February 17, 2024 15:58
@dylni
Copy link
Contributor Author

dylni commented Feb 17, 2024

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 17, 2024
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

Looks great!

@ChrisDenton
Copy link
Member

Thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 19, 2024

📌 Commit 24556e0 has been approved by ChrisDenton

It is now in the queue for this repository.

@bors bors 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 Feb 19, 2024
}
}

#[stable(feature = "handle_or_drop", since = "1.75.0")]
Copy link
Contributor

@klensy klensy Feb 19, 2024

Choose a reason for hiding this comment

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

From where that 1.75? And next one.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, you're right. And this doesn't need a new feature as the fact it needs dropping isn't itself new.

@ChrisDenton
Copy link
Member

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 19, 2024
@dylni dylni force-pushed the avoid-closing-invalid-handles branch from 24556e0 to f3b5b08 Compare March 9, 2024 16:38
@dylni
Copy link
Contributor Author

dylni commented Mar 9, 2024

Updated and rebased on the latest master

diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs
index 5566f525ea4..30a809daa21 100644
--- a/library/std/src/os/windows/io/handle.rs
+++ b/library/std/src/os/windows/io/handle.rs
@@ -173,7 +173,7 @@ fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
     }
 }
 
-#[stable(feature = "handle_or_drop", since = "1.75.0")]
+#[stable(feature = "io_safety", since = "1.63.0")]
 impl Drop for HandleOrNull {
     #[inline]
     fn drop(&mut self) {
@@ -251,7 +251,7 @@ fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleErr
     }
 }
 
-#[stable(feature = "handle_or_drop", since = "1.75.0")]
+#[stable(feature = "io_safety", since = "1.63.0")]
 impl Drop for HandleOrInvalid {
     #[inline]
     fn drop(&mut self) {

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 9, 2024
@rust-log-analyzer

This comment has been minimized.

@dylni dylni force-pushed the avoid-closing-invalid-handles branch from f3b5b08 to a82587c Compare March 9, 2024 16:43
@ChrisDenton
Copy link
Member

Thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 14, 2024

📌 Commit a82587c has been approved by ChrisDenton

It is now in the queue for this repository.

@bors bors 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 Mar 14, 2024
jhpratt added a commit to jhpratt/rust that referenced this pull request Mar 14, 2024
…s, r=ChrisDenton

Avoid closing invalid handles

Documentation for [`HandleOrInvalid`] has this note:

> If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.

Documentation for [`HandleOrNull`] has this note:

> If this holds a non-null handle, it will close the handle on drop.

Currently, both will call `CloseHandle` on their invalid handles as a result of using `OwnedHandle` internally, contradicting the above paragraphs. This PR adds destructors that match the documentation.

`@rustbot` label A-io O-windows T-libs

[`HandleOrInvalid`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrInvalid.html
[`HandleOrNull`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrNull.html
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 14, 2024
…s, r=ChrisDenton

Avoid closing invalid handles

Documentation for [`HandleOrInvalid`] has this note:

> If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.

Documentation for [`HandleOrNull`] has this note:

> If this holds a non-null handle, it will close the handle on drop.

Currently, both will call `CloseHandle` on their invalid handles as a result of using `OwnedHandle` internally, contradicting the above paragraphs. This PR adds destructors that match the documentation.

``@rustbot`` label A-io O-windows T-libs

[`HandleOrInvalid`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrInvalid.html
[`HandleOrNull`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrNull.html
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#119029 (Avoid closing invalid handles)
 - rust-lang#122238 (Document some builtin impls in the next solver)
 - rust-lang#122247 (rustdoc-search: depth limit `T<U>` -> `U` unboxing)
 - rust-lang#122287 (add test ensuring simd codegen checks don't run when a static assertion failed)
 - rust-lang#122368 (chore: remove repetitive words)
 - rust-lang#122397 (Various cleanups around the const eval query providers)
 - rust-lang#122406 (Fix WF for `AsyncFnKindHelper` in new trait solver)
 - rust-lang#122477 (Change some attribute to only_local)
 - rust-lang#122482 (Ungate the `UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES` lint)
 - rust-lang#122490 (Update build instructions for OpenHarmony)

Failed merges:

 - rust-lang#122471 (preserve span when evaluating mir::ConstOperand)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 280a1da into rust-lang:master Mar 14, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2024
Rollup merge of rust-lang#119029 - dylni:avoid-closing-invalid-handles, r=ChrisDenton

Avoid closing invalid handles

Documentation for [`HandleOrInvalid`] has this note:

> If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.

Documentation for [`HandleOrNull`] has this note:

> If this holds a non-null handle, it will close the handle on drop.

Currently, both will call `CloseHandle` on their invalid handles as a result of using `OwnedHandle` internally, contradicting the above paragraphs. This PR adds destructors that match the documentation.

```@rustbot``` label A-io O-windows T-libs

[`HandleOrInvalid`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrInvalid.html
[`HandleOrNull`]: https://doc.rust-lang.org/std/os/windows/io/struct.HandleOrNull.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants