terminate a connection after sending headers

Maxim Dounin mdounin at mdounin.ru
Tue Sep 2 13:09:03 UTC 2014


On Mon, Sep 01, 2014 at 05:25:56PM -0400, erankor2 wrote:

> Hi all,
> In the module I'm developing, I have the possibility of encountering an
> error after the response headers were already sent. As the headers were
> already sent (with status 200) the only way for me to signal the error to
> the client would be to close the connection. I tried calling
> ngx_http_finalize_request with both NGX_ERROR and NGX_HTTP_CLOSE and the
> connection is not closed. 
> After debugging it, I found it has to do with the 'if
> (mr->write_event_handler)' in ngx_http_terminate_request. I'm not sure what
> is the purpose of this if, but in my case write_event_handler has the value
> ngx_http_request_empty_handler, so the if evaluates to true and the
> connection is not terminated. When I forcefully change write_event_handler
> to NULL with GDB, I see the connection is closed as expected. 
> I searched the code for 'write_event_handler =' and could not find a single
> place where this member gets a value of NULL (it always gets a pointer to
> some function).

The r->write_event_handler is set to NULL on initial creation of a 
request.  If write_event_handler is not NULL during a request 
termination, nginx posts an event instead of freeing the request 
request directly (this is done to avoid use-after-free when 
processing posted subrequests).  The request is still freed 

Maxim Dounin

More information about the nginx mailing list