On Sun, Apr 26, 2020 at 4:55 PM Liam <networkimp...@gmail.com> wrote: > > During an io.Copy() where the Writer is a TCPConn and the Reader is a 200K > disk file, my code may concurrently Write() on the same TCPConn. > > I see the result of the Write() inserted into the result of the io.Copy(). I > had the impression that was impossible, but I must be mistaken, as the > sendfile(2) docs read: > > Note that a successful call to sendfile() may write fewer bytes than > requested; the caller should be prepared to retry the call if there were > unsent bytes. > > Could someone confirm that one must indeed synchronize concurrent use of > tcpConn.Write() and io.Copy(tcpConn, file)?
Synchronization should not be required. internal/poll.Sendfile acquires a write lock on dstFD, which is the TCP socket. That should ensure that the contents of an ordinary Write (which also acquires a write lock) should not interleave with the sendfile data. That said, if the sendfile system call cannot be used for whatever reason, the net package will fall back on doing ordinary Read and Write calls. And those Write calls can be interleaved with other Write calls done by a different goroutine. I think that is probably permitted, in that io.Copy doesn't promise to not interleave with simultaneous Write calls on the destination. So in the general case you should indeed use your own locking to avoid interleaving between io.Copy and a concurrent Write. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWtqsqjW_urobMMtM0w62Gtqi_7F%2Bn2UrtTD03Jp3Va9w%40mail.gmail.com.