php / php/php-src

Apache SIGTERM causes module shutdown to run at runtime

Open
#17,348 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug SAPI: apache2handler Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

Apache workers tends to sometimes crash when killed via SIGTERM:

Sample trace:

#2 php_module_shutdown () at ./main/main.c:2455
#3 0x00007fee0fbfc83d in php_module_shutdown_wrapper (sapi_globals=<optimized out>) at ./main/main.c:2448
#4 0x00007fee0fce6d05 in php_apache_child_shutdown (tmp=<optimized out>) at ./sapi/apache2handler/sapi_apache2.c:422
#5 0x00007fee11324d2e in apr_pool_destroy () from /usr/lib/x86_64-linux-gnu/libapr-1.so.0
#6 0x00007fee10f83303 in clean_child_exit () from /usr/lib/apache2/modules/mod_mpm_prefork.so
#7 0x00007fee10f83341 in just_die () from /usr/lib/apache2/modules/mod_mpm_prefork.so
#8 0x00007fee0fc866cd in zend_signal_handler (signo=signo@entry=15, siginfo=siginfo@entry=0x7fffe103fc30, context=context@entry=0x7fffe103fb00) at ./Zend/zend_signal.c:217
#9 0x00007fee0fc86873 in zend_signal_handler_defer (signo=15, siginfo=0x7fffe103fc30, context=0x7fffe103fb00) at ./Zend/zend_signal.c:102
#10 <signal handler called>
#11 0x00007fee11203bbf in __GI___poll (fds=fds@entry=0x7fffe1040890, nfds=nfds@entry=1, timeout=86400000) at ../sysdeps/unix/sysv/linux/poll.c:29
<a mysql function here>

The specific crash location within php_module_shutdown can vary quite a bit, depending on when exactly the signal handler was called.
But in particular, PHP does quite a lot of cleanup during module shutdown, and most is neither signal safe, nor can refcounts be relied on being correct.

I'd expect PHP to avoid any complex operations (like a module shutdown) upon receiving a SIGTERM signal. (Just like PHP also avoids doing special handling for CLI processes receiving a SIGTERM.)

PHP Version

PHP 7.4 - 8.4

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with php_apache_child_shutdown in sapi/apache2handler/sapi_apache2.c and the php_module_shutdown path in main/main.c, then inspect SIGTERM handling in Zend/zend_signal.c. Compare the Apache worker path with the CLI SIGTERM behavior described in the issue and reproduce the Apache shutdown trace. Done means SIGTERM no longer enters unsafe module-shutdown cleanup in Apache workers.

Written by the indexing model from the issue text.

Assessment

Tech stack
apache, c, php
Domain
backend, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.