Separate the spilling logic.
constify the input arguments.
Make the main loop do the socket closing.
Move the debug code out of the way so the loop is clearer.
At worker shutdown, as the last resort, spill all that's left.
Improve shutdown messages.