Repository navigation
Windows: keep the extension loaded while Tokio's threads run - #15
Merged
Merged
Conversation
…ntime thread busy On Windows, PHP unloads the extension (FreeLibrary) at the end of the process, while the thread still runs its code: the intermittent 0xC0000005 of testASuspensionThatCantSuspend, whose 5 ms future ends during the shutdown, made deterministic.
…may run its code PHP unloads the extensions at the end of the process (zend_destroy_modules(), FreeLibrary()), while the threads of RUNTIME (never shut down) or of a runtime from init_with() (released in the background) may still run the futures of try_suspend_on() that PHP stopped waiting for: the code was unmapped under them (0xC0000005). Linux and macOS never unload a module whose threads registered thread-local destructors; on Windows, the module is now pinned (GetModuleHandleExW with GET_MODULE_HANDLE_EX_FLAG_PIN) before the first runtime thread starts.
…d (embed SAPI) php-tokio's module outlives PHP (Linux and macOS keep it loaded, and Windows now does too), so its statics do: the resource type registered by the first event loop belongs to the PHP that registered it.
… name since PHP started It was registered once per process, in a OnceLock: once PHP started again in the same process (the module stays loaded), the ID was of the old table of types. No type had it (or another extension's did), so the event loop of the request was never shut down, and the next one reused it, with the memory PHP had freed: tests/embed/restart.sh crashed on the third start.
danog
marked this pull request as ready for review
November 20, 2026 13:49
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.
Fixes the intermittent
0xC0000005ofShutdownTest::testASuspensionThatCantSuspendon Windows (seen on PHP 8.2).Mechanism: at the end of the process,
zend_shutdown()→zend_destroy_modules()→DL_UNLOADcallsFreeLibrary()on the extension. Threads of a Tokio runtime may still run its code at that point:RUNTIME, which is never shut down;init_with(), which is released in the background;and they run the futures of
try_suspend_on()that PHP stopped waiting for. The code is unmapped under them, so a thread faults as soon as it runs. In that test,trySleep(5)can't suspend, so its 5 ms timer fires during the shutdown, sometimes beforeFreeLibrary()and sometimes after. Linux and macOS never unload a module whose threads registered thread-local destructors (Tokio's do), which is why they never crash.Repro (first commit):
testTheProcessExitsWhileAFutureOfTrySuspendOnRunskeeps a runtime thread busy with a future that yields without parking. Without the fix, it crashed every Windows job, for bothinit()andinitWith(), on PHP 8.2, 8.5, 8.6 and in coverage (run), and passed on Linux and macOS.Fix: before the first runtime thread starts, the module is pinned with
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN), so Windows keeps it loaded until the process exits, as the other platforms do. Shutting the runtimes down from a persistent resource's destructor instead wouldn't cover futures that outlast a shutdown timeout, nor blocking tasks.Same flaw elsewhere: any Rust extension whose threads outlive the request has the same problem on Windows. I checked ours:
init_with()) has the same problem. It pinsphp-tokio = "=0.2.1", so it gets this fix only with php-tokio 0.2.3 and a MadelineCrypto release that requires it (0.6.3).Same PR, a consequence of the pin: php-tokio's statics now outlive PHP on Windows too, as they already did on Linux and macOS, and PHP can start again in the same process (as FrankenPHP restarts it). The resource type that shuts the event loop down was registered once per process and kept in a
OnceLock. After a restart, that ID belongs to the old table of types: either no type has it anymore (PHP warns "Unknown list entry type" and never callsshutdown(), which brings back 0.2.1's use-after-free), or another extension's type got it, and its destructor gets our NULL pointer. The type is now looked up by name (zend_fetch_list_dtor_id) and registered when the current PHP doesn't have it. Regression test:tests/embed/restart.sh(new CI job), which starts PHP 3 times in one process through the embed SAPI.