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
{{ message }}
Repository navigation
kill-connection-w/o-timer! can take way too long #71
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.
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.
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?
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:
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:
Edit 2: I dove in a little further this afternoon.
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
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.
Activity
samth commentedon Sep 13, 2019
cc @zkry
Bogdanp commentedon Sep 13, 2019
I'm surprised the calls to
close-{input,output}-portare necessary. The documentation forrun-serverstates that the connections are managed by a new custodian so shouldn'tcustodian-shutdown-allalso 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.
samth commentedon Sep 13, 2019
As far as I can tell,
timealways reports 0 time for those calls, and the error handlers are never called in the benchmark. So I'm very confused.jeapostrophe commentedon Sep 13, 2019
My guess is that you are observing the time to flush the buffers.
samth commentedon Sep 13, 2019
But which buffers are being flushed? And why does it impact other requests (which are presumably in other threads)?
jeapostrophe commentedon Sep 13, 2019
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?
Bogdanp commentedon Sep 14, 2019
I investigated a little further and I don't think the problem has to do with buffering.
The reason
telnethangs when I replace the body ofkill-connection-w/o-timer!with(void)is because that makes it so that the input port is never closed. Because it is never closed, theconnection-loopinsidehandle-connection/cmnever exits (because there's no input coming in and because theport-close-evton the input port never fires (because it is never closed)).That makes sense, although I'm still surprised that the(explained in "edit 2")connection-loop-ing thread isn't killed when its custodian gets shut down.However, when the unmodified
kill-connection-w/o-timer!is triggered, it closes the two ports and then shuts down the custodian, which triggers theport-close-evtinside ofhandle-connection/cm, which is running inside a thread belonging to the custodian being shut down, which itself callskill-connection!again, attempting to close the ports again and then shutting down the custodian again. Theconnection-loopthen terminates and the connection handling function exits, which signals torun-serverthat the custodian should be shut down... again.When I modify(see "edit 2")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 callingcustodian-shutdown-allfrom 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 withinrun-server. I don't know enough about the runtime yet to know for sure, but does that sound plausible?You can test this yourselves by changing
kill-connection-w/o-timer!to:Thought: is it possible the connection-handling thread is owned by another custodian other than the connection's custodian? That would explain why calling(see "edit 2")custodian-shutdown-allby itself doesn't stop the connection thread.Edit: here's what the plot looks like after I made that change:
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 toclose-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 toclose-output-port, the latency spikes don't happen. So, this codeis not affected by the latency spikes. Uncommenting the
close-output-portcall brings back the spikes, regardless of whether or not I leave theflush-outputcall 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):
close-output-porttake so long under these circumstances? I can see thatfd-closeis 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 (manyfds being closed all at once) over that semaphore be the culprit?custodian-shutdown-all(the call made byrun-server) take just as long, given that it supposedly closes all the TCP ports that belong to it?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 torun-server, which seems reasonable to me.