Skip to content

fix(tia): stop ReadCapture from keeping a wrapper per filesystem call - #2057

Merged
MrPunyapal merged 3 commits into
pestphp:5.xfrom
MohannadElemary:fix/read-capture-wrapper-memory
Nov 21, 2026
Merged

MrPunyapal merged 3 commits into
pestphp:5.xfrom
MohannadElemary:fix/read-capture-wrapper-memory

Conversation

@MohannadElemary

@MohannadElemary MohannadElemary commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What:

  • Bug Fix
  • New Feature

Description:

With --tia recording, memory grows with every filesystem call a test makes, and a little more for every test. On a long suite this adds up (#2035).

The cause is in PHP, not in the recorder. ReadCapture steps aside around each filesystem call and at the end of each test by calling stream_wrapper_restore('file'), then registers itself again. stream_wrapper_restore() drops the user wrapper from the table but never frees it, so every call leaves one wrapper behind until the process ends. stream_wrapper_unregister() does free it.

This PR makes disable() call stream_wrapper_unregister('file') first, then restore or re-register the previous wrapper as before. The unregister is skipped when file:// is already gone, so a test that unregistered it itself does not raise a notice.

Four tests in tests/Unit/Plugins/Tia/ReadCapture.php:

  • 10,000 fopen() and fclose() calls while a test records keep under 100 KB.
  • 5,000 record cycles keep under 100 KB.
  • A test that already restored file:// gets no notice, and the plain wrapper is back after.
  • A test that unregistered file:// gets no notice, and the plain wrapper is back after.

On the current 5.x code the first three fail. The fourth fails on the fix without the guard. tests/.snapshots/success.txt was regenerated with REBUILD_SNAPSHOTS=true, and the totals in tests/Visual/Parallel.php come from a real parallel run.

Measured on PHP 8.4, 8.5 and 8.6 RC7:

  • 10,000 fopen() and fclose() pairs while recording: about 1.6 MB before on PHP 8.4 and 8.5 (1.4 MB on 8.6), and 16 bytes after, including 8.6 with OPcache on for the CLI.
  • 10,000 test start and stop cycles: +937.5 KB before on 8.4 and 8.5 (+703.1 KB on 8.6), 0 after.

On a Laravel 13 app with 401 tests (PHP 8.4, pcov):

  • --tia --fresh: +40.22 MB before, +34.41 MB after, +34.48 MB with read capture turned off.
  • --tia --fresh --parallel --processes=4: the workers grow 49.29 MB in total before and 43.4 to 43.6 MB after.
  • Changing a JSON fixture still selects the one test file that reads it, with and without --parallel.

With OPcache on for the CLI, files included through the wrapper still load and are cached. An exception thrown during a filesystem call still leaves ReadCapture registered, and the right wrapper is back at the end of the test.

This does not change the separate failing test reported in #2035, or #2039 and #2056. If #2039 lands and ReadCapture stops swapping wrappers per call, this fix is no longer needed.

Related:

Fixes the memory growth in #2035

stream_wrapper_restore() takes a user wrapper out of the wrapper table
but does not free it, so every filesystem call made while a test records
kept one until the process ended. Unregistering it first frees it.
A test can unregister file:// itself. stream_wrapper_unregister() then
warns, where stream_wrapper_restore() alone did not, so check first.

Also run the per-test memory check over 5,000 tests and update the
snapshot and parallel totals for the new tests.
@MrPunyapal

Copy link
Copy Markdown
Member

Please use fopen() and fclose() in the memory regression test. That still catches the wrapper leak and avoids the separate PHP 8.6 OPcache issue in url_stat().

@MohannadElemary

Copy link
Copy Markdown
Contributor Author

Done in 1c0a122. The 10,000-call test now uses fopen() and fclose().

It still catches the leak: about 1.4 to 1.6 MB growth without the fix, and 16 bytes with it, including on PHP 8.6 with OPcache on for the CLI.

I also updated the PR description and removed the unrelated PHP 8.6 caveat.

@MrPunyapal MrPunyapal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the updated memory regression on PHP 8.6 with CLI OPcache enabled. Integration and CI passed.

@MrPunyapal
MrPunyapal merged commit 66e5850 into pestphp:5.x Nov 21, 2026
7 checks passed
@MrPunyapal

Copy link
Copy Markdown
Member

Thanks!

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.

2 participants