Skip to content

Conversation

@noahnut
Copy link

@noahnut noahnut commented Sep 17, 2025

Summary

This PR updates SocketHolder::as_tcp_listener and SocketHolder::as_unix_listener to use try_clone() instead of constructing listeners with from_raw_fd(as_raw_fd()).

Motivation

Using TcpListener::from_raw_fd(self.socket.as_raw_fd()) (or the equivalent for UnixListener) takes ownership of the same raw file descriptor that self.socket already owns.
This can lead to double-closing the same FD once both objects are dropped, causing runtime aborts such as:

fatal runtime error: IO Safety violation: owned file descriptor already closed

Benefits

Memory/FD safety: each listener now owns a duplicated file descriptor, preventing double-close issues.

Eliminates unsafe: removes the need for unsafe blocks.

Minimal overhead: dup() system call cost is negligible compared to network I/O.

Notes

This change only affects FD ownership semantics. Runtime behavior of the listeners remains the same, except that it avoids UB/crashes when objects are dropped in different orders.

Related to #681

Replaced unsafe raw file descriptor usage with safe cloning of the socket for both TCP and Unix listeners. This change enhances safety and maintains functionality.
Simplified the creation of TCP and Unix listeners by directly returning the result of the socket cloning operation, enhancing code clarity and maintainability.
@gi0baro
Copy link
Member

gi0baro commented Nov 16, 2025

Thanks for your contribution. This is now superseded by #746

@gi0baro gi0baro closed this Nov 16, 2025
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