Skip to content

socket: change accept to return an OwnedFd#2780

Merged
SteveLauC merged 2 commits into
nix-rust:masterfrom
cgzones:accept
May 13, 2026
Merged

socket: change accept to return an OwnedFd#2780
SteveLauC merged 2 commits into
nix-rust:masterfrom
cgzones:accept

Conversation

@cgzones
Copy link
Copy Markdown
Contributor

@cgzones cgzones commented Apr 20, 2026

accept(2) returns on success a new file descriptor that is not coupled with the listened socket. Thus it is safe to wrap it into an OwnedFd, which will close the socket on cleanup.

What does this PR do

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

@cgzones cgzones force-pushed the accept branch 2 times, most recently from 8dd280c to c667ee6 Compare April 20, 2026 23:09
Comment thread CHANGELOG.md Outdated
Comment thread CHANGELOG.md
Copy link
Copy Markdown
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the patch!

I think we could take this a step further and make the sockfd argument I/O-safe as well:

fn accept<Fd: AsFd>(sockfd: Fd) -> Result<OwnedFd>

Are you interested in doing it in this PR

@SteveLauC
Copy link
Copy Markdown
Member

CI failures are unrelated and fixed, you can rebase your branch.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates nix::sys::socket::accept to return an owning file-descriptor type (OwnedFd) rather than a RawFd, aligning the API with Rust I/O-safety patterns and avoiding call-site unsafe wrappers.

Changes:

  • Change sys::socket::accept to return Result<OwnedFd> and wrap the returned fd in OwnedFd.
  • Update socket-related tests to use the returned OwnedFd directly (removing BorrowedFd::borrow_raw workarounds).
  • Add a changelog entry documenting the API change.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/sys/socket/mod.rs Changes accept to return OwnedFd and wraps the returned fd.
test/sys/test_socket.rs Removes raw-borrow workarounds and uses &OwnedFd with read/fcntl.
test/sys/test_sockopt.rs Adjusts imports and removes an unnecessary manual OwnedFd::from_raw_fd wrapping after accept.
changelog/2780.changed.md Documents the public API change for accept.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/sys/test_socket.rs
Comment thread src/sys/socket/mod.rs
accept(2) returns on success a new file descriptor that is not coupled
with the listened socket. Thus it is safe to wrap it into an OwnedFd,
which will close the socket on cleanup.
@cgzones
Copy link
Copy Markdown
Contributor Author

cgzones commented May 11, 2026

changed accept4 as well

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

Comment thread src/sys/socket/mod.rs
Comment thread src/sys/socket/mod.rs
Comment thread test/sys/test_socket.rs Outdated
Comment thread test/sys/test_socket.rs Outdated
Comment thread test/sys/test_socket.rs Outdated
Comment thread test/sys/test_sockopt.rs Outdated
Comment thread test/sys/test_sockopt.rs Outdated
Copy link
Copy Markdown
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

Thanks!

@SteveLauC SteveLauC added this pull request to the merge queue May 13, 2026
Merged via the queue into nix-rust:master with commit 759c65c May 13, 2026
46 checks passed
@cgzones cgzones deleted the accept branch May 13, 2026 10:46
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.

6 participants