Newbie: Shmem constructor not being called after reload

Acácio Centeno acacio.centeno at
Thu Jul 16 22:07:28 UTC 2015


On Emiller's guide to Nginx [1] it's stated that the shared memory
constructor would receive a previously allocated data pointer as a second
parameter upon a restart, so the constructor could reuse this area and
avoid losing the data. I've searched the forum archives using [2] and found
the thread [3], that stated this behavior too.

I've written a module that creates a shared memory area on a command
initialization (a location configuration) and the constructor is properly
called when Nginx starts. The area is created and I can use it effectively.
But when I issue a "nginx -s reload", the constructor is not called at all,
and I loose the data that was previously on the shared memory.

I'm wondering what I'm doing wrong. Is it because I'm calling
ngx_shared_memory_add on a location configuration?

This is what I'm doing:

static ngx_command_t ngx_http_mod_tutorial_commands[] = {

static char *
ngx_http_mod_tutorial_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *_)
    conf->shm_zone = ngx_shared_memory_add(
        cf, shm_name, 2 * ngx_pagesize, &ngx_http_mod_tutorial_module);
    if (!conf->shm_zone) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Could not create shm.");
    conf->shm_zone->init = ngx_http_mod_tutorial_init_shm_zone;

static ngx_int_t
ngx_http_mod_tutorial_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)
fprintf(stderr, "SHM_ZONE %p\n", data);

if (data) {
shm_zone->data = data;
return NGX_OK;

The message to stderr is printed when Nginx starts, but not after a reload,
that's why I know that the constructor is not being called again. Also, I
have a counter on the shared area and it's value is reset after a reload.

Any thoughts?


Best regards,
Acácio Centeno
Software Engineering
Azion Technologies
Porto Alegre, Brasil +55 51 3012 3005 | +55 51 8118 9947
Miami, USA +1 305 704 8816

Quaisquer informações contidas neste e-mail e anexos podem ser
confidenciais e privilegiadas, protegidas por sigilo legal. Qualquer forma
de utilização deste documento depende de autorização do emissor, sujeito as
penalidades cabíveis.

Any information in this e-mail and attachments may be confidential and
privileged, protected by legal confidentiality. The use of this document
require authorization by the issuer, subject to penalties.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx-devel mailing list