Writing a timer event (how to execute an event from time to time)

Peter Leonov gojpeg at gmail.com
Tue May 25 16:54:36 MSD 2010

> [...]
>> More to say, the timers mechanism is request agnostic. nginx doesn't track
>> if the timer (wev) structure is already trashed, or even the entire request
>> is freed. So you have to manage the lifetime of the timer manually.
> Like I said, my request is not freed because I lock responding to the
> client with http chunked response and buf->last_buf = 0. At least I
> think my request would not be freed until I call ngx_finalize_request,
> right?
Can't say for 0.7.x, but in 0.8 the request may be cleaned up before the last
buffer is sent. For example, returning NGX_ERROR from a handler would lead to
the request cleanup as soon as possible. It may be false for 0.7.

Setting up a cleanup handler seems to be a good thing to do anyway ;)
Like so:

ngx_http_cleanup_t        *cln;

cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
    return NGX_ERROR;

cln->data = r;
cln->handler = cleanup_handler;

static void
cleanup_handler(void *data) {
    ngx_http_request_t        *r;
    r = data;
    // here get the module context ant delete the timer

Best regards,

More information about the nginx-devel mailing list