releasing memory.

Maxim Dounin mdounin at
Thu May 18 13:32:08 UTC 2017


On Wed, May 17, 2017 at 05:53:40PM -0700, Dk Jack wrote:

> Hi,
> In my module, I registered a NGX_HTTP_POST_READ_PHASE handler. When this
> handler is invoked, I allocate my module context and attach it to the
> request. I also allocate some other memory and save the pointers to that
> memory in my context. In the POST_READ_PHASE handler, I also register a
> cleanup handler to the request (r->cleanup; ngx_http_cleanup_t
> <>). I assumed this is
> called before the request is freed.
> As I expected, my cleanup handler is getting called for most of the
> requests. However, in some cases it doesn't get called. This seems to
> happen mostly for the first request and if and only if the request is a
> very simple url like 'http://servername/' i.e. a simple request which tests
> server is up and running or not. If I add anything more to the uri-path,
> the cleanup handler gets called. Otherwise, it doesn't get called. Seems
> very strange. Could someone more knowledgeable comment on this behavior.
> Thanks.

HTTP cleanup handlers as added by ngx_http_cleanup_add() are only 
called on premature request termination via 
ngx_http_terminate_request().  They are to be used if your module 
does something that is expected to prevent normal request closing 
(for example, via increased r->count), but want nginx to be able 
to cancel this activity if it needs to terminate the request 
prematurely for some reason (usually due to an error).  For 
example, upstream module installs a http cleanup handler when it 
connects to an upstream server, and aborts the connection if the 
cleanup handler is called.

These handlers are not expected to be called on normal request 
completion, if nothing prevents normal close of a request.

If your goal is to simply release resources when a request is 
freed, use a r->pool cleanup handler instead, as added by 

Maxim Dounin

More information about the nginx-devel mailing list