Skip to content

Windows: keep the extension loaded while Tokio's threads run - #15

Merged
danog merged 5 commits into
masterfrom
windows-unload
Nov 20, 2026
Merged

danog merged 5 commits into
masterfrom
windows-unload

Conversation

@danog

@danog danog commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes the intermittent 0xC0000005 of ShutdownTest::testASuspensionThatCantSuspend on Windows (seen on PHP 8.2).

Mechanism: at the end of the process, zend_shutdown() → zend_destroy_modules() → DL_UNLOAD calls FreeLibrary() on the extension. Threads of a Tokio runtime may still run its code at that point:

  • those of the shared RUNTIME, which is never shut down;
  • those of a runtime from 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 before FreeLibrary() 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): testTheProcessExitsWhileAFutureOfTrySuspendOnRuns keeps a runtime thread busy with a future that yields without parking. Without the fix, it crashed every Windows job, for both init() and initWith(), 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:

  • MadelineCrypto joins its own executor thread at RSHUTDOWN, but its Tokio runtime (from init_with()) has the same problem. It pins php-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).
  • mongo-php-async-driver and revolt-rs spawn threads only in their tests.

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 calls shutdown(), 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.

…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
danog marked this pull request as ready for review November 20, 2026 13:49
@danog
danog merged commit bd475c2 into master Nov 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant