You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@whitequark reports an interesting bug on avsm/ocaml-cohttp#57
If we ignore SIGPIPE, then writes to closed sockets will continue for some time, leading to memory leaks unless Lwt_unix.getsockopt_error is called after the write to ensure that it succeeded.
Fixing this using fds is fairly easy, but cohttp uses Lwt_io channels, and we cannot retrieve the fd from it. It would probably be best to fix this within Lwt_io itself though, by checking for socket errors after flushing a write.
The memory leak I was talking about wasn't in Cohttp or Lwt. Rather, my code assumed that the writer thread will be killed after the socket is closed, and only released resources after that.
I've looked through logs in my application and Lwt_io.write actually does raise Unix_error(EPIPE) sometimes. I think it happened after I wrote a much bigger than usual amount of data to it. I would think that Lwt_io.write raises if the write causes the buffer to flush, but why would it then not raise and change state of getsockopt_error? I'm honestly lost.
@diml I'm not sure. I think at the moment of write the kernel would already have received FIN from the other side. I'll make a better repro case to strace it and verify.
This explains why the first write succeeds. It also means that calling getsockopt_error right after the write will not work reliably, as a round-trip is necessary to know the connection status.
Activity
whitequark commentedon Dec 12, 2013
A few notes:
Unix_error(EPIPE)sometimes. I think it happened after I wrote a much bigger than usual amount of data to it. I would think that Lwt_io.write raises if the write causes the buffer to flush, but why would it then not raise and change state ofgetsockopt_error? I'm honestly lost.whitequark commentedon Dec 13, 2013
@diml I'm not sure. I think at the moment of write the kernel would already have received FIN from the other side. I'll make a better repro case to strace it and verify.
vouillon commentedon Dec 13, 2013
At the moment of the write, a FIN packet has indeed been received, but it is only an indication that there is no more data to be received. But we don't know yet whether the other side still accepts data or not (it is possible to close only half of the connection:
shutdown(sock, SHUT_WR)). For that, one needs to try to send some data to the other side, which then replies with a RESET packet. See http://stackoverflow.com/questions/11436013/writing-to-a-closed-local-tcp-socket-not-failing for details.This explains why the first
writesucceeds. It also means that callinggetsockopt_errorright after thewritewill not work reliably, as a round-trip is necessary to know the connection status.whitequark commentedon Dec 13, 2013
@vouillon Thanks for explanation. I now think this is not a problem for me:
I think this issue can be closed.