terminate a connection after sending headers

Maxim Dounin mdounin at mdounin.ru
Thu Sep 4 01:10:02 UTC 2014


Hello!

On Wed, Sep 03, 2014 at 10:10:01AM -0400, erankor2 wrote:

> 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
> there's:
> 
>     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:
> https://github.com/erankor/nginx-hello-world-module/blob/master/ngx_http_hello_world_module.c
> 
> 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)

The problem is that you use your own timer, and don't run posted 
requests after it - but the request termination code relies on 
posted requests being run.

If you are using your own events, you should do processing similar 
to ngx_http_request_handler(), see src/http/ngx_http_request.c.  
Notably, you have to call the ngx_http_run_posted_requests() 
function after the code which calls ngx_http_finalize_request().

In this particular case, trivial fix is to do something like:

 static void
 event_callback(ngx_event_t *ev)
 {
+       ngx_connection_t   *c;
        ngx_http_request_t *r = (ngx_http_request_t *)ev->data;
        
+       c = r->connection;
+       
        ngx_http_finalize_request(r, NGX_ERROR);
+
+       ngx_http_run_posted_requests(c);
 }


-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list