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)
Thank you !
Eran