Skip to content

fix: upgrade pact-ffi to 0.5.9 without Windows logging hangs - #3

Closed
YOU54F wants to merge 3 commits into
masterfrom
agents/ci-issues-ffi-upgrade-windows-0510
Closed

YOU54F wants to merge 3 commits into
masterfrom
agents/ci-issues-ffi-upgrade-windows-0510

Conversation

@YOU54F

@YOU54F YOU54F commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • Bump both the Pact FFI header and native library downloads from 0.5.8 to 0.5.9.
  • Preserve config-based logging on stdout using Pact-PHP's existing logger singleton and explicit stdout sink.
  • Keep DEBUG logging and Windows --process-isolation; do not suppress logs or remove test isolation.
  • Respect an already-applied logger, including a PHPUnit-extension file sink, and surface sink/application failures through the existing logger exceptions.
  • Add logging regression tests and document the investigation in docs/troubleshooting.md.

This PR targets YOU54F/pact-php:master, not pact-foundation/pact-php.

Why Windows hangs

Investigated pact-foundation#829 and pact-foundation#830. Both Windows jobs stall in the very first binary consumer test after PHPUnit prints its configuration.

Pact FFI 0.5.9 changed pactffi_init_with_log_level from its default stdout writer to an explicit stderr writer:

https://github.com/pact-foundation/pact-reference/blob/libpact_ffi-v0.5.9/rust/pact_ffi/src/lib.rs#L121

PHPUnit 11.5.57's process-isolation runner drains stdout to EOF before reading stderr:

https://github.com/sebastianbergmann/phpunit/blob/11.5.57/src/Util/PHP/JobRunner.php#L174

When DEBUG output fills the stderr pipe, the child blocks writing logs while PHPUnit waits for stdout EOF. This is a pipe deadlock, not a slow test.

The investigation reproduced the mechanism with native Pact calls, a bounded 4 KiB stderr pipe, and stdout-first reading: 0.5.8 completed, while 0.5.9 blocked until stderr was drained. A successful native binary-consumer probe moved about 6 KiB of output from stdout in 0.5.8 to stderr in 0.5.9.

The fix explicitly selects stdout through the existing logger API, preserving the documented pre-upgrade destination on all platforms. Repeated initialization is idempotent, and the first applied logger retains its destination and level.

Why not 0.5.10

The invalid native boolean returns are deliberately deferred to a separate upstream investigation/fix.

Direct Python ctypes probes against the published Linux x86_64 artifacts, bypassing PHP entirely, found:

Native false-result path 0.5.8 0.5.9 0.5.10
pactffi_mock_server_matched(1234) for a nonexistent server byte 0 byte 0 byte 2
pactffi_with_body(0, Request, ...) for an invalid handle with a valid enum byte 0 byte 0 byte 2

PHP FFI and Python ctypes interpret byte 2 as true. This accounts for the 11 false-result assertion failures and the V3/V4 compatibility failures taking the matched-server path for port 0. ARM validation does not exhibit this behavior.

The release compiler changed from Rust 1.98.1 to 1.99.0, which is an investigation lead, not a proven cause. This PR does not normalize invalid return bytes or weaken assertions.

Validation

Local validation on macOS ARM / PHP 8.5.10 with Pact FFI 0.5.9:

  • FFI and logging tests: 133 tests, 235 assertions passed.
  • Full unit suite before the final native-output regression test was added: 1,006 tests, 5,798 assertions passed, with the existing 10 warnings and 4 PHPUnit deprecations also seen in upstream CI.
  • Native-output regression test: emits more than 4 KiB of DEBUG logs on stdout, no stderr output, and finishes within a 10-second subprocess timeout.
  • Binary consumer and provider examples with --process-isolation: passed.
  • PHPStan: no errors.
  • PHP CS Fixer dry runs for changed PHP files: no changes required.
  • Composer manifest validation and git diff --check: passed.

Actual Windows verification is left to this fork's CI; the local machine is macOS.

YOU54F and others added 2 commits October 7, 2026 17:26
Preserve config-based logging on stdout through the existing logger singleton so PHPUnit process isolation cannot deadlock on a full stderr pipe. Keep DEBUG output and respect an already-applied logger.\n\nAdd logging regression coverage and document why the 0.5.10 invalid native boolean returns remain out of scope.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Exclude test-generated pact changes from the FFI upgrade.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coveralls

coveralls commented Oct 7, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 37660029614

Coverage increased (+0.003%) to 92.475%

Details

  • Coverage increased (+0.003%) from the base build.
  • Patch coverage: 3 of 3 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2857
Covered Lines: 2642
Line Coverage: 92.47%
Coverage Strength: 114.48 hits per line

💛 - Coveralls

Comment thread docs/troubleshooting.md
Comment on lines +108 to +126
### Windows process isolation and Pact FFI 0.5.9

Pact FFI 0.5.9 changed `pactffi_init_with_log_level` to write to stderr instead of
stdout. PHPUnit's process-isolation runner reads the child process's stdout to
EOF before draining stderr. On Windows, DEBUG logs can fill the stderr pipe:
the child blocks writing logs while PHPUnit waits for the child to finish.

Pact-PHP uses its existing logger singleton and an explicit stdout sink for
config-based logging. This preserves the previous logging destination and DEBUG
output without removing `--process-isolation`. An already-applied logger, such as
the file sink configured through the PHPUnit extension, is left unchanged.
The first applied logger determines the destination and level for the process.

The upgrade intentionally stops at 0.5.9. The published 0.5.10 Linux x86_64 library
was observed returning byte `2` instead of `0` on false-result paths, including
`pactffi_mock_server_matched` for a nonexistent server and `pactffi_with_body` for
an invalid handle. Both PHP FFI and Python ctypes interpret this as true.
Addressing those invalid native boolean returns is a separate upstream task;
the PHP assertions must not be weakened to hide them.

@tienvx tienvx Oct 10, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think only 'Windows process isolation' worth mentioning. The 'Pact FFI 0.5.9 ' is only benefit maintainers of this library, I think users of this library doesn't want to know about this details. We already upgraded to 0.5.10 anyway. The description of this pull request already explain about it, so I'm fine with it.

So, I suggest to change to something like this:

Suggested change
### Windows process isolation and Pact FFI 0.5.9
Pact FFI 0.5.9 changed `pactffi_init_with_log_level` to write to stderr instead of
stdout. PHPUnit's process-isolation runner reads the child process's stdout to
EOF before draining stderr. On Windows, DEBUG logs can fill the stderr pipe:
the child blocks writing logs while PHPUnit waits for the child to finish.
Pact-PHP uses its existing logger singleton and an explicit stdout sink for
config-based logging. This preserves the previous logging destination and DEBUG
output without removing `--process-isolation`. An already-applied logger, such as
the file sink configured through the PHPUnit extension, is left unchanged.
The first applied logger determines the destination and level for the process.
The upgrade intentionally stops at 0.5.9. The published 0.5.10 Linux x86_64 library
was observed returning byte `2` instead of `0` on false-result paths, including
`pactffi_mock_server_matched` for a nonexistent server and `pactffi_with_body` for
an invalid handle. Both PHP FFI and Python ctypes interpret this as true.
Addressing those invalid native boolean returns is a separate upstream task;
the PHP assertions must not be weakened to hide them.
### Windows process isolation
On Windows, running `composer run-examples` can randomly cause this error:
> Script cd example/protobuf-sync-message/consumer && phpunit handling the run-example:protobuf-sync-message event returned with error code -1073741819
Script composer run-example:protobuf-sync-message handling the run-examples event returned with error code -1073741819
According to [Microsoft](https://learn.microsoft.com/en-us/answers/questions/379441/error-code-exited-with-code-1073741819#answer-379350):
> it indicates that your program was terminated due to an access violation. This error can occur for a variety of reasons, including de-referencing a NULL pointer or referencing an invalid address.
The solution is `composer run-examples:windows`. It attach option `--process-isolation`, which fixed this error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Oh, now I realize that this pull request is not for https://github.com/pact-foundation/pact-php. Let me cherry pick some code to pact-foundation#829

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

no need to cherry pick, might not be the right fix. thanks for looking Tien

@YOU54F YOU54F closed this Oct 21, 2026
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.

3 participants