-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: os: support disposing of File without closing its fd #24215
Comments
Barring any negative feedback on this proposal in the upcoming weeks, I plan to tackle this further with an implementation+tests (to scratch my own itch). |
@lucab have you tried runtime.KeepAlive for this use case? Yours seems quite similar to the documented example. |
@artyom yes, I'm aware of Keepalive but no this isn't the same usecase: in the snippet above, |
I think you can use a duplicated fd in |
@crvv thanks for your feedback! Yes, dup-ing the fd was also considered before proposing this, but it is merely a resource-heavy workaround for the borrowing case. |
This seems very very niche. Why not just assign the file to a map of things you don't want to close? It's hard to believe this comes up often enough to justify the addition of new methods in the library itself. FWIW I've used systemd sockets too and the programs seem to work fine just getting them once at the start of the program and holding onto them, instead of picking up and dropping the fd repeatedly. |
@rsc I understand that my example may look very niche, and that with custom applications it is easy to workaround by performing additional manual book-keeping (as all workarounds above show).
Of course I understand stdlib design and maintenance is a nuanced topic, so I'm just trying to clarify what may look like an odd proposal. |
This still seems too niche for the Go standard library. Go does provide the golang.org/x/sys/unix package if you want to do all your file descriptor management yourself. |
What version of Go are you using (
go version
)?Go 1.10
Summary
os.File
can wrap existing file-descriptors (fd), viaNewFile()
, in a file object. As with allFile
objects, this automatically closes the fd at GC time (i.e. via a private finalizer). This works well when taking ownership of an existing fd, but makes temporary borrowing of an fd impossible/racey: any furtherNewfile
usage may or may not work, depending on relative timing with the previous finalizer.This is a proposal for an explicit method to signal the end of fd borrowing, without closing it on GC. This is to model the non-ownership case.
Usecase
As a practical use-case, let's pick a Go server which is socket activated by systemd.
In this case, the fd is passed (and indexed) by systemd to the child Go server, and any
File
object can temporarily borrow it viaNewFile
wrapping:At the end of this snippet, users may want re-use the external
fd
but doing so is unsafe in a subtle way: as soon as the File object forfp
is not referenced anymore, GC can decide to run the internal finalizer at any point, and thenfd
will suddenly be closed.Note: as per public API doc,
fp
andlistener
lifespans are not strictly coupled.Proposal
To avoid racing with the finalizer, introduce a method to signal the end of a fd-borrowing span.
Name and signature could be as follow:
Semantics would be as follow:
File.file
)/cc @squeed
The text was updated successfully, but these errors were encountered: