Skip to content

Call SyscallConn() instead of Fd() to use file's fd #179

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

database64128
Copy link
Contributor

@database64128 database64128 commented Jul 24, 2025

os.File.SyscallConn is the safer alternative to os.File.Fd, without the pitfalls mentioned in Fd's doc comment.

The SyscallConn method is implemented on all platforms except Plan 9, where it returns syscall.EPLAN9, which matches errors.ErrUnsupported and goes to the fallback path.

[os.File.SyscallConn] is the safer alternative to [os.File.Fd], without the pitfalls mentioned in Fd's doc comment.

The SyscallConn method is implemented on all platforms except Plan 9, where it returns [syscall.EPLAN9], which matches [errors.ErrUnsupported] and goes to the fallback path.
@database64128 database64128 changed the title Call SyscallConn() instead of Fd() to use file's fd Call SyscallConn() instead of Fd() to use file's fd Jul 24, 2025
@oschwald
Copy link
Owner

Could you expand on the pitfalls that are applicable to this use case? The file will not be closed or finalized before the memory map is open and we are not holding on to it.

@database64128
Copy link
Contributor Author

Yes, in this use case it's safe to use Fd(). But SyscallConn is just strictly better than Fd in every aspect. It's like how the standard library is replacing the use of runtime.SetFinalizer with runtime.AddCleanup (golang/go#70907) after its introduction. The pitfalls of runtime.SetFinalizer may not apply to the existing uses, but it still makes sense to do the conversion.

Calling Fd() on an os.File has the side effect of putting the fd into blocking mode. This has potential future implications on our fallback path, when the Go runtime gains better support for async file IO. In Go 1.24 and earlier versions, os.File has no support for async file IO. Go 1.25 has undergone significant changes to support overlapped IO with os.File on Windows. Although they haven't made os.Open associate the fd with IOCP, they might do so in any future release, and then our use of Fd() would have caused the fd to be disassociated from IOCP (golang/go@8a8f506), which would cause our fallback path to tie up OS threads.

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