Skip to content

Commit bf6e653

Browse files
fix: exit() and dd() support in worker mode (#1946)
* Verifies exit behavior. * formatting * Checks for actual exit. * Fixes test. * Fixes test. * Update testdata/dd.php Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent fb1f468 commit bf6e653

3 files changed

Lines changed: 65 additions & 5 deletions

File tree

caddy/caddy_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,3 +1423,27 @@ func TestWorkerMatchDirectiveWithoutFileServer(t *testing.T) {
14231423
// the request should completely fall through the php_server module
14241424
tester.AssertGetResponse("http://localhost:"+testPort+"/static.txt", http.StatusNotFound, "Request falls through")
14251425
}
1426+
1427+
func TestDd(t *testing.T) {
1428+
tester := caddytest.NewTester(t)
1429+
tester.InitServer(`
1430+
{
1431+
skip_install_trust
1432+
admin localhost:2999
1433+
}
1434+
1435+
http://localhost:`+testPort+` {
1436+
php {
1437+
worker ../testdata/dd.php 1 {
1438+
match *
1439+
}
1440+
}
1441+
`, "caddyfile")
1442+
1443+
// simulate Symfony's dd()
1444+
tester.AssertGetResponse(
1445+
"http://localhost:"+testPort+"/some-path?output=dump123",
1446+
http.StatusInternalServerError,
1447+
"dump123",
1448+
)
1449+
}

frankenphp.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,16 @@ PHP_FUNCTION(frankenphp_handle_request) {
464464

465465
/*
466466
* If an exception occurred, print the message to the client before
467-
* closing the connection and bailout.
467+
* closing the connection.
468468
*/
469-
if (EG(exception) && !zend_is_unwind_exit(EG(exception)) &&
470-
!zend_is_graceful_exit(EG(exception))) {
471-
zend_exception_error(EG(exception), E_ERROR);
472-
zend_bailout();
469+
if (EG(exception)) {
470+
if (!zend_is_unwind_exit(EG(exception)) &&
471+
!zend_is_graceful_exit(EG(exception))) {
472+
zend_exception_error(EG(exception), E_ERROR);
473+
} else {
474+
/* exit() will jump directly to after php_execute_script */
475+
zend_bailout();
476+
}
473477
}
474478

475479
frankenphp_worker_request_shutdown();

testdata/dd.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
// simulate Symfony's dd() behavior
4+
// see https://github.com/symfony/http-kernel/blob/7.3/DataCollector/DumpDataCollector.php#L216
5+
class Dumper
6+
{
7+
private string $message;
8+
9+
public function dump(string $message): void
10+
{
11+
http_response_code(500);
12+
$this->message = $message;
13+
}
14+
15+
public function __destruct()
16+
{
17+
if (isset($this->message)) {
18+
echo $this->message;
19+
}
20+
}
21+
}
22+
23+
$dumper = new Dumper();
24+
25+
while (frankenphp_handle_request(function () use ($dumper) {
26+
$dumper->dump($_GET['output'] ?? '');
27+
exit(1);
28+
})) {
29+
// keep handling requests
30+
}
31+
32+
echo "we should never reach here\n";

0 commit comments

Comments
 (0)