Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@
"extra": {
"downloads": {
"pact-ffi-headers": {
"version": "0.5.8",
"version": "0.5.10",
"url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h",
"path": "bin/pact-ffi-headers/pact.h"
},
"pact-ffi-lib": {
"version": "0.5.8",
"version": "0.5.10",
"variables": {
"{$prefix}": "PHP_OS_FAMILY === 'Windows' ? 'pact_ffi' : 'libpact_ffi'",
"{$os}": "PHP_OS === 'Darwin' ? 'macos' : strtolower(PHP_OS_FAMILY)",
Expand Down
22 changes: 21 additions & 1 deletion docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ $config->setLogLevel('DEBUG');

* Pros
* Simple
* Support plugins (e.g. csv, gRPC)
* Cons
* Do not support plugins (e.g. csv, gRPC)
* Only single sink (stdout)

### 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.
Comment on lines +108 to +126

@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

8 changes: 6 additions & 2 deletions src/PhpPact/FFI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use PhpPact\FFI\Model\ArrayData;
use PhpPact\FFI\Model\BinaryData;
use PhpPact\FFI\Model\Result;
use PhpPact\Log\Enum\LogLevel;
use PhpPact\Log\Logger;
use PhpPact\Log\Model\Stdout;
use PhpPact\Standalone\Installer\Model\Scripts;

class Client implements ClientInterface
Expand Down Expand Up @@ -224,8 +227,9 @@ public function withSpecification(int $pact, int $specification): bool

public function initWithLogLevel(string $logLevel): void
{
$method = 'pactffi_init_with_log_level';
$this->call($method, $logLevel);
$logger = Logger::instance($this);
$logger->attach(new Stdout(LogLevel::tryFrom(\strtoupper($logLevel)) ?? LogLevel::INFO));
$logger->apply();
}

public function pactHandleWriteFile(int $pact, string $directory, bool $overwrite): int
Expand Down
123 changes: 123 additions & 0 deletions tests/PhpPact/FFI/ClientLoggingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace PhpPactTest\FFI;

use PhpPact\FFI\Client;
use PhpPact\Log\Enum\LogLevel;
use PhpPact\Log\Exception\LoggerApplyException;
use PhpPact\Log\Exception\LoggerAttachSinkException;
use PhpPact\Log\Logger;
use PhpPact\Log\Model\File;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

class ClientLoggingTest extends TestCase
{
private Client&MockObject $client;

public function setUp(): void
{
Logger::tearDown();
$this->client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->onlyMethods([
'loggerInit',
'loggerAttachSink',
'loggerApply',
'getLevelFilterTrace',
'getLevelFilterDebug',
'getLevelFilterInfo',
'getLevelFilterWarn',
'getLevelFilterError',
'getLevelFilterOff',
])
->getMock();
}

public function tearDown(): void
{
Logger::tearDown();
}

#[TestWith(['TRACE', 'getLevelFilterTrace'])]
#[TestWith(['debug', 'getLevelFilterDebug'])]
#[TestWith(['INFO', 'getLevelFilterInfo'])]
#[TestWith(['WARN', 'getLevelFilterWarn'])]
#[TestWith(['ERROR', 'getLevelFilterError'])]
#[TestWith(['OFF', 'getLevelFilterOff'])]
#[TestWith(['NONE', 'getLevelFilterOff'])]
#[TestWith(['unknown', 'getLevelFilterInfo'])]
public function testInitWithLogLevelUsesStdout(string $level, string $levelMethod): void
{
$this->client->expects($this->once())->method('loggerInit');
$this->client->expects($this->once())->method($levelMethod)->willReturn(3);
$this->client->expects($this->once())->method('loggerAttachSink')->with('stdout', 3)->willReturn(0);
$this->client->expects($this->once())->method('loggerApply')->willReturn(0);

$this->client->initWithLogLevel($level);
$this->client->initWithLogLevel($level);
}

public function testInitWithLogLevelPreservesAppliedFileLogger(): void
{
$this->client->expects($this->once())->method('getLevelFilterDebug')->willReturn(4);
$this->client->expects($this->once())->method('loggerAttachSink')->with('file pact.log', 4)->willReturn(0);
$this->client->expects($this->once())->method('loggerApply')->willReturn(0);
$logger = Logger::instance($this->client);
$logger->attach(new File('pact.log', LogLevel::DEBUG));
$logger->apply();

$this->client->initWithLogLevel('DEBUG');
}

public function testInitWithLogLevelReportsSinkFailure(): void
{
$this->client->method('getLevelFilterDebug')->willReturn(4);
$this->client->method('loggerAttachSink')->with('stdout', 4)->willReturn(-4);
$this->client->expects($this->never())->method('loggerApply');
$this->expectException(LoggerAttachSinkException::class);

$this->client->initWithLogLevel('DEBUG');
}

public function testInitWithLogLevelReportsApplyFailure(): void
{
$this->client->method('loggerAttachSink')->willReturn(0);
$this->client->method('loggerApply')->willReturn(-1);
$this->expectException(LoggerApplyException::class);

$this->client->initWithLogLevel('DEBUG');
}

public function testNativeDebugLogsDoNotFillStderr(): void
{
$code = <<<'PHP'
require $argv[1];
$client = new PhpPact\FFI\Client();
$client->initWithLogLevel('DEBUG');
$pact = $client->newPact('logging-consumer', 'logging-provider');
$interaction = $client->newInteraction($pact, 'logging');
for ($index = 0; $index < 64; $index++) {
if (!$client->withHeaderV2($interaction, $client->getInteractionPartRequest(), 'Accept', 0, 'image/jpeg')) {
throw new RuntimeException('Could not configure the logging interaction');
}
}
$client->freePactHandle($pact);
PHP;
$process = new Process(
[PHP_BINARY, '-d', 'ffi.enable=true', '-r', $code, __DIR__ . '/../../../vendor/autoload.php'],
null,
['PACT_DO_NOT_TRACK' => 'true'],
null,
10
);
$process->run();

$this->assertSame(0, $process->getExitCode(), $process->getErrorOutput());
$this->assertSame('', $process->getErrorOutput());
$this->assertStringContainsString('DEBUG', $process->getOutput());
$this->assertGreaterThan(4096, strlen($process->getOutput()));
}
}
2 changes: 2 additions & 0 deletions tests/PhpPact/FFI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpPact\FFI\Client;
use PhpPact\FFI\ClientInterface;
use PhpPact\FFI\Model\BinaryData;
use PhpPact\Log\Logger;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -143,6 +144,7 @@ public function testWithSpecification(): void
public function testInitWithLogLevel(): void
{
$this->client->initWithLogLevel('test');
Logger::tearDown();
$this->expectNotToPerformAssertions();
}

Expand Down
Loading