Skip to content

Commit 4cf0a57

Browse files
authored
fix(native): exclude same-process advisory lock owners without relying on flock (#442)
WP_Markdown_Native_Advisory_Locks used flock() as its only exclusion. php-wasm (WordPress Playground) grants every flock in one process, so two logical connections in the same request both acquired the same GET_LOCK name. Record lock ownership in a process-wide registry keyed by lock path before taking the flock; a lock held by another in-process owner is contended (GET_LOCK 0, RELEASE_LOCK 0). flock() still excludes other processes. Surfaced by Extra-Chill/data-machine-events#887 under mdi-native, where open_connection() contention passed on native PHP but not in Playground.
1 parent a061fc8 commit 4cf0a57

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎inc/native/class-wp-markdown-native-advisory-locks.php‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,27 @@ final class WP_Markdown_Native_Advisory_Locks {
1414
/** @var array<string,array{handle:resource,count:int,path:string}> */
1515
private array $locks = array();
1616

17-
public function __construct( private readonly string $state_root ) {}
17+
/**
18+
* Lock files owned by some instance in this PHP process, keyed by path.
19+
*
20+
* flock() alone is not a same-process exclusion guarantee: php-wasm
21+
* (WordPress Playground) grants every handle in one process, so two logical
22+
* connections in one request both "acquired" the same lock. Ownership is
23+
* therefore recorded in-process first; flock() still excludes other processes.
24+
*
25+
* @var array<string,int>
26+
*/
27+
private static array $process_owners = array();
28+
private static int $next_owner = 0;
29+
private readonly int $owner;
30+
31+
public function __construct( private readonly string $state_root ) {
32+
$this->owner = ++self::$next_owner;
33+
}
34+
35+
private function held_by_other_in_process( string $path ): bool {
36+
return isset( self::$process_owners[ $path ] ) && self::$process_owners[ $path ] !== $this->owner;
37+
}
1838

1939
/** Acquire a named lock, waiting no longer than the requested bounded timeout. */
2040
public function acquire( string $name, float $timeout ): bool {
@@ -34,8 +54,9 @@ public function acquire( string $name, float $timeout ): bool {
3454
$wait = $timeout * 1000000;
3555
$deadline = hrtime( true ) + (int) ( $wait * 1000 );
3656
do {
37-
if ( flock( $handle, LOCK_EX | LOCK_NB ) ) {
57+
if ( ! $this->held_by_other_in_process( $path ) && flock( $handle, LOCK_EX | LOCK_NB ) ) {
3858
$this->locks[ $name ] = array( 'handle' => $handle, 'count' => 1, 'path' => $path );
59+
self::$process_owners[ $path ] = $this->owner;
3960
return true;
4061
}
4162
if ( 0.0 === $wait || hrtime( true ) >= $deadline ) {
@@ -50,6 +71,9 @@ public function acquire( string $name, float $timeout ): bool {
5071
/** @return int|null One when released, zero when held by another connection, null when absent. */
5172
public function release( string $name ): ?int {
5273
if ( ! isset( $this->locks[ $name ] ) ) {
74+
if ( $this->held_by_other_in_process( $this->path( $name ) ) ) {
75+
return 0;
76+
}
5377
$handle = @fopen( $this->path( $name ), 'c+b' );
5478
if ( false === $handle ) {
5579
return null;
@@ -67,6 +91,9 @@ public function release( string $name ): ?int {
6791
}
6892
$lock = $this->locks[ $name ];
6993
unset( $this->locks[ $name ] );
94+
if ( ( self::$process_owners[ $lock['path'] ] ?? null ) === $this->owner ) {
95+
unset( self::$process_owners[ $lock['path'] ] );
96+
}
7097
flock( $lock['handle'], LOCK_UN );
7198
fclose( $lock['handle'] );
7299
return 1;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Advisory locks must exclude same-process owners even when flock() grants
4+
* every handle, as php-wasm (WordPress Playground) does within one process.
5+
*/
6+
7+
declare( strict_types=1 );
8+
9+
define( 'ABSPATH', __DIR__ . '/' );
10+
require_once __DIR__ . '/../inc/native/class-wp-markdown-native-advisory-locks.php';
11+
12+
/** Passthrough file wrapper whose stream_lock() always succeeds, like php-wasm. */
13+
final class MDI_Permissive_Flock_Stream {
14+
/** @var resource|null */
15+
public $context;
16+
/** @var resource */
17+
private $handle;
18+
19+
private static function real( string $path ): string {
20+
return substr( $path, strlen( 'mdi-permissive://' ) );
21+
}
22+
public function stream_open( string $path, string $mode ): bool {
23+
$handle = fopen( self::real( $path ), $mode );
24+
if ( false === $handle ) {
25+
return false;
26+
}
27+
$this->handle = $handle;
28+
return true;
29+
}
30+
public function stream_lock( int $operation ): bool {
31+
unset( $operation );
32+
return true;
33+
}
34+
public function stream_close(): void { fclose( $this->handle ); }
35+
public function stream_read( int $count ): string|false { return fread( $this->handle, $count ); }
36+
public function stream_write( string $data ): int|false { return fwrite( $this->handle, $data ); }
37+
public function stream_eof(): bool { return feof( $this->handle ); }
38+
public function stream_stat(): array|false { return fstat( $this->handle ); }
39+
public function url_stat( string $path, int $flags ): array|false {
40+
$real = self::real( $path );
41+
return file_exists( $real ) ? stat( $real ) : false;
42+
}
43+
public function mkdir( string $path, int $mode, int $options ): bool {
44+
return mkdir( self::real( $path ), $mode, (bool) ( $options & STREAM_MKDIR_RECURSIVE ) );
45+
}
46+
}
47+
stream_wrapper_register( 'mdi-permissive', MDI_Permissive_Flock_Stream::class );
48+
49+
$root = 'mdi-permissive://' . sys_get_temp_dir() . '/mdi-permissive-flock-' . bin2hex( random_bytes( 6 ) );
50+
$first = new WP_Markdown_Native_Advisory_Locks( $root );
51+
$second = new WP_Markdown_Native_Advisory_Locks( $root );
52+
53+
$assertions = array(
54+
'the first owner acquires' => true === $first->acquire( 'venue-lock', 0 ),
55+
'a same-process second owner is excluded' => false === $second->acquire( 'venue-lock', 0 ),
56+
'a same-process non-owner release reports 0' => 0 === $second->release( 'venue-lock' ),
57+
'the owner remains reentrant' => true === $first->acquire( 'venue-lock', 0 ) && 1 === $first->release( 'venue-lock' ),
58+
'still held after one reentrant release' => false === $second->acquire( 'venue-lock', 0 ),
59+
'final release transfers ownership' => 1 === $first->release( 'venue-lock' ) && true === $second->acquire( 'venue-lock', 0 ),
60+
'close releases in-process ownership' => ( static function () use ( $first, $second ): bool { $second->close(); return true === $first->acquire( 'venue-lock', 0 ); } )(),
61+
'unrelated names do not contend' => true === $second->acquire( 'other-lock', 0 ),
62+
);
63+
$passed = ! in_array( false, $assertions, true );
64+
fwrite( $passed ? STDOUT : STDERR, json_encode( array( 'assertions' => $assertions, 'passed' => $passed ), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ) . "\n" );
65+
exit( $passed ? 0 : 1 );

0 commit comments

Comments
 (0)