Skip to content

Commit 72c5727

Browse files
committed
#50: * fix bailout tests for PHP JIT.
JIT PHP in tracing mode can compile functions on demand. When memory runs out, JIT crashes with an error because it tries to compile a closure. This code attempts to work around the issue so that the test runs correctly. So we use: $function = function(bool $out = true) { if($out) echo "Shutdown function called\n"; }; $function(false); register_shutdown_function($function);
1 parent 13cff19 commit 72c5727

7 files changed

+56
-27
lines changed

run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ else
1616
TEST_PATH="$BASE_PATH/$1"
1717
fi
1818

19-
"$PHP_EXECUTABLE" "$RUN_TESTS_PATH" --show-diff -p "$PHP_EXECUTABLE" "$TEST_PATH"
19+
"$PHP_EXECUTABLE" "$RUN_TESTS_PATH" -d zend_extension=opcache.so -d opcache.enable_cli=1 --show-diff -p "$PHP_EXECUTABLE" "$TEST_PATH"

tests/bailout/003-stack-overflow-simple.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ function deepRecursion($depth = 0) {
1616
return deepRecursion($depth + 1);
1717
}
1818

19-
register_shutdown_function(function() {
20-
echo "Shutdown function called\n";
21-
});
19+
$function = function(bool $out = true) {
20+
if($out) echo "Shutdown function called\n";
21+
};
22+
23+
$function(false);
24+
25+
register_shutdown_function($function);
2226

2327
echo "Before spawn\n";
2428

@@ -39,4 +43,4 @@ Before stack overflow
3943
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
4044

4145
Warning: Graceful shutdown mode was started in %s on line %d
42-
Shutdown function called
46+
Shutdown function called

tests/bailout/004-stack-overflow-nested.phpt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ function deepRecursion($depth = 0) {
1616
return deepRecursion($depth + 1);
1717
}
1818

19-
register_shutdown_function(function() {
20-
echo "Shutdown function called\n";
21-
});
19+
$function = function(bool $out = true) {
20+
if($out) echo "Shutdown function called\n";
21+
};
22+
23+
$function(false);
24+
25+
register_shutdown_function($function);
2226

2327
echo "Before spawn\n";
2428

2529
spawn(function() {
2630
echo "Outer async started\n";
27-
31+
2832
spawn(function() {
2933
echo "Inner async started\n";
3034
deepRecursion();
3135
echo "Inner async after stack overflow (should not reach)\n";
3236
});
33-
37+
3438
echo "Outer async continues\n";
3539
});
3640

@@ -47,4 +51,4 @@ Inner async started
4751
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
4852

4953
Warning: Graceful shutdown mode was started in %s on line %d
50-
Shutdown function called
54+
Shutdown function called

tests/bailout/006-memory-exhaustion-multiple-coroutines.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ memory_limit=2M
1414

1515
use function Async\spawn;
1616

17-
register_shutdown_function(function() {
18-
echo "Shutdown function called\n";
19-
});
17+
$function = function(bool $out = true) {
18+
if($out) echo "Shutdown function called\n";
19+
};
20+
21+
$function(false);
22+
23+
register_shutdown_function($function);
2024

2125
echo "Before spawn\n";
2226

@@ -50,4 +54,4 @@ Coroutine 2 started
5054
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
5155

5256
Warning: Graceful shutdown mode was started in %s on line %d
53-
Shutdown function called
57+
Shutdown function called

tests/bailout/007-stack-overflow-with-suspend.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ function deepRecursionWithSuspend($depth = 0) {
2020
return deepRecursionWithSuspend($depth + 1);
2121
}
2222

23-
register_shutdown_function(function() {
24-
echo "Shutdown function called\n";
25-
});
23+
$function = function(bool $out = true) {
24+
if($out) echo "Shutdown function called\n";
25+
};
26+
27+
$function(false);
28+
29+
register_shutdown_function($function);
2630

2731
echo "Before spawn\n";
2832

@@ -48,4 +52,4 @@ Other coroutine running
4852
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
4953

5054
Warning: Graceful shutdown mode was started in %s on line %d
51-
Shutdown function called
55+
Shutdown function called

tests/bailout/011-stack-overflow-during-await.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ function deepRecursion($depth = 0) {
1717
return deepRecursion($depth + 1);
1818
}
1919

20-
register_shutdown_function(function() {
21-
echo "Shutdown function called\n";
22-
});
20+
$function = function(bool $out = true) {
21+
if($out) echo "Shutdown function called\n";
22+
};
23+
24+
$function(false);
25+
26+
register_shutdown_function($function);
2327

2428
echo "Before spawn\n";
2529

@@ -43,4 +47,4 @@ Coroutine started
4347
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
4448

4549
Warning: Graceful shutdown mode was started in %s on line %d
46-
Shutdown function called
50+
Shutdown function called

tests/bailout/014-stack-overflow-with-finally.phpt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ echo "Before scope\n";
2525

2626
$scope = new Scope();
2727

28-
$scope->onFinally(function() {
29-
echo "Finally handler executed\n";
30-
});
28+
$finally = function($x = false, $out = true) {
29+
if($out) {
30+
echo "Finally handler executed\n";
31+
}
32+
};
33+
34+
// JIT PHP in tracing mode can compile functions on demand. When memory runs out,
35+
// JIT crashes with an error because it tries to compile a closure.
36+
// This code attempts to work around the issue so that the test runs correctly.
37+
$finally(false, false);
38+
39+
$scope->onFinally($finally);
3140

3241
$coroutine = $scope->spawn(function() {
3342
echo "Before stack overflow\n";
@@ -48,4 +57,4 @@ Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d by
4857

4958
Warning: Graceful shutdown mode was started in %s on line %d
5059
Shutdown function called
51-
Finally handler executed
60+
Finally handler executed

0 commit comments

Comments
 (0)