Fix file descriptor being closed twice

This was leading to us closing fd that was in use by other routine and
thus causing problems in singlepoll code that should never have same fd
parked twice.

The cause was following:
If FileConn failed due to lack of free fd's for Dup operation we were
calling Close syscall to close the fd. This fd was previously passed to
os.NewFile which causes Golang to add finalizer for closing that fd.

If memory, file descriptor and connection pressure were right, Golang was
closing fd that was being parked on, which in turns meant that we could
get the same fd in the dial code, which would be parked again causing
the singlepoller to enter undefined state.
parent 35e1e8c5
......@@ -163,14 +163,15 @@ func dial(ctx context.Context, dialer net.Dialer, netw, addr string) (c net.Conn
}
}
// NOTE:XXX: never call syscall.Close on fd after os.NewFile
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
fd = -1 // so we don't touch it, we handled the control to Golang with NewFile
if c, err = net.FileConn(file); err != nil {
syscall.Close(fd)
_ = file.Close() // shouldn't error either way
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
c.Close()
return nil, err
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment