Skip to content

retrieve an fd from an Lwt_io channel #27

Closed

Description

@avsm

@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.

See gist for repro case: https://gist.github.com/whitequark/5b4f7b026e96394042b1

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.

Activity

  1. whitequark commented on Dec 12, 2013

    @whitequark
    Contributor

    A few notes:

    • 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.
  2. whitequark commented on Dec 13, 2013

    @whitequark
    Contributor

    @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.

  3. vouillon commented on Dec 13, 2013

    @vouillon
    Member

    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 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.

  4. whitequark commented on Dec 13, 2013

    @whitequark
    Contributor

    @vouillon Thanks for explanation. I now think this is not a problem for me:

    • If a connection is not actually used, I will clean it up after a timeout.
    • If a connection is indeed used, Unix_error will be raised after two flushes of Lwt_io channel buffer. This is consistent with my observations.

    I think this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions