terminate a connection after sending headers

erankor2 nginx-forum at nginx.us
Wed Sep 3 14:10:01 UTC 2014

Maxim, thank you very much for your response.

To clarify - the problem is not about freeing the request (I don't think
there's a resource leak here), the problem is that the connection is just
left hanging until the client closes it / the server is restarted. 
It is true that write_event_handler gets initialized to zero when the
request is allocated, but it is set to ngx_http_request_empty_handler before
the first line of my code even runs. In ngx_http_core_content_phase

    if (r->content_handler) {
        r->write_event_handler = ngx_http_request_empty_handler;
        ngx_http_finalize_request(r, r->content_handler(r));
        return NGX_OK;

where r->content_handler is the entry point to my code. So, unless I
explicitly reset it back to NULL (something that I never saw in any other
nginx module) write_event_handler will not be null and the connection will
be left hanging.

I forked some sample hello world module and modified it to reproduce the
problem, please check it out here:

In that code, I'm sending the response headers and then trigger a timer for
1 second. In the timer callback I close the request with NGX_ERROR, but the
connection remains active (I used a timer here since that's the easiest
solution to defer the execution, in my real project I'm performing
asynchronous file I/O)

Thank you !


Posted at Nginx Forum: http://forum.nginx.org/read.php?2,253006,253040#msg-253040

More information about the nginx mailing list