comparison src/core/nginx.c @ 9270:3d455e37abf8

Core: PID file writing synchronization. Now, ngx_daemon() does not call exit() in the parent process immediately, but instead waits for the child process to signal it actually started (and wrote the PID file if configured to). This ensures that the PID file already exists when the parent process exits. To make sure that signal handlers won't cause unexpected logging in the parent process if the child process dies (for example, due to errors when writing the PID file), ngx_init_signals() is moved to the child process. This resolves "PID file ... not readable (yet?) after start" and "Failed to parse PID from file..." errors as observed with systemd. Note that the errors observed are considered to be a bug in systemd, which isn't able to work properly with traditional Unix daemons. Still, the workaround is implemented to make sure there will be no OS vendor patches trying to address this.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 13 May 2024 06:13:22 +0300
parents 6a3ee145d0b5
children
comparison
equal deleted inserted replaced
9269:4eb02e5ddb48 9270:3d455e37abf8
340 ngx_process = NGX_PROCESS_MASTER; 340 ngx_process = NGX_PROCESS_MASTER;
341 } 341 }
342 342
343 #if !(NGX_WIN32) 343 #if !(NGX_WIN32)
344 344
345 if (ngx_init_signals(cycle->log) != NGX_OK) {
346 return 1;
347 }
348
349 if (!ngx_inherited && ccf->daemon) { 345 if (!ngx_inherited && ccf->daemon) {
350 if (ngx_daemon(cycle->log) != NGX_OK) { 346 if (ngx_daemon(cycle->log) != NGX_OK) {
351 return 1; 347 return 1;
352 } 348 }
353 349
361 #endif 357 #endif
362 358
363 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { 359 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
364 return 1; 360 return 1;
365 } 361 }
362
363 #if !(NGX_WIN32)
364
365 if (ngx_init_signals(cycle->log) != NGX_OK) {
366 return 1;
367 }
368
369 if (ngx_daemon_sync(cycle->log) != NGX_OK) {
370 return 1;
371 }
372
373 #endif
366 374
367 if (ngx_log_redirect_stderr(cycle) != NGX_OK) { 375 if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
368 return 1; 376 return 1;
369 } 377 }
370 378