diff --git a/packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts b/packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts index 92aab4e8f8..1688fec8e4 100644 --- a/packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts +++ b/packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts @@ -88,7 +88,7 @@ describe('rotatePHPRuntime()', () => { expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2); }, 30_000); - it('Should recreate the PHP runtime after PHP crash', async () => { + it('Should recreate the PHP runtime after a PHP runtime crash', async () => { const recreateRuntimeSpy = vitest.fn(recreateRuntime); const php = new PHP(await recreateRuntimeSpy()); rotatePHPRuntime({ @@ -101,10 +101,34 @@ describe('rotatePHPRuntime()', () => { await php.dispatchEvent({ type: 'request.error', error: new Error('mock error'), + source: 'php-wasm', }); expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2); }, 30_000); + it('Should not recreate the PHP runtime after a PHP fatal', async () => { + const recreateRuntimeSpy = vitest.fn(recreateRuntime); + const php = new PHP(await recreateRuntimeSpy()); + rotatePHPRuntime({ + php, + cwd: '/test-root', + recreateRuntime: recreateRuntimeSpy, + maxRequests: 1234, + }); + // Trigger error with no `source` + await php.dispatchEvent({ + type: 'request.error', + error: new Error('mock error'), + }); + // Trigger error with request `source` + await php.dispatchEvent({ + type: 'request.error', + error: new Error('mock error'), + source: 'request', + }); + expect(recreateRuntimeSpy).toHaveBeenCalledTimes(1); + }, 30_000); + it('Should not rotate after the cleanup handler is called, even if there is a PHP runtime error', async () => { const recreateRuntimeSpy = vitest.fn(recreateRuntime); const php = new PHP(await recreateRuntimeSpy()); @@ -124,6 +148,7 @@ describe('rotatePHPRuntime()', () => { php.dispatchEvent({ type: 'request.error', error: new Error('mock error'), + source: 'php-wasm', }); expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2); diff --git a/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts b/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts index fae1ab2ad8..8f159cbc80 100644 --- a/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts +++ b/packages/php-wasm/universal/src/lib/rotate-php-runtime.ts @@ -1,4 +1,5 @@ import { PHP } from './php'; +import { PHPEvent } from './universal-php'; export interface RotateOptions { php: PHP; @@ -57,11 +58,17 @@ export function rotatePHPRuntime({ await rotateRuntime(); } - php.addEventListener('request.error', rotateRuntime); + async function rotateRuntimeForPhpWasmError(event: PHPEvent) { + if (event.type === 'request.error' && event.source === 'php-wasm') { + await rotateRuntime(); + } + } + + php.addEventListener('request.error', rotateRuntimeForPhpWasmError); php.addEventListener('request.end', rotateRuntimeAfterMaxRequests); return function () { - php.removeEventListener('request.error', rotateRuntime); + php.removeEventListener('request.error', rotateRuntimeForPhpWasmError); php.removeEventListener('request.end', rotateRuntimeAfterMaxRequests); }; }