fix: don't segfault when the PHP CLI is executed while FrankenPHP is running - #2593
Open
ousamabenyounes wants to merge 2 commits into
Open
fix: don't segfault when the PHP CLI is executed while FrankenPHP is running#2593ousamabenyounes wants to merge 2 commits into
ousamabenyounes wants to merge 2 commits into
Conversation
…running ExecuteScriptCLI and ExecutePHPCode start the embedded PHP CLI SAPI via php_embed_init(). When FrankenPHP is already running in the same process (Init has been called), booting the embedded SAPI on top of the running FrankenPHP SAPI corrupts the PHP engine and crashes the whole process with a segmentation fault. Refuse the call cleanly instead: when FrankenPHP is running, log an error and return a non-zero exit status rather than crashing. The standalone CLI path (no prior Init) is unchanged. Fixes php#2342 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
staticcheck QF1008: exec.ExitError embeds *os.ProcessState, so Exited() is promoted (as ExitCode() is already used on the next line).
Contributor
|
But this can never happen? There's no code wiring to create this scenario. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
frankenphp.ExecuteScriptCLI()(andExecutePHPCode()) crash the whole processwith a segmentation fault when called after
frankenphp.Init()in the sameprocess (issue #2342):
Why
Both functions start the embedded PHP CLI SAPI through
php_embed_init(). Thatruns
sapi_startup()+php_module_startup()for the embed SAPI. When FrankenPHPis already running, its own SAPI and the PHP engine are already started, so
booting a second SAPI on top corrupts global engine state and the process
crashes.
What this PR does
The minimal, safe fix: prevent the crash. When FrankenPHP is already running,
ExecuteScriptCLI/ExecutePHPCodenow log an error and return a non-zero exitstatus instead of booting the embedded CLI SAPI and segfaulting. The standalone
CLI path (no prior
Init) is untouched.This intentionally does not add a new way to run CLI scripts inside a running
server (the broader idea discussed on the issue, e.g. running on a dedicated
thread/child process); it only turns an undefined-behaviour crash into a defined,
recoverable error. That larger feature can be built on top later.
Thread-safety note: the guard reads
isRunning(andglobalLogger) without anextra lock, consistent with how
Init/Shutdownalready manageisRunningandhow these globals are read elsewhere in the package. CLI-after-
Initis asequential misuse, not concurrent access.
Test verification (RED → GREEN)
Run in the dev image (
dev.Dockerfile), PHP 8.5 ZTS,-tags nowatcher.RED — unmodified
main(only the repro/test applied):(A standalone
Init()+ExecuteScriptCLI()program likewise crashes withsignal: segmentation fault, exit 139.)GREEN — with the fix (
go test -race):Existing standalone CLI tests still pass (no regression).
refuseCLIWhileRunningshows 100% coverage;
go build,go vet,gofmt, the Caddy module build andgo mod tidy -diffare all clean.