diff --git a/.github/workflows/frankenphp.yml b/.github/workflows/frankenphp.yml new file mode 100644 index 000000000..290b242dd --- /dev/null +++ b/.github/workflows/frankenphp.yml @@ -0,0 +1,57 @@ +name: "FrankenPHP" + +on: + pull_request: + branches: + - "v*.*" + - "feature/*" + push: + branches: + - "v*.*" + - "feature/*" + +permissions: + contents: read + +jobs: + thread-reboot: + name: "FrankenPHP in-process thread reboot" + runs-on: "ubuntu-24.04" + + services: + mongodb: + # Single-node replica set with a built-in healthcheck, which the runner + # waits for before running the steps. + image: "mongodb/mongodb-atlas-local:8.0" + ports: + - "27017:27017" + + env: + # The FrankenPHP container reaches the service through the Docker host. + MONGODB_URI: "mongodb://host.docker.internal:27017/?directConnection=true&serverSelectionTimeoutMS=2000" + + steps: + - name: "Checkout" + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: true + + - name: "Set up Docker buildx" + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + + - name: "Build the image with the driver" + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: "." + file: "tests/frankenphp/Dockerfile" + tags: "mongodb-frankenphp" + load: true + # Layer cache shared between runs: dependency layers and the extension + # build are reused when the sources they depend on did not change. + cache-from: "type=gha" + cache-to: "type=gha,mode=max" + + - name: "Run the restart test" + run: "SKIP_BUILD=1 bash tests/frankenphp/restart-test.sh" + env: + IMAGE_NAME: "mongodb-frankenphp" diff --git a/phongo.c b/phongo.c index 888be89bf..630978aee 100644 --- a/phongo.c +++ b/phongo.c @@ -25,7 +25,6 @@ #include #include "phongo.h" -#include "src/phongo_atomic.h" #include "src/phongo_client.h" #include "src/phongo_error.h" #include "src/phongo_ini.h" @@ -37,12 +36,25 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb) ZEND_TSRMLS_CACHE_DEFINE(); #endif -/* Initialize a thread counter, which will be atomically incremented in GINIT. - * In turn, GSHUTDOWN will decrement the counter and call mongoc_cleanup() when - * it reaches zero (i.e. last thread is shutdown). This is necessary because - * mongoc_cleanup() must be called after all persistent clients have been - * destroyed. */ -static int32_t phongo_num_threads = 0; +/* Track whether libmongoc has been initialized in this process. libmongoc + * state is process-wide: the C driver documents that mongoc_init() must be + * called exactly once and that it does not reinitialize the driver after + * mongoc_cleanup(). Some SAPIs (e.g. FrankenPHP) shut the PHP engine down and + * start it again in the same process, which runs GINIT, GSHUTDOWN, MINIT, and + * MSHUTDOWN once per engine lifecycle. libmongoc must therefore be initialized + * and shut down once per process, not once per engine lifecycle. Otherwise, the + * next engine startup reuses a libmongoc instance whose global state (e.g. the + * handshake data) was freed by mongoc_cleanup() and cannot be restored by the + * subsequent mongoc_init() call. */ +static bool phongo_libmongoc_initialized = false; + +/* Shut down libmongoc when the process exits. This is registered with atexit() + * in MINIT and is intentionally not tied to the engine lifecycle. */ +static void phongo_libmongoc_shutdown(void) +{ + mongoc_cleanup(); + bson_mem_restore_vtable(); +} /* Declare zend_class_entry dependencies, which are initialized in MINIT */ zend_class_entry* phongo_json_serializable_ce; @@ -130,9 +142,6 @@ PHP_GINIT_FUNCTION(mongodb) /* {{{ */ ZEND_TSRMLS_CACHE_UPDATE(); #endif - /* Increment the thread counter. */ - phongo_atomic_int32_fetch_add(&phongo_num_threads, 1, phongo_memory_order_seq_cst); - /* Clear extension globals */ memset(mongodb_globals, 0, sizeof(zend_mongodb_globals)); @@ -203,11 +212,20 @@ PHP_MINIT_FUNCTION(mongodb) /* {{{ */ phongo_register_ini_entries(INIT_FUNC_ARGS_PASSTHRU); /* Assign our custom vtable to libbson, so all memory allocation in libbson - * (and libmongoc) will use PHP's persistent memory API. After doing so, - * initialize libmongoc. Later, we will shutdown libmongoc and restore - * libbson's vtable in the final GSHUTDOWN. */ + * (and libmongoc) will use PHP's persistent memory API. The vtable must be + * assigned on every engine startup because libmongoc outlives the engine. + * libmongoc is initialized once per process and shut down along with the + * vtable restoration when the process exits. */ bson_mem_set_vtable(&bson_mem_vtable); - mongoc_init(); + + if (!phongo_libmongoc_initialized) { + mongoc_init(); + phongo_libmongoc_initialized = true; + + /* libmongoc must be shut down at process exit. See the comment above + * phongo_libmongoc_initialized for details. */ + (void) atexit(phongo_libmongoc_shutdown); + } /* Prep default object handlers to be used when we register the classes */ memcpy(&phongo_std_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); @@ -387,15 +405,6 @@ PHP_GSHUTDOWN_FUNCTION(mongodb) /* {{{ */ /* TODO: Check that logging actually gets disabled. The logger HashTable * should be empty by this point. */ phongo_log_set_stream(NULL); - - /* Decrement the thread counter. If it reaches zero, we can infer that this - * is the last thread, MSHUTDOWN has been called, persistent clients from - * all threads have been destroyed, and it is now safe to shutdown libmongoc - * and restore libbson's original vtable. */ - if (phongo_atomic_int32_fetch_sub(&phongo_num_threads, 1, phongo_memory_order_seq_cst) - 1 == 0) { - mongoc_cleanup(); - bson_mem_restore_vtable(); - } } /* }}} */ PHP_MINFO_FUNCTION(mongodb) /* {{{ */ diff --git a/tests/bson/bson-document-duplicate-keys-001.phpt b/tests/bson/bson-document-duplicate-keys-001.phpt new file mode 100644 index 000000000..46ffc3f3e --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-001.phpt @@ -0,0 +1,27 @@ +--TEST-- +MongoDB\BSON\Document: Encoding duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.1 (encoding) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#21-test-encoding +--FILE-- + 1, 'foo' => 2]); + +echo "PHP collapses duplicate keys, keeping the last value\n"; +echo $document->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $document === hex2bin('0e00000010666f6f000200000000')); + +?> +===DONE=== + +--EXPECT-- +PHP collapses duplicate keys, keeping the last value +{ "foo" : 2 } +bool(true) +===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-002.phpt b/tests/bson/bson-document-duplicate-keys-002.phpt new file mode 100644 index 000000000..7b642b9d7 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-002.phpt @@ -0,0 +1,79 @@ +--TEST-- +MongoDB\BSON\Document: Decoding BSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.2 (decoding) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#22-test-decoding +--FILE-- + $value) { + var_dump($key, $value); +} + +echo "\nget() and offsetGet() return the first value\n"; +var_dump($document->get('foo')); +var_dump($document['foo']); +var_dump($document->has('foo')); + +echo "\ntoPHP() keeps the last value for object type maps\n"; +var_dump($document->toPHP()); +var_dump($document->toPHP(['root' => 'object'])); + +echo "\ntoPHP() keeps the last value for array type maps\n"; +var_dump($document->toPHP(['root' => 'array'])); + +echo "\ntoPHP() with the bson type map preserves duplicates\n"; +$bsonDocument = $document->toPHP(['root' => 'bson']); +var_dump($bsonDocument instanceof MongoDB\BSON\Document); +echo $bsonDocument->toRelaxedExtendedJSON(), "\n"; + +echo "\nExtended JSON preserves duplicates\n"; +echo $document->toRelaxedExtendedJSON(), "\n"; +echo $document->toCanonicalExtendedJSON(), "\n"; + +?> +===DONE=== + +--EXPECTF-- +Iteration returns both elements in order +string(3) "foo" +int(1) +string(3) "foo" +int(2) + +get() and offsetGet() return the first value +int(1) +int(1) +bool(true) + +toPHP() keeps the last value for object type maps +object(stdClass)#%d (1) { + ["foo"]=> + int(2) +} +object(stdClass)#%d (1) { + ["foo"]=> + int(2) +} + +toPHP() keeps the last value for array type maps +array(1) { + ["foo"]=> + int(2) +} + +toPHP() with the bson type map preserves duplicates +bool(true) +{ "foo" : 1, "foo" : 2 } + +Extended JSON preserves duplicates +{ "foo" : 1, "foo" : 2 } +{ "foo" : { "$numberInt" : "1" }, "foo" : { "$numberInt" : "2" } } +===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-003.phpt b/tests/bson/bson-document-duplicate-keys-003.phpt new file mode 100644 index 000000000..fea1e62e5 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-003.phpt @@ -0,0 +1,42 @@ +--TEST-- +MongoDB\BSON\Document: Round-trip of BSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.3 (round-trip) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#23-test-round-trip +--FILE-- +toRelaxedExtendedJSON()); +echo $fromJson->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $fromJson === $bson); + +echo "\nRound-trip through PHP keeps the last value\n"; +$roundTrip = MongoDB\BSON\Document::fromPHP($document->toPHP()); +echo $roundTrip->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $roundTrip === $bson); + +?> +===DONE=== + +--EXPECT-- +Raw BSON is preserved +bool(true) + +Round-trip through Extended JSON preserves duplicates +{ "foo" : 1, "foo" : 2 } +bool(true) + +Round-trip through PHP keeps the last value +{ "foo" : 2 } +bool(false) +===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-004.phpt b/tests/bson/bson-document-duplicate-keys-004.phpt new file mode 100644 index 000000000..8dcf68298 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-004.phpt @@ -0,0 +1,27 @@ +--TEST-- +MongoDB\BSON\Document: Parsing Extended JSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.4 (parsing Extended JSON) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#24-test-parsing-extended-json +--FILE-- +toRelaxedExtendedJSON(), "\n"; +var_dump((string) $document === $expected); + +?> +===DONE=== + +--EXPECT-- +Duplicate keys are preserved in order +{ "foo" : 1, "foo" : 2 } +bool(true) +===DONE=== diff --git a/tests/frankenphp/Caddyfile b/tests/frankenphp/Caddyfile new file mode 100644 index 000000000..5d47b3ee0 --- /dev/null +++ b/tests/frankenphp/Caddyfile @@ -0,0 +1,17 @@ +{ + auto_https off +} + +:80 { + root * /app/public + php_server { + worker { + file /app/public/worker.php + # A single worker, so a different instance identifier can only come + # from a restart of that worker. + num 1 + # All requests go to the worker, whatever the requested path is. + match * + } + } +} diff --git a/tests/frankenphp/Dockerfile b/tests/frankenphp/Dockerfile new file mode 100644 index 000000000..dd09eb104 --- /dev/null +++ b/tests/frankenphp/Dockerfile @@ -0,0 +1,23 @@ +# Builds the extension from this checkout into the FrankenPHP image, so the +# functional test runs against the PHP build that FrankenPHP embeds (ZTS). +FROM dunglas/frankenphp:1.13.4-php8.5 + +# Dependencies needed by the bundled libmongoc and libmongocrypt builds. +RUN apt-get update && \ + apt-get install --yes --no-install-recommends cmake libssl-dev libsasl2-dev && \ + rm -rf /var/lib/apt/lists/* + +COPY config.m4 Makefile.frag phongo.c phongo.h phongo_version.h /usr/src/mongodb/ +COPY scripts /usr/src/mongodb/scripts +COPY src /usr/src/mongodb/src + +WORKDIR /usr/src/mongodb + +RUN phpize && \ + ./configure --with-php-config="$(command -v php-config)" && \ + make -j "$(nproc)" && \ + make install && \ + docker-php-ext-enable mongodb + +COPY tests/frankenphp/Caddyfile /etc/frankenphp/Caddyfile +COPY tests/frankenphp/worker.php /app/public/worker.php diff --git a/tests/frankenphp/restart-test.sh b/tests/frankenphp/restart-test.sh new file mode 100755 index 000000000..21c1c1cf5 --- /dev/null +++ b/tests/frankenphp/restart-test.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +# +# Functional test for PHPC-2797: FrankenPHP reboots all its PHP threads +# in-process when opcache_reset() is called. The engine then runs GSHUTDOWN and +# MINIT again in the same process, while libmongoc can only be initialized once +# per process. Before the fix, the next Manager creation aborted the process in +# mongoc_handshake_data_append(). +# +# The worker also runs a command on a MongoDB server on every request, so the +# test checks that the connection still works after the restart. Point +# MONGODB_URI at a running server, for example: +# docker run --detach --publish 27017:27017 mongodb/mongodb-atlas-local:8.0 +# MONGODB_URI='mongodb://host.docker.internal:27017/?directConnection=true' \ +# bash tests/frankenphp/restart-test.sh +# +# Requires Docker. Run it with: +# MONGODB_URI=... bash tests/frankenphp/restart-test.sh + +set -o errexit +set -o nounset +set -o pipefail + +REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) + +CONTAINER_NAME=${CONTAINER_NAME:-mongodb-frankenphp-restart} +IMAGE_NAME=${IMAGE_NAME:-mongodb-frankenphp} +PORT=${PORT:-8080} +BASE_URL="http://127.0.0.1:${PORT}" + +if [ -z "${MONGODB_URI:-}" ]; then + echo "FAILED: set MONGODB_URI, for example mongodb://host.docker.internal:27017/?directConnection=true" + exit 1 +fi + +cleanup() { + docker logs "${CONTAINER_NAME}" 2>&1 || true + docker rm --force --volumes "${CONTAINER_NAME}" >/dev/null 2>&1 || true +} +trap cleanup EXIT + +# Set SKIP_BUILD when the image was already built, for example by the CI +# workflow with a layer cache. +if [ -z "${SKIP_BUILD:-}" ]; then + docker build --tag "${IMAGE_NAME}" --file "${REPO_ROOT}/tests/frankenphp/Dockerfile" "${REPO_ROOT}" +fi + +# ZEND_DONT_UNLOAD_MODULES keeps the extension mapped when the engine shuts +# down, like SAPIs that restart the engine in-process. Without it, the extension +# is unloaded and reloaded with fresh global state, which hides PHPC-2797. +docker run --detach \ + --name "${CONTAINER_NAME}" \ + --add-host host.docker.internal:host-gateway \ + --env ZEND_DONT_UNLOAD_MODULES=1 \ + --env MONGODB_URI="${MONGODB_URI}" \ + --publish "${PORT}:80" \ + "${IMAGE_NAME}" >/dev/null + +get() { + curl --silent --fail --max-time 30 "${BASE_URL}$1" +} + +# Wait for the worker to answer successful requests, which also waits for +# MongoDB. PHP errors can be returned with a 200 status, so check the body. +echo "Waiting for FrankenPHP..." +body_before="" +for _ in $(seq 1 60); do + body_before=$(get / || true) + if [ "${body_before#ok }" != "${body_before}" ]; then + break + fi + sleep 1 +done + +if [ "${body_before#ok }" = "${body_before}" ]; then + echo "FAILED: unexpected response before the reboot: ${body_before}" + exit 1 +fi +instance_before=${body_before#ok } +echo "Worker instance before the reboot: ${instance_before}" + +# opcache_reset() makes FrankenPHP reboot all its PHP threads in-process. +if [ "$(get /reset)" != "reset" ]; then + echo "FAILED: opcache_reset() did not run, the PHP threads were not rebooted" + exit 1 +fi + +# The worker script runs again in the new engine lifecycle, so it reports a new +# instance identifier. Requests retry while the threads are being rebooted. If +# the extension aborted the process, or the connection to the server is broken, +# the requests fail. +body_after="" +for _ in $(seq 1 30); do + body_after=$(get / || true) + if [ "${body_after#ok }" != "${body_after}" ] && [ "${body_after#ok }" != "${instance_before}" ]; then + break + fi + sleep 1 +done + +if [ "${body_after#ok }" = "${body_after}" ]; then + echo "FAILED: the worker did not answer or the command failed after the reboot: ${body_after}" + exit 1 +fi + +instance_after=${body_after#ok } +if [ "${instance_after}" = "${instance_before}" ]; then + echo "FAILED: the PHP threads were not rebooted, the worker instance did not change" + exit 1 +fi + +echo "Worker instance after the reboot: ${instance_after}" + +if [ "$(docker inspect --format '{{.State.Running}}' "${CONTAINER_NAME}")" != "true" ]; then + echo "FAILED: the FrankenPHP process died after the in-process reboot" + exit 1 +fi + +echo "OK: the extension survived an in-process PHP thread reboot and still runs commands" diff --git a/tests/frankenphp/worker.php b/tests/frankenphp/worker.php new file mode 100644 index 000000000..9be02bcba --- /dev/null +++ b/tests/frankenphp/worker.php @@ -0,0 +1,37 @@ +executeCommand('admin', new MongoDB\Driver\Command(['ping' => 1])); + + echo 'ok ', $instance; +}; + +while (frankenphp_handle_request($handler)) { + gc_collect_cycles(); +}