-
Notifications
You must be signed in to change notification settings - Fork 18k
os, internal/poll: fd.Read with read deadline on fifo before writer has opened returns EOF prematurely #44176
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
Comments
The above strace log show that |
I tried to reproduce your comment by copygin my play.golang.org example above into I see:
at the point it hangs on But the result is the same -- if the read did not error (strace shows it didn't return -1) then the fd is not closed, and thus the higher level implementation should not be returning |
CC @ianlancetaylor, @bradfitz. |
What sort of help do you need? :-) Would you like a change that implements what I suggested, or are you looking for an expert third party to approve of the proposed change first? |
A change that implements the suggestion, along with a test, would be fine. Thanks. |
I've drafted a test based on my playground code above, and so far have managed to figure out that this is going to be harder than I thought. |
The test was correct, but due to golang/go#44176 we must make sure the writer end is open first. Issue #486
I'm stuck again, confusing myself in the depths of internal/poll and now runtime/netpoll.go I'm reminded that golang/go/20280 is the reason I'm exploring this path. I have a fifo that I want to read from until either EOF or a context is cancelled. In order to notice that the context is cancelled, I use SetReadDeadline on the fifo, hence the strange construct in the playground example. The earlier statements about expecting EAGAIN on read was incorrect, because Go runtime is using epoll in the runtime, so we should expect to see EPOLLHUP on the reader while the write end is still closed. I think the EPOLLHUP gets eaten in (For the benefit of the tape, the best way to debug this that I've found is after having built a local go fresh from Git ( Meanwhile I've noticed #20280 (comment) which I think is a better approach than setting a read deadline every time just to test for cancellation, so while I think there's still a bug here, I think I am no longer dependent on it. |
Could you open fifo in blocking mode, that should avoid the race? |
The problem I have is nearly identical to #20280, where one does not want to open in blocking mode because one wants a context cancellation to interrupt the read. The suggestions from that bug report have indeed helped -- the magic is knowing you don't need to set a read deadline on the read before you start the read. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Create a fifo with os.Mkfifo, then open the reader in nonblocking, RDONLY mode. Start a goroutine to read the pipe, with a SetReadDeadline,. in a loop until EOF is received.
Open the writer end and write a single line, then close the file.
A race condition exists in such code where the read is scheduled before the write, and in just the right conditions, the reader returns 0 bytes read and EOF before the write succeeds.
https://play.golang.org/p/cZ9A_FCur43 has a demonstration that uses wg2 to force the race condition. With the race disabled (the race is still there, just harder to trigger) the writer opens the file before the read goroutine is scheduled and the reader gets all the data.
In the errant case (race enabled), the reader gets scheduled first. The write end of the pipe has not been opened yet, and according to
man pipe(7)
andread(2)
the reader should get an EAGAIN, not EOF. EOF is to signal to the reader that the writer has closed the pipe.What did you expect to see?
When reading from a nonblocking named pipe on Linux before the writer has opened the pipe, one expects to get an EAGAIN error. https://man7.org/linux/man-pages/man2/read.2.html
I think that the correct behaviour for Go, having handled
EAGAIN
, is to return n = 0, err = nil from fd.Read when a read deadline is set, which would align withpipe(7)
in as much as EOF remains reserved as a signal that the writer has closed the pipe.Otherwise (as is presently the case) there is no way to correctly shut down a pipe reader and handle this race correctly.
What did you see instead?
The reader gets EOF too early. This looks like a problem with how
fd.eofError()
ininternal/poll/fd_posix.go
is implemented, and/or how ZeroReadIsEOF is set for nonblocking reads inos/file_unix.go
.The text was updated successfully, but these errors were encountered: