Skip to content

kill-connection-w/o-timer! can take way too long #71

Closed

Description

@samth

When the initial-connection-timeout expires, the web server closes some ports and shuts down a custodian. However, this can take a surprisingly long time. In particular, the calls to close-{input,output}-port can add 500+ms of latency! It's not clear to me why this happens, but maybe there's something else we can do here.

To reproduce, see https://github.com/zkry/racket-test-server and note the spikes in the resulting picture. They correspond exactly to this timer, and if you change the initial timeout to 2, they happen every 2 seconds. If you comment out the two with-handlers forms in kill-connection-w/o-timer!, the spikes go away.

Activity

  1. samth commented on Sep 13, 2019

    @samth
    SponsorMemberAuthor
  2. Bogdanp commented on Sep 13, 2019

    @Bogdanp
    Contributor

    I'm surprised the calls to close-{input,output}-port are necessary. The documentation for run-server states that the connections are managed by a new custodian so shouldn't custodian-shutdown-all also close the ports in that case?

    I tried connecting to the server via telnet, making a request and then waiting for the timeout. When the lines are commented out, nothing happens and telnet hangs. When they're left in, telnet receives the close packets.

    I don't know if this is related to why those calls take so long, but I figured it was worth mentioning.

  3. samth commented on Sep 13, 2019

    @samth
    SponsorMemberAuthor

    As far as I can tell, time always reports 0 time for those calls, and the error handlers are never called in the benchmark. So I'm very confused.

  4. jeapostrophe commented on Sep 13, 2019

    @jeapostrophe
    Contributor

    My guess is that you are observing the time to flush the buffers.

  5. samth commented on Sep 13, 2019

    @samth
    SponsorMemberAuthor

    But which buffers are being flushed? And why does it impact other requests (which are presumably in other threads)?

  6. jeapostrophe commented on Sep 13, 2019

    @jeapostrophe
    Contributor

    I assume it is the connection buffers? That could be why telnet hangs with them gone. There could be a blocking call somewhere in the kernel during closing as some special case?

  7. Bogdanp commented on Sep 14, 2019

    @Bogdanp
    Contributor

    I investigated a little further and I don't think the problem has to do with buffering.

    The reason telnet hangs when I replace the body of kill-connection-w/o-timer! with (void) is because that makes it so that the input port is never closed. Because it is never closed, the connection-loop inside handle-connection/cm never exits (because there's no input coming in and because the port-close-evt on the input port never fires (because it is never closed)). That makes sense, although I'm still surprised that the connection-loop-ing thread isn't killed when its custodian gets shut down. (explained in "edit 2")

    However, when the unmodified kill-connection-w/o-timer! is triggered, it closes the two ports and then shuts down the custodian, which triggers the port-close-evt inside of handle-connection/cm, which is running inside a thread belonging to the custodian being shut down, which itself calls kill-connection! again, attempting to close the ports again and then shutting down the custodian again. The connection-loop then terminates and the connection handling function exits, which signals to run-server that the custodian should be shut down... again.

    When I modify kill-connection-w/o-timer! so that all it does is close the input port, the spikes go away and the connection-closing after a timeout behavior is preserved, which leads me to think that the issue is related to calling custodian-shutdown-all from within a thread that is managed by the custodian that is being asked to shut down. Either that or there's something else going on within run-server. I don't know enough about the runtime yet to know for sure, but does that sound plausible? (see "edit 2")

    You can test this yourselves by changing kill-connection-w/o-timer! to:

    (define (kill-connection-w/o-timer! conn)
      (with-handlers ([exn:fail:network? void])
        (close-input-port (connection-i-port conn))))

    Thought: is it possible the connection-handling thread is owned by another custodian other than the connection's custodian? That would explain why calling custodian-shutdown-all by itself doesn't stop the connection thread. (see "edit 2")


    Edit: here's what the plot looks like after I made that change:

    Screenshot 2019-09-14 at 10 01 25


    Edit 2: I dove in a little further this afternoon.

    Code within the connection-handling function is parameterized with a child custodian of the one that manages the thread and the connection. This explains why shutting down the custodian doesn't shut down the connection-handling thread.

    The call that causes the latency to spike is not custodian-shutdown-all, but the one to close-output-port. This would point to buffers that need flushing as the culprit, as @jeapostrophe suggested. However, when I explicitly flush the output port, but omit the call to close-output-port, the latency spikes don't happen. So, this code

    (define (kill-connection-w/o-timer! conn)
      (with-handlers ([exn:fail:network? void])
        (flush-output (connection-o-port conn))
        #;(close-output-port (connection-o-port conn)))
      (with-handlers ([exn:fail:network? void])
        (close-input-port (connection-i-port conn)))
      (custodian-shutdown-all (connection-custodian conn)))

    is not affected by the latency spikes. Uncommenting the close-output-port call brings back the spikes, regardless of whether or not I leave the flush-output call in. Closing the input port before the output port has no effect.

    Questions that I have (and I might dive in deeper tomorrow to see if I can answer them):

    • Why does close-output-port take so long under these circumstances? I can see that fd-close is an "atomic" operation (not 100% on what that means in rktio yet) and that it needs to take a semaphore in order to operate. Could too much contention (many fds being closed all at once) over that semaphore be the culprit?
    • Why doesn't custodian-shutdown-all (the call made by run-server) take just as long, given that it supposedly closes all the TCP ports that belong to it?
    • Would it be wrong to change kill-connection-w/o-timer! such that it only closes the input port? That would leave reaping the output port and anything else managed by the connection's custodian to run-server, which seems reasonable to me.
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