-
Notifications
You must be signed in to change notification settings - Fork 376
[CLI] Explore spawning new OS processes in CLI spawnProcess #3001
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ import { jspi } from 'wasm-feature-detect'; | |
| import { MessageChannel, type MessagePort, parentPort } from 'worker_threads'; | ||
| import { mountResources } from '../mounts'; | ||
| import { logger } from '@php-wasm/logger'; | ||
| import { spawnWorkerThread } from '../run-cli'; | ||
|
|
||
| import type { Mount } from '@php-wasm/cli-util'; | ||
|
|
||
| export type WorkerBootOptions = { | ||
|
|
@@ -122,31 +124,22 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { | |
| } | ||
| } | ||
|
|
||
| async bootAndSetUpInitialWorker({ | ||
| siteUrl, | ||
| mountsBeforeWpInstall, | ||
| mountsAfterWpInstall, | ||
| phpVersion: php = RecommendedPHPVersion, | ||
| wordpressInstallMode, | ||
| wordPressZip, | ||
| sqliteIntegrationPluginZip, | ||
| firstProcessId, | ||
| processIdSpaceLength, | ||
| dataSqlPath, | ||
| followSymlinks, | ||
| trace, | ||
| internalCookieStore, | ||
| withXdebug, | ||
| nativeInternalDirPath, | ||
| }: PrimaryWorkerBootOptions) { | ||
| async bootAndSetUpInitialWorker(options: PrimaryWorkerBootOptions) { | ||
| const { | ||
| siteUrl, | ||
| mountsBeforeWpInstall, | ||
| mountsAfterWpInstall, | ||
| wordpressInstallMode, | ||
| wordPressZip, | ||
| sqliteIntegrationPluginZip, | ||
| dataSqlPath, | ||
| internalCookieStore, | ||
| } = options; | ||
| if (this.booted) { | ||
| throw new Error('Playground already booted'); | ||
| } | ||
| this.booted = true; | ||
|
|
||
| let nextProcessId = firstProcessId; | ||
| const lastProcessId = firstProcessId + processIdSpaceLength - 1; | ||
|
|
||
| try { | ||
| const constants: Record<string, string | number | boolean | null> = | ||
| { | ||
|
|
@@ -157,27 +150,10 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { | |
| let wordpressBooted = false; | ||
| const requestHandler = await bootWordPressAndRequestHandler({ | ||
| siteUrl, | ||
| createPhpRuntime: async () => { | ||
| const processId = nextProcessId; | ||
|
|
||
| if (nextProcessId < lastProcessId) { | ||
| nextProcessId++; | ||
| } else { | ||
| // We've reached the end of the process ID space. Start over. | ||
| nextProcessId = firstProcessId; | ||
| } | ||
|
|
||
| return await loadNodeRuntime(php, { | ||
| emscriptenOptions: { | ||
| fileLockManager: this.fileLockManager!, | ||
| processId, | ||
| trace: trace ? tracePhpWasm : undefined, | ||
| phpWasmInitOptions: { nativeInternalDirPath }, | ||
| }, | ||
| followSymlinks, | ||
| withXdebug, | ||
| }); | ||
| }, | ||
| createPhpRuntime: createPhpRuntimeFactory( | ||
| options, | ||
| this.fileLockManager! | ||
| ), | ||
| wordpressInstallMode, | ||
| wordPressZip: | ||
| wordPressZip !== undefined | ||
|
|
@@ -203,7 +179,10 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { | |
| }, | ||
| cookieStore: internalCookieStore ? undefined : false, | ||
| dataSqlPath, | ||
| spawnHandler: sandboxedSpawnHandlerFactory, | ||
| spawnHandler: () => | ||
| sandboxedSpawnHandlerFactory(() => | ||
| createPHPWorker(options, this.fileLockManager!) | ||
| ), | ||
| async onPHPInstanceCreated(php) { | ||
| await mountResources(php, mountsBeforeWpInstall); | ||
| if (wordpressBooted) { | ||
|
|
@@ -230,64 +209,37 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { | |
| } | ||
| } | ||
|
|
||
| async hello() { | ||
| return 'hello'; | ||
| } | ||
|
|
||
|
Comment on lines
+212
to
+215
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello! Is this left over from a development test, and do we still want it?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it's a testing leftover, good spot |
||
| async bootWorker(args: WorkerBootOptions) { | ||
| await this.bootRequestHandler(args); | ||
| } | ||
|
|
||
| async bootRequestHandler({ | ||
| siteUrl, | ||
| followSymlinks, | ||
| phpVersion, | ||
| firstProcessId, | ||
| processIdSpaceLength, | ||
| trace, | ||
| nativeInternalDirPath, | ||
| mountsBeforeWpInstall, | ||
| mountsAfterWpInstall, | ||
| withXdebug, | ||
| }: WorkerBootRequestHandlerOptions) { | ||
| async bootRequestHandler(options: WorkerBootRequestHandlerOptions) { | ||
| if (this.booted) { | ||
| throw new Error('Playground already booted'); | ||
| } | ||
| this.booted = true; | ||
|
|
||
| let nextProcessId = firstProcessId; | ||
| const lastProcessId = firstProcessId + processIdSpaceLength - 1; | ||
|
|
||
| try { | ||
| const requestHandler = await bootRequestHandler({ | ||
| siteUrl, | ||
| createPhpRuntime: async () => { | ||
| const processId = nextProcessId; | ||
|
|
||
| if (nextProcessId < lastProcessId) { | ||
| nextProcessId++; | ||
| } else { | ||
| // We've reached the end of the process ID space. Start over. | ||
| nextProcessId = firstProcessId; | ||
| } | ||
|
|
||
| return await loadNodeRuntime(phpVersion, { | ||
| emscriptenOptions: { | ||
| fileLockManager: this.fileLockManager!, | ||
| processId, | ||
| trace: trace ? tracePhpWasm : undefined, | ||
| ENV: { | ||
| DOCROOT: '/wordpress', | ||
| }, | ||
| phpWasmInitOptions: { nativeInternalDirPath }, | ||
| }, | ||
| followSymlinks, | ||
| withXdebug, | ||
| }); | ||
| }, | ||
| siteUrl: options.siteUrl, | ||
| createPhpRuntime: createPhpRuntimeFactory( | ||
| options, | ||
| this.fileLockManager! | ||
| ), | ||
| onPHPInstanceCreated: async (php) => { | ||
| await mountResources(php, mountsBeforeWpInstall); | ||
| await mountResources(php, mountsAfterWpInstall); | ||
| await mountResources(php, options.mountsBeforeWpInstall); | ||
| await mountResources(php, options.mountsAfterWpInstall); | ||
| }, | ||
| sapiName: 'cli', | ||
| cookieStore: false, | ||
| spawnHandler: sandboxedSpawnHandlerFactory, | ||
| spawnHandler: () => | ||
| sandboxedSpawnHandlerFactory(() => | ||
| createPHPWorker(options, this.fileLockManager!) | ||
| ), | ||
| }); | ||
| this.__internal_setRequestHandler(requestHandler); | ||
|
|
||
|
|
@@ -307,6 +259,91 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a factory function that starts a new PHP runtime in the currently | ||
| * running process. This is used for rotating the PHP runtime periodically. | ||
| */ | ||
| function createPhpRuntimeFactory( | ||
| options: WorkerBootRequestHandlerOptions, | ||
| fileLockManager: FileLockManager | RemoteAPI<FileLockManager> | ||
| ) { | ||
| let nextProcessId = options.firstProcessId; | ||
| const lastProcessId = | ||
| options.firstProcessId + options.processIdSpaceLength - 1; | ||
| return async () => { | ||
| const processId = nextProcessId; | ||
|
|
||
| if (nextProcessId < lastProcessId) { | ||
| nextProcessId++; | ||
| } else { | ||
| // We've reached the end of the process ID space. Start over. | ||
| nextProcessId = options.firstProcessId; | ||
| } | ||
|
|
||
| return await loadNodeRuntime( | ||
| options.phpVersion || RecommendedPHPVersion, | ||
| { | ||
| emscriptenOptions: { | ||
| fileLockManager, | ||
| processId, | ||
| trace: options.trace ? tracePhpWasm : undefined, | ||
| phpWasmInitOptions: { | ||
| nativeInternalDirPath: options.nativeInternalDirPath, | ||
| }, | ||
| }, | ||
| followSymlinks: options.followSymlinks, | ||
| withXdebug: options.withXdebug, | ||
| } | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Spawns a new PHP process to be used in the PHP spawn handler (in proc_open() etc. calls). | ||
| * It boots from this worker-thread-v1.ts file, but is a separate process. | ||
| * | ||
| * We explicitly avoid using PHPProcessManager.acquirePHPInstance() here. | ||
| * | ||
| * Why? | ||
| * | ||
| * Because each PHP instance acquires actual OS-level file locks via fcntl() and LockFileEx() | ||
| * syscalls. Running multiple PHP instances from the same OS process would allow them to | ||
| * acquire overlapping locks. Running every PHP instance in a separate OS process ensures | ||
| * any locks that overlap between PHP instances conflict with each other as expected. | ||
| * | ||
| * @param options - The options for the worker. | ||
| * @param fileLockManager - The file lock manager to use. | ||
| * @returns A promise that resolves to the PHP worker. | ||
| */ | ||
| async function createPHPWorker( | ||
| options: WorkerBootRequestHandlerOptions, | ||
| fileLockManager: FileLockManager | RemoteAPI<FileLockManager> | ||
| ) { | ||
| const spawnedWorker = await spawnWorkerThread('v1'); | ||
|
|
||
| const handler = consumeAPI<PlaygroundCliBlueprintV1Worker>( | ||
| spawnedWorker.phpPort | ||
| ); | ||
| handler.useFileLockManager(fileLockManager as any); | ||
| await handler.bootWorker(options); | ||
|
|
||
| return { | ||
| php: handler, | ||
| reap: () => { | ||
| try { | ||
| handler.dispose(); | ||
| } catch { | ||
| /** */ | ||
| } | ||
| try { | ||
| spawnedWorker.worker.terminate(); | ||
| } catch { | ||
| /** */ | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| process.on('unhandledRejection', (e: any) => { | ||
| logger.error('Unhandled rejection:', e); | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.