Skip to content

ParallelFilesystemDriver: keep the pool alive for open files, tolerate a worker gone before a destructor's close - #105

Open
danog wants to merge 1 commit into
amphp:3.xfrom
danog:fix-parallel-file-lifetime-3.x
Open

danog wants to merge 1 commit into
amphp:3.xfrom
danog:fix-parallel-file-lifetime-3.x

Conversation

@danog

@danog danog commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Two related problems with ParallelFilesystemDriver. Together they throw StatusError: The worker has been shut down into the event loop, failing whatever is running at the time.

1. Open files don't keep the driver's pool alive

When no pool is given, the driver creates its own (new ContextWorkerPool(...)), and the files it opens don't hold a reference to it. Once the driver is destroyed, for example when it's replaced as the global driver with filesystem($otherDriver), the pool is destroyed too, and ContextWorkerPool::__destruct() kills the workers that still hold open files.

FileWorker now keeps the pool alive while its file is open.

2. ParallelFile::__destruct() races with the worker's exit

The destructor checks that the worker runs, then queues the fclose. If the worker exits in between (its pool is shut down or killed, or it dies), execute() throws StatusError or WorkerException from the queued callback, where nothing can catch it. The worker took the file with it, so those two exceptions are now ignored there. Other failures still propagate.

How we hit it

In MadelineProto, Magic::start() replaces the global driver. Files opened before that, through amphp/file's default parallel driver, then fail like this. It happened in every run of one of our tests without ext-uv or ext-eio, and it can happen in any application that uses amphp/file before MadelineProto starts.

Tests

Two tests in ParallelFileTest, both failing before this change with StatusError: The worker has been shut down:

  • testOpenFileKeepsTheDriversPoolAlive: the driver is destroyed while a file it opened is still read.
  • testFileDestroyedBeforeItsWorkerShutsDown: a file is destroyed, then its pool is killed before the queued close runs.

The full suite passes, and Psalm reports no new issues (58 before and after, all pre-existing).

The same change applies to 4.x; I'll open a separate PR for it.

… before a destructor's close

ParallelFilesystemDriver creates and owns its pool when none is given, and
the files it opens didn't keep that pool alive: once the driver was
destroyed (for example replaced as the global filesystem driver), the pool
was destroyed too, killing the workers holding the files still open.
FileWorker now keeps the pool alive while its file is open.

ParallelFile::__destruct() checks that its worker runs, then queues the
fclose: if the worker exited meanwhile (its pool was shut down or killed,
or it died), the queued close threw StatusError (or WorkerException) in
the event loop, where nothing could catch it. The worker took the file
with it: those exceptions are now ignored there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant