Skip to content

Commit cd48160

Browse files
committed
fix: ensure there is always a free thread for non-worker scripts
1 parent 6a6dda5 commit cd48160

5 files changed

Lines changed: 23 additions & 10 deletions

File tree

frankenphp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,8 @@ static void *manager_thread(void *arg) {
558558
free(arg);
559559

560560
uintptr_t rh;
561-
while ((rh = go_fetch_request())) {
561+
while ((rh = go_fetch_request()))
562562
thpool_add_work(thpool, go_execute_script, (void *) rh);
563-
}
564563

565564
/* channel closed, shutdown gracefully */
566565
thpool_wait(thpool);

frankenphp.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
// [FrankenPHP app server]: https://frankenphp.dev
66
package frankenphp
77

8+
//go:generate rm -Rf C-Thread-Pool/
89
//go:generate git clone --branch=feat/mac-os-compat --depth=1 git@github.com:dunglas/C-Thread-Pool.git
9-
//go:generate rm -rf C-Thread-Pool/.git C-Thread-Pool/.circleci C-Thread-Pool/docs C-Thread-Pool/tests
10+
//go:generate rm -Rf C-Thread-Pool/.git C-Thread-Pool/.circleci C-Thread-Pool/docs C-Thread-Pool/tests
1011

1112
// #cgo CFLAGS: -Wall
1213
// #cgo CFLAGS: -I/usr/local/include/php -I/usr/local/include/php/Zend -I/usr/local/include/php/TSRM -I/usr/local/include/php/main
@@ -45,6 +46,7 @@ var (
4546
AlreaydStartedError = errors.New("FrankenPHP is already started")
4647
InvalidPHPVersionError = errors.New("FrankenPHP is only compatible with PHP 8.2+")
4748
ZendSignalsError = errors.New("Zend Signals are enabled, recompile PHP with --disable-zend-signals")
49+
NotEnoughThreads = errors.New("the number of threads must be superior to the number of workers")
4850
MainThreadCreationError = errors.New("error creating the main thread")
4951
RequestContextCreationError = errors.New("error during request context creation")
5052
RequestStartupError = errors.New("error during PHP request startup")
@@ -221,16 +223,18 @@ func Init(options ...Option) error {
221223
opt.workers[i].num = numCPU
222224
}
223225

224-
numWorkers += w.num
226+
numWorkers += opt.workers[i].num
225227
}
226228

227229
if opt.numThreads <= 0 {
228230
if numWorkers >= numCPU {
229-
// Keep a free thread to handle requests not handled by a worker
230-
opt.numThreads = numCPU + 1
231+
// Start at least as many threads as workers, and keep a free thread to handle requests in non-worker mode
232+
opt.numThreads = numWorkers + 1
231233
} else {
232234
opt.numThreads = numCPU
233235
}
236+
} else if opt.numThreads <= numWorkers {
237+
return NotEnoughThreads
234238
}
235239

236240
switch C.frankenphp_check_version() {

frankenphp_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *
3939
if opts == nil {
4040
opts = &testOptions{}
4141
}
42-
if opts.nbWorkers == 0 {
43-
opts.nbWorkers = 2
44-
}
4542
if opts.nbParrallelRequests == 0 {
4643
opts.nbParrallelRequests = 100
4744
}

testdata/Caddyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
debug
33
frankenphp {
4-
#worker ./phpinfo.php
4+
worker ./phpinfo.php
55
}
66
}
77

worker_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ func TestWorkerDie(t *testing.T) {
4949
}, &testOptions{workerScript: "die.php", nbWorkers: 1, nbParrallelRequests: 10})
5050
}
5151

52+
func TestNonWorkerModeAlwaysWorks(t *testing.T) {
53+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
54+
req := httptest.NewRequest("GET", "http://example.com/index.php", nil)
55+
w := httptest.NewRecorder()
56+
handler(w, req)
57+
58+
resp := w.Result()
59+
body, _ := io.ReadAll(resp.Body)
60+
61+
assert.Contains(t, string(body), "I am by birth a Genevese")
62+
}, &testOptions{workerScript: "phpinfo.php"})
63+
}
64+
5265
func ExampleServeHTTP_workers() {
5366
if err := frankenphp.Init(
5467
frankenphp.WithWorkers("worker1.php", 4),

0 commit comments

Comments
 (0)