Skip to content

Throw FilesystemException when uv_fs_open() fails (3.x) - #103

Open
danog wants to merge 1 commit into
amphp:3.xfrom
danog:fix-uv-open-errors-3.x
Open

danog wants to merge 1 commit into
amphp:3.xfrom
danog:fix-uv-open-errors-3.x

Conversation

@danog

@danog danog commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Since ext-uv 0.3, the uv_fs_open() callback gets a negative error code on failure instead of false. On 3.x, the !$fh checks of UvFilesystemDriver::read() and write() let that integer through, and it reaches uv_fs_fstat() / uv_fs_write() as a file handle. The result is a TypeError instead of a FilesystemException. Seen in production on a disk out of inodes (-28, ENOSPC):

TypeError: uv_fs_write(): Argument #2 ($fd) must be of type resource, int given in vendor/amphp/file/src/Driver/UvFilesystemDriver.php:393
#1 vendor/amphp/file/src/Filesystem.php(329): Amp\File\Driver\UvFilesystemDriver->write(...)
#2 vendor/amphp/file/src/functions.php(300): Amp\File\Filesystem->write(...)

4.x fixed the checks in 756b1cb, which was never backported. This backports it, and has doFsOpen() throw the FilesystemException itself, with the path and libuv's error (uv_strerror()): Failed opening file handle to /nonexistent-dir/x: no such file or directory.

Tests

  • Two new tests in FilesystemDriverTest cover every driver: reading a missing file, and writing into a missing directory, must throw FilesystemException. With ext-uv 0.3, both fail on 3.x with the TypeError and pass with this change.
  • The suite otherwise has the same results with and without it. UvFileTest::testClose and testWriteAfterClose already fail on 3.x with ext-uv 0.3 (no ClosedException after close()); 4.x changed that code with the locking rework, so it's left out here.
  • psalm reports no errors. php-cs-fixer only flags existing Eio files.

#104 adds the error reason to 4.x.

https://claude.ai/code/session_0185K6MyhFXHvLLtf79c6Xif

ext-uv 0.3 passes the (negative) error code to the uv_fs_open() callback
on failure instead of false, so the `!$fh` checks of read() and write()
let it through, and it reached uv_fs_fstat()/uv_fs_write() as a file
handle: a TypeError instead of a FilesystemException (e.g. a write on a
full disk: "uv_fs_write(): Argument amphp#2 ($fd) must be of type resource,
int given"). doFsOpen() now throws FilesystemException itself, with the
error of libuv (uv_strerror()).

4.x fixed the checks in 756b1cb (without the error): backported here.
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