Skip to content

Commit 30466b1

Browse files
authored
Fix shutdown errors (#1104)
Fixes #1078 ## What is this PR doing? If fixes an Asyncify error and memory leak when running PHP shutdown functions. ## What problem is it solving? It adds full support for PHP shutdown functions and prevents crashes. ## How is the problem addressed? By adding Asyncify support for `wasm_sapi_request_shutdown` and preventing `free` from running if memory wasn't allocated. ## Testing Instructions - Checkout this branch - Run dev env `npm run dev` - [Open this URL](http://localhost:5400/website-server/?php=8.0&wp=latest&storage=none&networking=yes#{%20%22landingPage%22:%20%22/wp-admin/%22,%20%22features%22:%20{%20%22networking%22:%20true%20},%20%22steps%22:%20[%20{%20%22step%22:%20%22login%22%20},%20{%20%22step%22:%20%22writeFile%22,%20%22path%22:%20%22/wordpress/wp-content/mu-plugins/rewrite.php%22,%20%22data%22:%20%22%3C?php%20add_action(%20'shutdown',%20function()%20{%20post_message_to_js('test');%20}%20);%22%20}%20]%20}) and confirm that WP loads without Asyncify errors and and memory leaks (check the browser console)
1 parent 1a1c241 commit 30466b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+404
-343
lines changed

packages/php-wasm/compile/php/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
521521
"get_http_body",\
522522
"wasm_php_exec",\
523523
"wasm_sapi_handle_request",\
524+
"wasm_sapi_request_shutdown",\
524525
"_call_user_function_ex",\
525526
"_call_user_function_impl",\
526527
"_mysqlnd_run_command",\
@@ -786,6 +787,7 @@ RUN set -euxo pipefail; \
786787
"_wasm_add_ENV_entry", \n\
787788
"_wasm_read", \n\
788789
"_wasm_sapi_handle_request", \n\
790+
"_wasm_sapi_request_shutdown", \n\
789791
"_wasm_set_content_length", \n\
790792
"_wasm_set_content_type", \n\
791793
"_wasm_set_cookies", \n\

packages/php-wasm/compile/php/php_wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ PHP_FUNCTION(post_message_to_js)
396396

397397
char *response;
398398
size_t response_len = js_module_onMessage(data, &response);
399-
if (response != NULL)
399+
if (response != -1)
400400
{
401401
zend_string *return_string = zend_string_init(response, response_len, 0);
402402
free(response);

packages/php-wasm/compile/php/phpwasm-emscripten-library.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const LibraryExample = {
2020
// stdout, stderr, and headers information are written for the JavaScript
2121
// code to read later on.
2222
FS.mkdir("/internal");
23-
23+
2424
PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
2525
? require('events').EventEmitter
2626
: class EventEmitter {
@@ -548,7 +548,7 @@ const LibraryExample = {
548548
return Asyncify.handleSleep((wakeUp) => {
549549
const poll = function () {
550550
if (PHPWASM.child_proc_by_pid[pid]?.exited) {
551-
HEAPU32[exitCodePtr >> 2] =
551+
HEAPU32[exitCodePtr >> 2] =
552552
PHPWASM.child_proc_by_pid[pid].exitCode;
553553
wakeUp(pid);
554554
} else {
@@ -950,7 +950,7 @@ const LibraryExample = {
950950
// separates JS context from the PHP context so we
951951
// don't let PHP crash here.
952952
console.error(e);
953-
wakeUp(0);
953+
wakeUp(-1);
954954
});
955955
});
956956
}
298 Bytes
Binary file not shown.
144 Bytes
Binary file not shown.
58 Bytes
Binary file not shown.
219 Bytes
Binary file not shown.
203 Bytes
Binary file not shown.
95 Bytes
Binary file not shown.
63 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)