bad file descriptor
pmazzini opened this issue · 1 comments
pmazzini commented
I've been seeing bad file descriptor
error message sometimes when trying to read an incoming packet.
Problem:
- The
responder
creates anet.UDPConn
via net.ListenUDP - Afterwards, it calls connFd to get a file descriptor which uses the File method of the
net.UDPConn
. - The
File
method creates a duplicate:
File returns a copy of the underlying os.File. It is the caller's responsibility to close f when finished.
- The
Close
method is never explicitly called and the GC takes care of it - This leads to the mentioned error message, trying to read from a closed fd
Solution:
There are at least 2 alternatives and I wanted to ask for opinions.
-
Avoid creating a duplicate fd to read packets: This implies not using
net.ListenUDP
, (which doesn't expose the "original" fd) and using a syscall directly to create the listening socket. -
Explicitly close the duplicate file descriptor to avoid the GC closing it for us at an undesired time: This implies removing/modifying the
connFd
function.
Any other alternatives? Thoughts?
leoleovich commented
Explicitly close please ;)