-
Notifications
You must be signed in to change notification settings - Fork 86
apps/cli: surface child-process crashes instead of silent 2-min timeout #3159
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 1 commit
Commits
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
Next
Next commit
apps/cli: surface child-process crashes instead of silent 2-min timeout
When the WordPress server child process crashes on startup (e.g. due to a module import mismatch), `waitForReadyMessage` would block for the full 2-minute inactivity timeout and then throw a generic "Timeout waiting for ready message" error, burying the real cause. Listen for the child's exit event and for the pre-listener race, and reject with the child's stderr log path plus its tail so users see the actual error immediately. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Loading branch information
commit dfeda45719fd2cb88f6fc29fb523b30be52f6caa
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||||||
| * Manages WordPress server processes via process manager daemon. Each site runs in a separate | ||||||||||||||||||||||||||||
| * process that spawns Playground CLI. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| import fs from 'fs'; | ||||||||||||||||||||||||||||
| import path from 'path'; | ||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||
| PLAYGROUND_CLI_ACTIVITY_CHECK_INTERVAL, | ||||||||||||||||||||||||||||
|
|
@@ -20,6 +21,7 @@ import { | |||||||||||||||||||||||||||
| type DaemonBusEventMap, | ||||||||||||||||||||||||||||
| sendMessageToProcess, | ||||||||||||||||||||||||||||
| } from 'cli/lib/daemon-client'; | ||||||||||||||||||||||||||||
| import { PROCESS_MANAGER_LOGS_DIR } from 'cli/lib/paths'; | ||||||||||||||||||||||||||||
| import { ProcessDescription } from 'cli/lib/types/process-manager-ipc'; | ||||||||||||||||||||||||||||
| import { ServerConfig, ManagerMessagePayload } from 'cli/lib/types/wordpress-server-ipc'; | ||||||||||||||||||||||||||||
| import { Logger } from 'cli/logger'; | ||||||||||||||||||||||||||||
|
|
@@ -118,7 +120,7 @@ export async function startWordPressServer( | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const processDesc = await startProcess( processName, wordPressServerChildPath ); | ||||||||||||||||||||||||||||
| await waitForReadyMessage( processDesc.pmId ); | ||||||||||||||||||||||||||||
| await waitForReadyMessage( processName, processDesc.pmId ); | ||||||||||||||||||||||||||||
| await sendMessage( | ||||||||||||||||||||||||||||
| processDesc.pmId, | ||||||||||||||||||||||||||||
| processName, | ||||||||||||||||||||||||||||
|
|
@@ -132,11 +134,45 @@ export async function startWordPressServer( | |||||||||||||||||||||||||||
| return processDesc; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async function waitForReadyMessage( pmId: number ): Promise< void > { | ||||||||||||||||||||||||||||
| const CHILD_STDERR_TAIL_BYTES = 4096; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function readChildStderrTail( processName: string ): string { | ||||||||||||||||||||||||||||
| const errorLogPath = path.join( PROCESS_MANAGER_LOGS_DIR, `${ processName }-error.log` ); | ||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| const { size } = fs.statSync( errorLogPath ); | ||||||||||||||||||||||||||||
| if ( size === 0 ) { | ||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| const readBytes = Math.min( size, CHILD_STDERR_TAIL_BYTES ); | ||||||||||||||||||||||||||||
| const buffer = Buffer.alloc( readBytes ); | ||||||||||||||||||||||||||||
| const fd = fs.openSync( errorLogPath, 'r' ); | ||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| fs.readSync( fd, buffer, 0, readBytes, size - readBytes ); | ||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||
| fs.closeSync( fd ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return buffer.toString( 'utf8' ).trimEnd(); | ||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function buildChildExitedError( processName: string ): Error { | ||||||||||||||||||||||||||||
| const errorLogPath = path.join( PROCESS_MANAGER_LOGS_DIR, `${ processName }-error.log` ); | ||||||||||||||||||||||||||||
| const tail = readChildStderrTail( processName ); | ||||||||||||||||||||||||||||
| let message = `WordPress server child process exited before becoming ready. See ${ errorLogPath }`; | ||||||||||||||||||||||||||||
| if ( tail ) { | ||||||||||||||||||||||||||||
| message += `\n${ tail }`; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return new Error( message ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async function waitForReadyMessage( processName: string, pmId: number ): Promise< void > { | ||||||||||||||||||||||||||||
| const bus = await getDaemonBus(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| let timeoutId: NodeJS.Timeout; | ||||||||||||||||||||||||||||
| let readyHandler: ( packet: DaemonBusEventMap[ 'process-message' ] ) => void; | ||||||||||||||||||||||||||||
| let exitHandler: ( event: DaemonBusEventMap[ 'process-event' ] ) => void; | ||||||||||||||||||||||||||||
| let abortListener: () => void; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return new Promise< void >( ( resolve, reject ) => { | ||||||||||||||||||||||||||||
|
|
@@ -148,16 +184,33 @@ async function waitForReadyMessage( pmId: number ): Promise< void > { | |||||||||||||||||||||||||||
| resolve(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| exitHandler = ( event ) => { | ||||||||||||||||||||||||||||
| if ( event.process.pm_id === pmId && event.event === 'exit' ) { | ||||||||||||||||||||||||||||
| reject( buildChildExitedError( processName ) ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
Contributor
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. Nice 👍 |
||||||||||||||||||||||||||||
| abortListener = () => { | ||||||||||||||||||||||||||||
| reject( new Error( 'Operation aborted' ) ); | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| abortController.signal.addEventListener( 'abort', abortListener ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| bus.on( 'process-message', readyHandler ); | ||||||||||||||||||||||||||||
| bus.on( 'process-event', exitHandler ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Guard against the race where the child exits between `startProcess` resolving and | ||||||||||||||||||||||||||||
| // our listeners being attached: if the process is no longer running now, surface the | ||||||||||||||||||||||||||||
| // error immediately rather than waiting for the ready-message timeout. | ||||||||||||||||||||||||||||
| void ( async () => { | ||||||||||||||||||||||||||||
| const running = await isProcessRunning( processName ); | ||||||||||||||||||||||||||||
| if ( ! running ) { | ||||||||||||||||||||||||||||
| reject( buildChildExitedError( processName ) ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } )(); | ||||||||||||||||||||||||||||
|
Contributor
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.
Suggested change
Part syntax nitpicking, part being more explicit about what happens if we catch an exception here. |
||||||||||||||||||||||||||||
| } ).finally( () => { | ||||||||||||||||||||||||||||
| clearTimeout( timeoutId ); | ||||||||||||||||||||||||||||
| abortController.signal.removeEventListener( 'abort', abortListener ); | ||||||||||||||||||||||||||||
| bus.off( 'process-message', readyHandler ); | ||||||||||||||||||||||||||||
| bus.off( 'process-event', exitHandler ); | ||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -397,7 +450,7 @@ export async function runBlueprint( | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const processDesc = await startProcess( processName, wordPressServerChildPath ); | ||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| await waitForReadyMessage( processDesc.pmId ); | ||||||||||||||||||||||||||||
| await waitForReadyMessage( processName, processDesc.pmId ); | ||||||||||||||||||||||||||||
| await sendMessage( | ||||||||||||||||||||||||||||
| processDesc.pmId, | ||||||||||||||||||||||||||||
| processName, | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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.
This can be helpful, or it can be misleading (because the logs include contents from prior invocations)…
The best thing would probably be for the process manager daemon to forward the
stderroutput for the current invocation. That's not trivial, but we could probably achieve it by having the process manager daemon keep a rolling buffer of each child's stderr stream and then send the buffer's contents along with theexitevent inProcessManagerDaemon::handleProcessExit.Not something you have to do in this PR, but an agent can probably do an OK job of it.