Skip to content

Commit 424ca42

Browse files
fix: timeouts handling on macOS (#1435)
* ci: run tests on macOS * debug * debug * fix * nobrotli * install brotli * fix * fix * Also registers php.ini if ZEND_MAX_EXECUTION_TIMERS is disabled. * Removes max_execution_time from tests (it gets overwritten on mac) * tiny refacto * fix free * cs --------- Co-authored-by: Alliballibaba <[email protected]>
1 parent a9cf944 commit 424ca42

File tree

5 files changed

+74
-42
lines changed

5 files changed

+74
-42
lines changed

.github/workflows/tests.yaml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ on:
1414
permissions:
1515
contents: read
1616
jobs:
17-
tests:
17+
tests-linux:
18+
name: Tests (Linux, PHP ${{ matrix.php-versions }})
1819
runs-on: ubuntu-latest
1920
strategy:
2021
fail-fast: false
@@ -71,3 +72,39 @@ jobs:
7172
if: matrix.php-versions == '8.4'
7273
with:
7374
version: latest
75+
tests-mac:
76+
name: Tests (macOS, PHP 8.4)
77+
runs-on: macos-latest
78+
env:
79+
GOEXPERIMENT: cgocheck2
80+
HOMEBREW_NO_AUTO_UPDATE: 1
81+
steps:
82+
- uses: actions/checkout@v4
83+
- uses: actions/setup-go@v5
84+
with:
85+
go-version: "1.24"
86+
cache-dependency-path: |
87+
go.sum
88+
caddy/go.sum
89+
- uses: shivammathur/setup-php@v2
90+
with:
91+
php-version: 8.4
92+
ini-file: development
93+
coverage: none
94+
tools: none
95+
env:
96+
phpts: ts
97+
debug: true
98+
- name: Set Set CGO flags
99+
run: |
100+
{
101+
echo "CGO_CFLAGS=-I/opt/homebrew/include/ $(php-config --includes)"
102+
echo "CGO_LDFLAGS=-L/opt/homebrew/lib/ $(php-config --ldflags) $(php-config --libs)"
103+
} >> "${GITHUB_ENV}"
104+
- name: Build
105+
run: go build -tags nowatcher
106+
- name: Run library tests
107+
run: go test -tags nowatcher -race -v ./...
108+
- name: Run Caddy module tests
109+
working-directory: caddy/
110+
run: go test -tags nowatcher,nobadger,nomysql,nopgx -race -v ./...

CONTRIBUTING.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,13 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
149149
3. Enable `tmate` to connect to the container
150150

151151
```patch
152-
-
153-
name: Set CGO flags
152+
- name: Set CGO flags
154153
run: echo "CGO_CFLAGS=$(php-config --includes)" >> "$GITHUB_ENV"
155-
+ -
156-
+ run: |
154+
+ - run: |
157155
+ sudo apt install gdb
158156
+ mkdir -p /home/runner/.config/gdb/
159157
+ printf "set auto-load safe-path /\nhandle SIG34 nostop noprint pass" > /home/runner/.config/gdb/gdbinit
160-
+ -
161-
+ uses: mxschmitt/action-tmate@v3
158+
+ - uses: mxschmitt/action-tmate@v3
162159
```
163160

164161
4. Connect to the container

caddy/caddy_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func TestPHPIniConfiguration(t *testing.T) {
660660
frankenphp {
661661
num_threads 2
662662
worker ../testdata/ini.php 1
663-
php_ini max_execution_time 100
663+
php_ini upload_max_filesize 100M
664664
php_ini memory_limit 10000000
665665
}
666666
}
@@ -673,7 +673,7 @@ func TestPHPIniConfiguration(t *testing.T) {
673673
}
674674
`, "caddyfile")
675675

676-
testSingleIniConfiguration(tester, "max_execution_time", "100")
676+
testSingleIniConfiguration(tester, "upload_max_filesize", "100M")
677677
testSingleIniConfiguration(tester, "memory_limit", "10000000")
678678
}
679679

@@ -688,7 +688,7 @@ func TestPHPIniBlockConfiguration(t *testing.T) {
688688
frankenphp {
689689
num_threads 1
690690
php_ini {
691-
max_execution_time 15
691+
upload_max_filesize 100M
692692
memory_limit 20000000
693693
}
694694
}
@@ -702,7 +702,7 @@ func TestPHPIniBlockConfiguration(t *testing.T) {
702702
}
703703
`, "caddyfile")
704704

705-
testSingleIniConfiguration(tester, "max_execution_time", "15")
705+
testSingleIniConfiguration(tester, "upload_max_filesize", "100M")
706706
testSingleIniConfiguration(tester, "memory_limit", "20000000")
707707
}
708708

frankenphp.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@
3333
ZEND_TSRMLS_CACHE_DEFINE()
3434
#endif
3535

36-
/* Timeouts are currently fundamentally broken with ZTS except on Linux and
37-
* FreeBSD: https://bugs.php.net/bug.php?id=79464 */
38-
#ifndef ZEND_MAX_EXECUTION_TIMERS
39-
static const char HARDCODED_INI[] = "max_execution_time=0\n"
40-
"max_input_time=-1\n\0";
41-
#endif
42-
4336
static const char *MODULES_TO_RELOAD[] = {"filter", "session", NULL};
4437

4538
frankenphp_version frankenphp_get_version() {
@@ -900,25 +893,18 @@ static void *php_main(void *arg) {
900893

901894
sapi_startup(&frankenphp_sapi_module);
902895

903-
#ifndef ZEND_MAX_EXECUTION_TIMERS
904-
#if (PHP_VERSION_ID >= 80300)
905-
frankenphp_sapi_module.ini_entries = HARDCODED_INI;
896+
#ifdef ZEND_MAX_EXECUTION_TIMERS
897+
/* overwrite php.ini with custom user settings */
898+
char *php_ini_overrides = go_get_custom_php_ini(false);
906899
#else
907-
frankenphp_sapi_module.ini_entries = malloc(sizeof(HARDCODED_INI));
908-
if (frankenphp_sapi_module.ini_entries == NULL) {
909-
perror("malloc failed");
910-
exit(EXIT_FAILURE);
911-
}
912-
memcpy(frankenphp_sapi_module.ini_entries, HARDCODED_INI,
913-
sizeof(HARDCODED_INI));
900+
/* overwrite php.ini with custom user settings and disable
901+
* max_execution_timers */
902+
char *php_ini_overrides = go_get_custom_php_ini(true);
914903
#endif
915-
#else
916-
/* overwrite php.ini with custom user settings */
917-
char *php_ini_overrides = go_get_custom_php_ini();
904+
918905
if (php_ini_overrides != NULL) {
919906
frankenphp_sapi_module.ini_entries = php_ini_overrides;
920907
}
921-
#endif
922908

923909
frankenphp_sapi_module.startup(&frankenphp_sapi_module);
924910

@@ -938,13 +924,13 @@ static void *php_main(void *arg) {
938924
tsrm_shutdown();
939925
#endif
940926

941-
#if (PHP_VERSION_ID < 80300)
942927
if (frankenphp_sapi_module.ini_entries) {
943-
free(frankenphp_sapi_module.ini_entries);
928+
free((char *)frankenphp_sapi_module.ini_entries);
944929
frankenphp_sapi_module.ini_entries = NULL;
945930
}
946-
#endif
931+
947932
go_frankenphp_shutdown_main_thread();
933+
948934
return NULL;
949935
}
950936

phpmainthread.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package frankenphp
88
// #include "frankenphp.h"
99
import "C"
1010
import (
11-
"fmt"
11+
"strings"
1212
"sync"
1313

1414
"github.com/dunglas/frankenphp/internal/memory"
@@ -191,16 +191,28 @@ func go_frankenphp_shutdown_main_thread() {
191191
}
192192

193193
//export go_get_custom_php_ini
194-
func go_get_custom_php_ini() *C.char {
194+
func go_get_custom_php_ini(disableTimeouts C.bool) *C.char {
195195
if mainThread.phpIni == nil {
196-
return nil
196+
mainThread.phpIni = make(map[string]string)
197197
}
198198

199-
// pass the php.ini overrides to PHP before startup
199+
// Timeouts are currently fundamentally broken
200+
// with ZTS except on Linux and FreeBSD: https://bugs.php.net/bug.php?id=79464
201+
// Disable timeouts if ZEND_MAX_EXECUTION_TIMERS is not supported
202+
if disableTimeouts {
203+
mainThread.phpIni["max_execution_time"] = "0"
204+
mainThread.phpIni["max_input_time"] = "-1"
205+
}
206+
207+
// Pass the php.ini overrides to PHP before startup
200208
// TODO: if needed this would also be possible on a per-thread basis
201-
overrides := ""
209+
var overrides strings.Builder
202210
for k, v := range mainThread.phpIni {
203-
overrides += fmt.Sprintf("%s=%s\n", k, v)
211+
overrides.WriteString(k)
212+
overrides.WriteByte('=')
213+
overrides.WriteString(v)
214+
overrides.WriteByte('\n')
204215
}
205-
return C.CString(overrides)
216+
217+
return C.CString(overrides.String())
206218
}

0 commit comments

Comments
 (0)