edicl/hunchentoot — pull request #38 by lemoinem (2012-08-22 19:04:58+00:00)
Title: Fix fd exhaustion if the connection is closed prematurely

Here is the patch for fd exhaustion.

The test is simple, run something like:
`$ for i in $(seq 1 3000) ; cat test | nc -c localhost 80 & done`
(test does not even need to exist) and you will hit case 1.

Remove the "-c" argument for nc, and you will hit case 2 (or the HTTPS one if hunchentoot expects an SSL connection).

In any case, you will end up with hunchentoot crashing with the message "Too many fd opened".

Details:

If the handling thread does not reach process-connection, the socket stream will
never be closed and hunchentoot will eventually run out of available file
descriptors:

1. If the connection is refused by the server (i.e. too-many-taskmaster-requests
is called), send-service-unavailable-reply doesn't close the socket stream.

2. If the connection is closed by the client before the handling thread is
spawned, the error handler in create-request-handler-thread does not close the
stream socket.

Moreover, in the case of an HTTPS request, if the client drops the connection
before the SSL stream is fully initialized (or if the SSL initialization fails),
*hunchentoot-stream* in process-connection will not have been bound and the
socket stream will not be closed.