[Bug]: [mutate] Windows: StreamWrapper::stream_lock() always takes LOCK_SH, so LOCK_EX writes fail in a mutant child and the mutant counts as killed

Open Beginner friendly
#2,072 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@joshdaugherty is already working on this.

Since Nov 23, 2026.

  • #55 by @joshdaugherty — open
  • #56 by @mohammed-a-ashqar — open

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
testing

Research direction

The defect is in pest-plugin-mutate's src/Support/StreamWrapper.php, in stream_lock() around line 179; read that method and compare it with the wrapper's other stream_* methods. Reproduce with the four-file project in the report: run the plain test first, then vendor\bin\pest.bat --mutate --path=src/Store.php tests/StoreTest.php, and use the PEST_MUTATION_TESTING/PEST_MUTATION_FILE child invocation to see the failing write. Done means the child run passes and the mutate run reports Mutations: 3 untested, 0 tested rather than a 100% score.

Written by the indexing model from the issue text.

Description

What Happened

On Windows, StreamWrapper::stream_lock() in pest-plugin-mutate answers every lock request with flock($this->resource, LOCK_SH), whatever operation PHP asked for. A LOCK_EX write made while the wrapper is active, such as file_put_contents($path, $data, LOCK_EX), is therefore taken under a shared lock, and the write then fails with errno=13 Permission denied. The wrapper is active in every mutant child process, so a test that reaches such a write fails in every child. hasFinished() reads a failing child as a kill, so a mutant that no test can kill is scored as killed and the score reads 100%.

Expected: the lock PHP asked for is taken, the write succeeds, and a mutant that no test kills is reported as untested.

How to Reproduce

On Windows (vendor/bin/pest through its .bat proxy), a project of four files, the whole of it:

composer.json:

{
    "require-dev": {
        "pestphp/pest": "5.3.7",
        "pestphp/pest-plugin-mutate": "5.0.2"
    },
    "autoload": { "psr-4": { "Repro\\": "src/" } },
    "config": { "allow-plugins": { "pestphp/pest-plugin": true } }
}

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
    <testsuites><testsuite name="Unit"><directory>tests</directory></testsuite></testsuites>
    <source><include><directory>src</directory></include></source>
</phpunit>

src/Store.php:

<?php

declare(strict_types=1);

namespace Repro;

final class Store
{
    public static function save(string $path, string $data): int|false
    {
        return file_put_contents($path, $data, LOCK_EX);
    }

    public static function triple(int $n): int|float
    {
        return $n * 3;
    }
}

tests/StoreTest.php:

<?php

declare(strict_types=1);

use Repro\Store;

it('saves with an exclusive lock', function (): void {
    $path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'repro-store-'.getmypid().'.txt';

    expect(Store::save($path, 'hello'))->toBe(5)
        ->and(file_get_contents($path))->toBe('hello');

    @unlink($path);

    Store::triple(2); // called but not asserted, so a mutation of it genuinely survives
});

Install and run:

composer install
vendorin\pest.bat tests/StoreTest.php
vendorin\pest.bat --mutate --path=src/Store.php tests/StoreTest.php

The first command passes (Tests: 1 passed (2 assertions)). The second reports:

Mutations: 3 tested
Score:     100.00%

Store::triple() is called by the test and its result is never asserted, so all three mutants of line 16 should survive and be reported as untested. The mutant child fails before it reaches that point. To see the child's own failure, run the test the way a child is run, with PEST_MUTATION_TESTING set to the real path of the mutated file and PEST_MUTATION_FILE to its replacement (an unchanged copy here):

copy src\Store.php override.php
set PEST_MUTATION_TESTING=<full path of src\Store.php>
set PEST_MUTATION_FILE=<full path of override.php>
vendorin\pest.bat tests/StoreTest.php
PHP Notice:  fwrite(): Write of 198 bytes failed with errno=13 Permission denied in ...\src\Support\StreamWrapper.php on line 258
  Failed asserting that false is identical to 5.
  Tests:    1 failed (1 assertions)

The 198 bytes notice is Pest's own test-run-history write, which goes through the same wrapper and fails the same way. The failing assertion is Store::save() returning false for the hello write.

With the one-method change below applied to vendor/pestphp/pest-plugin-mutate/src/Support/StreamWrapper.php, the same child run passes (Tests: 1 passed (2 assertions)) and the --mutate run reports the expected result:

Mutations: 3 untested, 0 tested
Score:     0.00%

Why a shared lock denies the write: on Windows flock() takes a mandatory lock. A plain PHP script with no Pest in it shows the difference on the same machine:

$h = fopen($path, 'c+');
flock($h, LOCK_SH);
fwrite($h, 'hello'); // false, "Write of 5 bytes failed with errno=13 Permission denied"

The same script with LOCK_EX in place of LOCK_SH writes the 5 bytes.

Suggested fix, in src/Support/StreamWrapper.php:

--- a/src/Support/StreamWrapper.php
+++ b/src/Support/StreamWrapper.php
@@ -179,7 +179,12 @@
 
     public function stream_lock(int $operation): bool
     {
-        return flock($this->resource, LOCK_SH);
+        // PHP first calls this with 0 to ask whether the stream supports locking; flock() rejects 0.
+        if (($operation & ~LOCK_NB) === 0) {
+            return true;
+        }
+
+        return flock($this->resource, $operation);
     }
 
     public function stream_metadata(string $path, int $option, mixed $value): bool

PHP calls stream_lock() with 0 first, to ask whether the stream supports locking, so that case is answered true instead of being passed to flock(). Every other operation is forwarded to flock() unchanged.

Sample Repository

None: the four files above are the whole project.

Pest Version

5.3.7 (pestphp/pest-plugin-mutate v5.0.2 at d0c8711, phpunit/phpunit 13.4.7, symfony/process v8.1.10). StreamWrapper.php at tag v5.0.2 and on 5.x (d0c8711) are byte-identical (sha256 dc777db6a8b6fa3b...).

PHP Version

8.4.27 (NTS, x64)

Operation System

Windows (Windows 11 Pro, 10.0.26200). macOS and Linux were not measured.

Notes

Filed here because issues are disabled on pestphp/pest-plugin-mutate.

  • Not the same defect as #2028 (fixed by #2029, merged 2026-11-16), where Pest core's ReadCapture::stream_lock() forwarded the 0 support probe to flock() and threw a ValueError on PHP 8.5. That is a different class in a different wrapper. Here the plugin's wrapper takes the wrong lock for a real LOCK_EX request. It was measured on PHP 8.4.27 only.
  • On a line covered by two or more tests a Windows child also hits #1927; that was kept out of the reproduction above by covering line 16 with one test.
  • Searched on 2026-11-22 over pestphp/pest and pestphp/pest-plugin-mutate, open and closed, issues and pull requests, for stream_lock, LOCK_EX, LOCK_SH, flock, StreamWrapper lock and errno=13: the hits are #1885 (parallel coverage), #1766 (type-coverage cache lock), #1949 (url_stat), and #2028, #2029 and #2039 (TIA's ReadCapture, a different wrapper). None reports this. The search index can lag, so this is not a clearance for anything newer.
Dominant language
PHP
Stars
11.8k
Forks
563
Avg merge
2d 21h
Merged PRs (30d)
39

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pestphp/pest

All issues in pestphp/pest

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.