Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Remove all Cgo handles #1071

Closed
wants to merge 189 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
189 commits
Select commit Hold shift + click to select a range
2e15123
Adds filesystem watcher with tests.
Sep 1, 2024
19f4024
Refactoring.
Sep 2, 2024
008ff89
Merge branch 'main' into file-watchers
Sep 2, 2024
9451ade
Formatting.
Sep 2, 2024
bf4e72f
Formatting.
Sep 2, 2024
4920767
Switches to absolute path in tests.
Sep 2, 2024
c834a02
Fixes race condition from merge conflict.
Sep 2, 2024
4273035
Fixes race condition.
Sep 2, 2024
d5d86d5
Fixes tests.
Sep 2, 2024
236a782
Fixes markdown lint errors.
Sep 2, 2024
ad3c7fc
Switches back to absolute paths.
Sep 2, 2024
9b40362
Reverts back to relative file paths.
Sep 2, 2024
97b4688
Fixes golangci-lint issues.
Sep 2, 2024
b528c23
Uses github.com/dunglas/go-fswatch instead.
Sep 2, 2024
8cac13b
Stops watcher before stopping workers.
Sep 3, 2024
8a66165
Updates docs.
Sep 3, 2024
d8de3ee
Avoids segfault in tests.
Sep 3, 2024
3d7d849
Fixes watcher segmentation violations on shutdown.
Sep 3, 2024
08a4c8e
Adjusts watcher latencies and tests.
Sep 3, 2024
41d337b
Adds fswatch to dockerfiles
Sep 3, 2024
639c072
Fixes fswatch in alpine.
Sep 4, 2024
f6ba8d7
Fixes segfault (this time for real).
Sep 4, 2024
f6f4a9c
Allows queueing new reload if file changes while workers are reloading.
Sep 5, 2024
5e345f1
Makes tests more consistent.
Sep 5, 2024
0423bb9
Prevents the watcher from getting stuck if there is an error in the w…
Sep 5, 2024
1db309b
Reverts changing the image.
Sep 5, 2024
c6591f0
Puts fswatch version into docker-bake.hcl.
Sep 5, 2024
c946de9
Asserts instead of panicking.
Sep 5, 2024
0d4327d
Adds notice
AlliBalliBaba Sep 5, 2024
497627a
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
70461de
Update dev.Dockerfile
AlliBalliBaba Sep 5, 2024
6fcedfb
Update Dockerfile
AlliBalliBaba Sep 5, 2024
d8e0a2c
Update Dockerfile
AlliBalliBaba Sep 5, 2024
9df79f3
Update alpine.Dockerfile
AlliBalliBaba Sep 5, 2024
d8fb28c
Update alpine.Dockerfile
AlliBalliBaba Sep 5, 2024
d66588d
Update dev-alpine.Dockerfile
AlliBalliBaba Sep 5, 2024
53456f7
Update dev-alpine.Dockerfile
AlliBalliBaba Sep 5, 2024
fc27030
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
c61e42c
Update dev.Dockerfile
AlliBalliBaba Sep 5, 2024
c0836b9
Update docs/config.md
AlliBalliBaba Sep 5, 2024
71613fb
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
d87f3ff
Runs fswatch version.
Sep 5, 2024
0254b33
Removes .json.
Sep 5, 2024
1fb4bf2
Replaces ms with s.
Sep 5, 2024
665fa28
Resets the channel after closing it.
Sep 5, 2024
255c1c5
Update watcher_options.go
AlliBalliBaba Sep 5, 2024
f02c30b
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
9ec89dc
Update watcher_test.go
AlliBalliBaba Sep 5, 2024
420b125
Asserts no error instead.
Sep 5, 2024
138a3fe
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
e0e4bea
Fixes a race condition where events are fired after frankenphp has st…
Sep 5, 2024
77013f5
Updates docs.
Sep 5, 2024
a03bd81
Update watcher_options_test.go
AlliBalliBaba Sep 5, 2024
fce2e97
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 5, 2024
054dd1f
Allows queuing events while watchers are reloading.
Sep 5, 2024
36cab24
go fmt
Sep 5, 2024
94b2723
Refactors stopping and draining logic.
Sep 5, 2024
8044848
Allows extended watcher configuration with dirs, recursion, symlinks,…
Sep 6, 2024
96c2a11
Updates docs.
Sep 6, 2024
3de70de
Adds TODOS.
Sep 6, 2024
45a73ae
go fmt.
Sep 6, 2024
bc73a16
Fixes linting errors.
Sep 6, 2024
63975e7
Also allows wildcards in the longform and adjusts docs.
Sep 6, 2024
971e75a
Adds debug log.
Sep 6, 2024
f2b7bda
Fixes the watcher short form.
Sep 6, 2024
5de7a3d
Refactors sessions and options into a struct.
Sep 7, 2024
816b157
Fixes an overflow in the 'workersReadyWG' on unexpected terminations.
Sep 7, 2024
c90eeb1
Properly logs errors coming from session.Start().
Sep 7, 2024
f4b7c52
go fmt.
Sep 7, 2024
4a73228
Adds --nocache.
Sep 7, 2024
043801d
Fixes lint issue.
Sep 7, 2024
269a920
Refactors and resolves race condition on worker reload.
Sep 7, 2024
8af1874
Implements debouncing with a timer as suggested by @withinboredom.
Sep 7, 2024
a16b59e
Starts watcher even if no workers are defined.
Sep 7, 2024
2cc0d15
Updates docs with file limit warning.
Sep 7, 2024
94159d3
Adds watch config unit tests.
Sep 7, 2024
eea350b
Adjusts debounce timings.
Sep 7, 2024
64d1fa0
go fmt.
Sep 7, 2024
c60fbf6
Adds fswatch to static builder (test).
Sep 7, 2024
584a702
Adds a short grace period between stopping and destroying the watcher…
Sep 7, 2024
fe26542
Adds caddy test.
Sep 7, 2024
686ec68
Adjusts sleep time.
Sep 7, 2024
5edc067
Swap to edant/watcher.
Sep 10, 2024
1edd441
Fixes watch options and tests.
Sep 11, 2024
ba26532
go fmt.
Sep 11, 2024
e4377e9
Adds TODO.
Sep 11, 2024
08b8f42
Installs edant/watcher in the bookworm image.
Sep 11, 2024
c775523
Fixes linting.
Sep 11, 2024
c2b7d50
Refactors the watcher into its own module.
Sep 12, 2024
66c2e9a
Adjusts naming.
Sep 12, 2024
b294954
ADocker image adjustments and refactoring.
Sep 13, 2024
ebdbd48
Merge branch 'main' into file-watchers
Sep 13, 2024
d72d189
Testing installation methods.
Sep 14, 2024
8f3ee2b
Installs via gcc instead.
Sep 14, 2024
1acecf3
Fixes pointer formats.
Sep 14, 2024
2f28c19
Fixes lint issues.
Sep 14, 2024
3f16a0f
Fixes arm alpine and updates docs.
Sep 14, 2024
a7670df
Clang format.
Sep 14, 2024
438e4e2
Fixes dirs.
Sep 14, 2024
66b9c30
Adds watcher version arg.
Sep 14, 2024
1efc486
Merge branch 'refs/heads/main' into file-watchers
Sep 16, 2024
cd342c3
Uses static lib version.
Sep 16, 2024
db62988
Adds watcher to tests and sanitizers.
Sep 16, 2024
771c695
Uses sudo for copying the shared lib.
Sep 16, 2024
8bf02ca
Removes unnused func.
Sep 16, 2024
0893923
Refactoring.
Sep 16, 2024
e855f84
Update .github/workflows/sanitizers.yaml
AlliBalliBaba Sep 16, 2024
3d2de4a
Merge branch 'file-watchers' of https://github.com/AlliBalliBaba/fran…
Sep 16, 2024
5713209
Adds fpic.
Sep 16, 2024
fe978c0
Fixes linting.
Sep 16, 2024
a5fb0d2
Skips tests in msan.
Sep 17, 2024
41796bd
Resets op_cache in every worker thread after termination
Sep 17, 2024
5071e7e
Review fixes part 1.
Sep 19, 2024
f199b11
Test: installing libstc++ instead of gcc.
Sep 19, 2024
76d4ba0
Test: using msan ignorelist.
Sep 19, 2024
55b3589
Test: using msan ignorelist.
Sep 19, 2024
0bf555b
Test: using msan ignorelist.
Sep 19, 2024
266829b
Allows '/**/' for global recursion and '**/' for relative recursion.
Sep 19, 2024
0d0fa51
Reverts using the ignorelist.
Sep 19, 2024
f025e62
Calls opcache directly.
Sep 19, 2024
03123c4
Adds --watch to php-server command
Sep 19, 2024
2913b4a
Replaces handles with thread ids.
Sep 21, 2024
a03ff51
Gives each thread its own pinner.
Sep 21, 2024
9179c6d
Replaces sync.map with a slice like suggested by @withinboredom.
Sep 22, 2024
d268bd8
Merge branch 'main' into no-handles
Sep 24, 2024
8034f80
Merge branch 'main' into file-watchers
Sep 24, 2024
4c8a9c1
Properly free CStrings.
Sep 24, 2024
5f37d25
Sorts alphabetically and uses curl instead of git.
Sep 24, 2024
f925f54
Labeling and formatting.
Sep 24, 2024
1193de8
Update .github/workflows/sanitizers.yaml
AlliBalliBaba Sep 24, 2024
ef21c77
Update .github/workflows/sanitizers.yaml
AlliBalliBaba Sep 24, 2024
009c0b4
Update .github/workflows/tests.yaml
AlliBalliBaba Sep 24, 2024
6be5f99
Update .github/workflows/tests.yaml
AlliBalliBaba Sep 24, 2024
be2c344
Update caddy/caddy.go
AlliBalliBaba Sep 24, 2024
4e22dcd
Update docs/config.md
AlliBalliBaba Sep 24, 2024
6bad089
Update frankenphp_with_watcher_test.go
AlliBalliBaba Sep 24, 2024
01e320e
Update watcher/watcher.h
AlliBalliBaba Sep 24, 2024
e40d455
Update frankenphp.c
AlliBalliBaba Sep 24, 2024
26f02bb
Update watcher/watcher.go
AlliBalliBaba Sep 24, 2024
dbbdf5e
Update docs/config.md
AlliBalliBaba Sep 24, 2024
393be86
Update frankenphp_with_watcher_test.go
AlliBalliBaba Sep 24, 2024
9df7894
Update testdata/files/.gitignore
AlliBalliBaba Sep 24, 2024
b2837df
Update watcher/watcher-c.h
AlliBalliBaba Sep 24, 2024
9a1ac7c
Update watcher/watcher.c
AlliBalliBaba Sep 24, 2024
4ce9d37
Fixes test and Dockerfile.
Sep 24, 2024
01cc6eb
Fixes Dockerfiles.
Sep 24, 2024
672f7ee
Resets go versions.
Sep 26, 2024
5c3e0b9
Replaces unsafe.pointer with uintptr_t
Sep 26, 2024
08eaf89
Prevents worker channels from being destroyed on reload.
Sep 26, 2024
df4e144
Minimizes the public api by only passing a []string.
Sep 26, 2024
3c7c7cc
Adds support for directory patterns and multiple '**' globs.
Sep 27, 2024
5af705f
Adjusts label.
Sep 27, 2024
9fbeeaf
go fmt.
Sep 27, 2024
cd86650
Merge branch 'main' into file-watchers
Sep 27, 2024
bfc07f2
go mod tidy.
Sep 27, 2024
b399a3f
Fixes merge conflict.
Sep 27, 2024
e154531
Refactoring and formatting.
Sep 27, 2024
ea15b7e
Cleans up unused vars and functions.
Sep 27, 2024
3feba9c
Allows dirs with a dot.
Sep 27, 2024
26672c3
Makes test nicer.
Sep 27, 2024
67fcd28
Add dir tests.
Sep 27, 2024
bdd603d
Moves the watch directive inside the worker directive.
Sep 29, 2024
f2122fb
Adds debug log on special events.
Sep 29, 2024
8670acb
Removes line about symlinks.
Sep 30, 2024
27b4314
Hints at multiple possible --watch flags.
Sep 30, 2024
bc746e1
Adds ./**/*.php as default watch configuration.
Oct 2, 2024
6e685a0
Changes error to a warning.
Oct 2, 2024
ef7ab94
Changes the default to './**/*.{php,yaml,yml,twig,env}' and supports …
Oct 2, 2024
b8d915c
Fixes linting.
Oct 2, 2024
29d1f20
Merge branch 'refs/heads/file-watchers' into no-handles
Oct 5, 2024
b503043
Refactors workers into a struct.
Oct 5, 2024
a4b2086
Fixes the logger order.
Oct 5, 2024
037dcd4
Extracts worker.startThread().
Oct 5, 2024
90c02d9
Resets the thread's worker one it's done.
Oct 5, 2024
620d010
Merge branch 'main' into file-watchers
Oct 5, 2024
043bb86
Fixes merge conflict and adjust values.
Oct 5, 2024
3e12959
Merge branch 'file-watchers' into no-handles
Oct 6, 2024
20f55e7
Fixes merge conflicts.
Oct 6, 2024
8a2fb45
Adjusts values.
Oct 6, 2024
6d6d4ff
Formatting.
Oct 6, 2024
f24e6c4
Merge branch 'file-watchers' into no-handles
Oct 6, 2024
a481d57
Refactors backoff algorithm.
Oct 6, 2024
583fdba
More formatting.
Oct 6, 2024
5f38619
Makes struct non-public.
Oct 6, 2024
2180216
go fmt.
Oct 6, 2024
cc0c249
Resets main_request - active_request logic.
Oct 7, 2024
0fbc8b4
Sets a higher min backoff.
Oct 7, 2024
6261930
Formatting.
Oct 7, 2024
e1b9afd
Merge branch 'main' into no-handles
Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
echo '<?php phpinfo();' > /app/public/index.php

COPY --link caddy/frankenphp/Caddyfile /etc/caddy/Caddyfile
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK CMD curl -f http://localhost:2019/metrics || exit 1
Expand Down Expand Up @@ -85,6 +85,16 @@
COPY --link caddy caddy
COPY --link internal internal
COPY --link testdata testdata
COPY --link watcher watcher

# install edant/watcher (necessary for file watching)
ARG EDANT_WATCHER_VERSION=next
WORKDIR /usr/local/src/watcher
RUN curl -L https://github.com/e-dant/watcher/archive/refs/heads/$EDANT_WATCHER_VERSION.tar.gz | tar xz
WORKDIR /usr/local/src/watcher/watcher-$EDANT_WATCHER_VERSION/watcher-c
RUN gcc -o libwatcher.so ./src/watcher-c.cpp -I ./include -I ../include -std=c++17 -O3 -Wall -Wextra -fPIC -shared && \
cp libwatcher.so /usr/local/lib/libwatcher.so && \
ldconfig /usr/local/lib

# install edant/watcher (necessary for file watching)
ARG EDANT_WATCHER_VERSION=next
Expand Down
10 changes: 10 additions & 0 deletions alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
echo '<?php phpinfo();' > /app/public/index.php

COPY --link caddy/frankenphp/Caddyfile /etc/caddy/Caddyfile
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK CMD curl -f http://localhost:2019/metrics || exit 1
Expand Down Expand Up @@ -106,6 +106,16 @@
COPY --link caddy caddy
COPY --link internal internal
COPY --link testdata testdata
COPY --link watcher watcher

# install edant/watcher (necessary for file watching)
ARG EDANT_WATCHER_VERSION=next
WORKDIR /usr/local/src/watcher
RUN curl -L https://github.com/e-dant/watcher/archive/refs/heads/$EDANT_WATCHER_VERSION.tar.gz | tar xz
WORKDIR /usr/local/src/watcher/watcher-$EDANT_WATCHER_VERSION/watcher-c
RUN gcc -o libwatcher.so ./src/watcher-c.cpp -I ./include -I ../include -std=c++17 -O3 -Wall -Wextra -fPIC -shared && \
cp libwatcher.so /usr/local/lib/libwatcher.so && \
ldconfig /usr/local/lib

# install edant/watcher (necessary for file watching)
ARG EDANT_WATCHER_VERSION=next
Expand Down
61 changes: 27 additions & 34 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ frankenphp_config frankenphp_get_config() {
}

typedef struct frankenphp_server_context {
uintptr_t current_request;
uintptr_t main_request;
bool has_main_request;
bool has_active_request;
bool worker_ready;
char *cookie_data;
bool finished;
} frankenphp_server_context;

__thread frankenphp_server_context *local_ctx = NULL;
__thread int thread_index;

static void frankenphp_free_request_context() {
frankenphp_server_context *ctx = SG(server_context);
Expand Down Expand Up @@ -231,9 +232,8 @@ PHP_FUNCTION(frankenphp_finish_request) { /* {{{ */
php_output_end_all();
php_header();

if (ctx->current_request != 0) {
go_frankenphp_finish_request(ctx->main_request, ctx->current_request,
false);
if (ctx->has_active_request) {
go_frankenphp_finish_request(thread_index, false);
}

ctx->finished = true;
Expand All @@ -249,7 +249,7 @@ PHP_FUNCTION(frankenphp_request_headers) {

frankenphp_server_context *ctx = SG(server_context);
struct go_apache_request_headers_return headers =
go_apache_request_headers(ctx->current_request, ctx->main_request);
go_apache_request_headers(thread_index, ctx->has_active_request);

array_init_size(return_value, headers.r1);

Expand All @@ -260,7 +260,7 @@ PHP_FUNCTION(frankenphp_request_headers) {
add_assoc_stringl_ex(return_value, key.data, key.len, val.data, val.len);
}

go_apache_request_cleanup(headers.r2);
go_apache_request_cleanup(thread_index);
}
/* }}} */

Expand Down Expand Up @@ -327,7 +327,7 @@ PHP_FUNCTION(frankenphp_handle_request) {

frankenphp_server_context *ctx = SG(server_context);

if (ctx->main_request == 0) {
if (!ctx->has_main_request) {
/* not a worker, throw an error */
zend_throw_exception(
spl_ce_RuntimeException,
Expand All @@ -340,18 +340,14 @@ PHP_FUNCTION(frankenphp_handle_request) {
frankenphp_worker_request_shutdown();

ctx->worker_ready = true;

/* Mark the worker as ready to handle requests */
go_frankenphp_worker_ready(ctx->main_request);
}

#ifdef ZEND_MAX_EXECUTION_TIMERS
/* Disable timeouts while waiting for a request to handle */
zend_unset_timeout();
#endif

uintptr_t request =
go_frankenphp_worker_handle_request_start(ctx->main_request);
bool request = go_frankenphp_worker_handle_request_start(thread_index);
if (frankenphp_worker_request_startup() == FAILURE
/* Shutting down */
|| !request) {
Expand Down Expand Up @@ -384,8 +380,8 @@ PHP_FUNCTION(frankenphp_handle_request) {
}

frankenphp_worker_request_shutdown();
ctx->current_request = 0;
go_frankenphp_finish_request(ctx->main_request, request, true);
ctx->has_active_request = false;
go_frankenphp_finish_request(thread_index, true);

RETURN_TRUE;
}
Expand Down Expand Up @@ -426,7 +422,7 @@ static zend_module_entry frankenphp_module = {
static void frankenphp_request_shutdown() {
frankenphp_server_context *ctx = SG(server_context);

if (ctx->main_request && ctx->current_request) {
if (ctx->has_main_request && ctx->has_active_request) {
frankenphp_destroy_super_globals();
}

Expand All @@ -437,7 +433,7 @@ static void frankenphp_request_shutdown() {
}

int frankenphp_update_server_context(
bool create, uintptr_t current_request, uintptr_t main_request,
bool create, bool has_main_request, bool has_active_request,

const char *request_method, char *query_string, zend_long content_length,
char *path_translated, char *request_uri, const char *content_type,
Expand All @@ -459,8 +455,8 @@ int frankenphp_update_server_context(
// It is not reset by zend engine, set it to 200.
SG(sapi_headers).http_response_code = 200;

ctx->main_request = main_request;
ctx->current_request = current_request;
ctx->has_main_request = has_main_request;
ctx->has_active_request = has_active_request;

SG(request_info).auth_password = auth_password;
SG(request_info).auth_user = auth_user;
Expand Down Expand Up @@ -493,9 +489,8 @@ static size_t frankenphp_ub_write(const char *str, size_t str_length) {
return 0;
}

struct go_ub_write_return result = go_ub_write(
ctx->current_request ? ctx->current_request : ctx->main_request,
(char *)str, str_length);
struct go_ub_write_return result =
go_ub_write(thread_index, (char *)str, str_length);

if (result.r1) {
php_handle_aborted_connection();
Expand All @@ -512,7 +507,7 @@ static int frankenphp_send_headers(sapi_headers_struct *sapi_headers) {
int status;
frankenphp_server_context *ctx = SG(server_context);

if (ctx->current_request == 0) {
if (!ctx->has_active_request) {
return SAPI_HEADER_SEND_FAILED;
}

Expand All @@ -526,35 +521,35 @@ static int frankenphp_send_headers(sapi_headers_struct *sapi_headers) {
}
}

go_write_headers(ctx->current_request, status, &sapi_headers->headers);
go_write_headers(thread_index, status, &sapi_headers->headers);

return SAPI_HEADER_SENT_SUCCESSFULLY;
}

static void frankenphp_sapi_flush(void *server_context) {
frankenphp_server_context *ctx = (frankenphp_server_context *)server_context;

if (ctx && ctx->current_request != 0 && go_sapi_flush(ctx->current_request)) {
if (ctx && ctx->has_active_request && go_sapi_flush(thread_index)) {
php_handle_aborted_connection();
}
}

static size_t frankenphp_read_post(char *buffer, size_t count_bytes) {
frankenphp_server_context *ctx = SG(server_context);

return ctx->current_request
? go_read_post(ctx->current_request, buffer, count_bytes)
return ctx->has_active_request
? go_read_post(thread_index, buffer, count_bytes)
: 0;
}

static char *frankenphp_read_cookies(void) {
frankenphp_server_context *ctx = SG(server_context);

if (ctx->current_request == 0) {
if (!ctx->has_active_request) {
return "";
}

ctx->cookie_data = go_read_cookies(ctx->current_request);
ctx->cookie_data = go_read_cookies(thread_index);

return ctx->cookie_data;
}
Expand Down Expand Up @@ -664,16 +659,13 @@ void frankenphp_register_bulk_variables(go_string known_variables[27],

static void frankenphp_register_variables(zval *track_vars_array) {
/* https://www.php.net/manual/en/reserved.variables.server.php */
frankenphp_server_context *ctx = SG(server_context);

/* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array);

go_register_variables(ctx->current_request ? ctx->current_request
: ctx->main_request,
track_vars_array);
go_register_variables(thread_index, track_vars_array);
}

static void frankenphp_log_message(const char *message, int syslog_type_int) {
Expand Down Expand Up @@ -732,6 +724,7 @@ static void set_thread_name(char *thread_name) {
static void *php_thread(void *arg) {
char thread_name[16] = {0};
snprintf(thread_name, 16, "php-%" PRIxPTR, (uintptr_t)arg);
thread_index = (int)(uintptr_t)arg;
set_thread_name(thread_name);

#ifdef ZTS
Expand All @@ -744,7 +737,7 @@ static void *php_thread(void *arg) {

local_ctx = malloc(sizeof(frankenphp_server_context));

while (go_handle_request()) {
while (go_handle_request(thread_index)) {
}

#ifdef ZTS
Expand Down
Loading
Loading