Skip to content

Fix GH-24256: fclose() returns false for stream wrappers without stream_flush() - #25543

Open
TiagoCruz1224 wants to merge 1 commit into
php:PHP-8.6from
TiagoCruz1224:fix-gh24256
Open

TiagoCruz1224 wants to merge 1 commit into
php:PHP-8.6from
TiagoCruz1224:fix-gh24256

Conversation

@TiagoCruz1224

@TiagoCruz1224 TiagoCruz1224 commented Nov 17, 2026 •

Copy link
Copy Markdown

Since c07cc85 (bug #60110), fclose(), file_put_contents() and copy() return the result of the flush on close. A user wrapper without stream_flush() makes that flush fail, so these functions return false although all the data was written. For fclose() this happens when something was written since the last fflush(), because only then is there a flush on close. stream_flush() is optional, so when the stream is closed a missing one now counts as nothing to flush. An explicit fflush() still returns false for such a wrapper, and a stream_flush() that returns false still makes fclose() return false.

This picks up #24259 by @lazerg, which was closed for inactivity, with the same fix and a test that also covers the explicit fflush() and a failing stream_flush().

Fixes #24256

I prepared this change with AI assistance, then reviewed it and verified each case myself.

@AceMedia

Copy link
Copy Markdown

I tested this against the current PHP-8.6 tip (1be37f6). The patch no longer applies cleanly there because php_userstreamop_flush() has since gained the NO_FCLOSE handling, so I ported the same change by hand (the call_result == FAILURE branch returning 0 when stream->in_free) and added the PR's gh24256.phpt.

Results on a CLI/FPM build with -O2, no debug:

  • gh24256.phpt fails on the untouched tip and passes with the change.
  • For a wrapper without stream_flush(), file_put_contents() returned false on the tip even though all bytes were written. It now returns the byte count. fclose() already returned true there once fflush() had run, so the visible difference is file_put_contents().
  • A failing stream_flush() still makes fclose() return false, and file_put_contents() still returns false. Short writes still warn and return false.
  • ext/standard/tests/file and ext/standard/tests/streams: 1120 tests, 815 passed, 305 skipped, 0 failed. This was a CLI-only build, so I have not run the full suite.

The PR will need a rebase onto the current PHP-8.6 branch. I have not tested Windows.

Disclosure: I used an LLM to help run these checks and to draft this comment. I reviewed the results before posting.

…tream_flush()

Since the fix for bug #60110, fclose(), file_put_contents() and copy()
return the result of the flush on close. A user stream wrapper without
stream_flush() made that flush fail, so these functions returned false
although nothing went wrong. stream_flush() is optional, so when the
stream is closed a missing one now means there was nothing to flush.
An explicit fflush() still returns false for such a wrapper, and a
stream_flush() that returns false still makes fclose() return false.

Based on phpGH-24259 by lazerg.
@TiagoCruz1224

Copy link
Copy Markdown
Author

Thanks for testing this, @AceMedia.

I rebased onto the current PHP-8.6 (d9567b9). The only conflict was with the NO_FCLOSE handling from GH-25586. The flag is still restored right after the stream_flush() call, and the call_result == FAILURE branch comes after it, the same as your port. Nothing else changed.

I ran ext/standard/tests/file and ext/standard/tests/streams on a debug CLI build (1120 tests, 773 run). gh24256.phpt fails without the change and passes with it, and there are no other differences. Without the change, the issue's script still gets fclose(): false after a plain fwrite().

You're right about fclose() after an explicit fflush(). It only flushes on close when something was written since the last flush, so it already returned true there. I've made that clearer in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants