Welcome! Log In Create A New Profile

Advanced

Re: terminate a connection after sending headers

Maxim Dounin
September 03, 2014 09:12PM
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/

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

terminate a connection after sending headers

erankor2 September 01, 2014 05:25PM

Re: terminate a connection after sending headers

Maxim Dounin September 02, 2014 09:10AM

Re: terminate a connection after sending headers

erankor2 September 03, 2014 10:10AM

Re: terminate a connection after sending headers

Maxim Dounin September 03, 2014 09:12PM

Re: terminate a connection after sending headers

erankor2 September 04, 2014 04:38PM

Re: terminate a connection after sending headers

Maxim Dounin September 05, 2014 12:36AM

Re: terminate a connection after sending headers

erankor2 September 07, 2014 04:49PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 194
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready