Skip to content

Use OwnedFd/OwnedSocket in Socket #600

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

Merged
merged 3 commits into from
Jul 4, 2025
Merged

Use OwnedFd/OwnedSocket in Socket #600

merged 3 commits into from
Jul 4, 2025

Conversation

Thomasdezeeuw
Copy link
Collaborator

Instead of the workaround where we use std::net::TcpStream.

Unfortunately Windows uses std::os::windows::io::RawSocket (u64), which
is different from the RawSocket that we define that uses
windows_sys::Win32::Networking::WinSock::SOCKET (usize). So the std lib
version gets ranamed to StdRawSocket.
// something we don't want. So check for that we have this
// `assert!`.
#[cfg(unix)]
assert!(raw >= 0, "tried to create a `Socket` with an invalid fd");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like there is a test for this assertion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the test with the error message from std lib, don't think there is value is checking it twice.

Instead of the workaround where we use std::net::TcpStream.
@Thomasdezeeuw
Copy link
Collaborator Author

Panic message is different in 1.70 vs. current stable 🤦 I'll just change it to only check for the panic

It changed between 1.70 and current stable. Checking if the function
panics should be good enough.
@Thomasdezeeuw Thomasdezeeuw merged commit 39d3e34 into master Jul 4, 2025
47 checks passed
@Thomasdezeeuw Thomasdezeeuw deleted the ownedfd branch July 4, 2025 13:23
Comment on lines +90 to +91
// potentially close a fd it doesn't own. All of that isn't memory
// unsafe, so it's not desired but never memory unsafe or causes UB.
Copy link
Member

Choose a reason for hiding this comment

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

It's not immediate UB, but we consider it library UB because downstream effects can cause memory unsafety, e.g. by incorrectly touching mmap'ed fds.

https://doc.rust-lang.org/std/io/index.html#io-safety

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.

3 participants