Skip to content

Commit 0793c2b

Browse files
committed
address some nick comments in the review
1 parent 8dc90d6 commit 0793c2b

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

packages/cli-v3/src/dev/devSupervisor.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,34 @@ class DevSupervisor implements WorkerRuntime {
104104
const enableProcessReuse =
105105
typeof this.options.config.experimental_processKeepAlive === "boolean"
106106
? this.options.config.experimental_processKeepAlive
107+
: typeof this.options.config.experimental_processKeepAlive === "object"
108+
? this.options.config.experimental_processKeepAlive.enabled
107109
: false;
108110

111+
const maxPoolSize =
112+
typeof this.options.config.experimental_processKeepAlive === "object"
113+
? this.options.config.experimental_processKeepAlive.devMaxPoolSize ?? 25
114+
: 25;
115+
116+
const maxExecutionsPerProcess =
117+
typeof this.options.config.experimental_processKeepAlive === "object"
118+
? this.options.config.experimental_processKeepAlive.maxExecutionsPerProcess ?? 50
119+
: 50;
120+
109121
if (enableProcessReuse) {
110122
logger.debug("[DevSupervisor] Enabling process reuse", {
111123
enableProcessReuse,
124+
maxPoolSize,
125+
maxExecutionsPerProcess,
112126
});
113127
}
114128

115129
this.taskRunProcessPool = new TaskRunProcessPool({
116130
env,
117131
cwd: this.options.config.workingDir,
118-
enableProcessReuse:
119-
typeof this.options.config.experimental_processKeepAlive === "boolean"
120-
? this.options.config.experimental_processKeepAlive
121-
: typeof this.options.config.experimental_processKeepAlive === "object"
122-
? this.options.config.experimental_processKeepAlive.enabled
123-
: false,
124-
maxPoolSize:
125-
typeof this.options.config.experimental_processKeepAlive === "object"
126-
? this.options.config.experimental_processKeepAlive.devMaxPoolSize ?? 25
127-
: 25,
128-
maxExecutionsPerProcess:
129-
typeof this.options.config.experimental_processKeepAlive === "object"
130-
? this.options.config.experimental_processKeepAlive.maxExecutionsPerProcess ?? 50
131-
: 50,
132+
enableProcessReuse,
133+
maxPoolSize,
134+
maxExecutionsPerProcess,
132135
});
133136

134137
this.socket = this.#createSocket();
@@ -355,6 +358,9 @@ class DevSupervisor implements WorkerRuntime {
355358
httpClient: this.options.client,
356359
logLevel: this.options.args.logLevel,
357360
taskRunProcessPool: this.taskRunProcessPool,
361+
cwd: this.options.config.experimental_devProcessCwdInBuildDir
362+
? worker.build.outputPath
363+
: undefined,
358364
onFinished: () => {
359365
logger.debug("[DevSupervisor] Run finished", { runId: message.run.friendlyId });
360366

packages/cli-v3/src/entryPoints/managed/controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,15 @@ export class ManagedRunController {
207207
runId: runFriendlyId,
208208
message: "killing existing execution before starting new run",
209209
});
210-
await this.currentExecution.kill().catch(() => {});
210+
211+
await this.currentExecution.shutdown().catch((error) => {
212+
this.sendDebugLog({
213+
runId: runFriendlyId,
214+
message: "Error during execution shutdown",
215+
properties: { error: error instanceof Error ? error.message : String(error) },
216+
});
217+
});
218+
211219
this.currentExecution = null;
212220
}
213221

packages/cli-v3/src/entryPoints/managed/execution.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,18 @@ export class RunExecution {
120120
}
121121

122122
if (exitExecution) {
123-
this.shutdown("kill");
123+
this.shutdownExecution("kill");
124124
}
125125
}
126126

127+
public async shutdown() {
128+
if (this.taskRunProcess) {
129+
await this.taskRunProcessProvider.handleProcessAbort(this.taskRunProcess);
130+
}
131+
132+
this.shutdownExecution("shutdown");
133+
}
134+
127135
/**
128136
* Prepares the execution with task run environment variables.
129137
* This should be called before executing, typically after a successful run to prepare for the next one.
@@ -232,7 +240,6 @@ export class RunExecution {
232240
if (this.currentAttemptNumber && this.currentAttemptNumber !== run.attemptNumber) {
233241
this.sendDebugLog("error: attempt number mismatch", snapshotMetadata);
234242
// This is a rogue execution, a new one will already have been created elsewhere
235-
// TODO: keep this one, kill the process even if it's a keep-alive one
236243
await this.exitTaskRunProcessWithoutFailingRun({
237244
flush: false,
238245
reason: "attempt number mismatch",
@@ -250,7 +257,6 @@ export class RunExecution {
250257
if (deprecated) {
251258
this.sendDebugLog("run execution is deprecated", { incomingSnapshot: snapshot });
252259

253-
// TODO: keep this one, kill the process even if it's a keep-alive one
254260
await this.exitTaskRunProcessWithoutFailingRun({
255261
flush: false,
256262
reason: "deprecated execution",
@@ -469,7 +475,7 @@ export class RunExecution {
469475
if (startError) {
470476
this.sendDebugLog("failed to start attempt", { error: startError.message });
471477

472-
this.shutdown("failed to start attempt");
478+
this.shutdownExecution("failed to start attempt");
473479
return;
474480
}
475481

@@ -480,12 +486,12 @@ export class RunExecution {
480486
if (executeError) {
481487
this.sendDebugLog("failed to execute run", { error: executeError.message });
482488

483-
this.shutdown("failed to execute run");
489+
this.shutdownExecution("failed to execute run");
484490
return;
485491
}
486492

487493
// This is here for safety, but it
488-
this.shutdown("execute call finished");
494+
this.shutdownExecution("execute call finished");
489495
}
490496

491497
private async executeRunWrapper({
@@ -786,7 +792,7 @@ export class RunExecution {
786792
if (startError) {
787793
this.sendDebugLog("failed to start attempt for retry", { error: startError.message });
788794

789-
this.shutdown("retryImmediately: failed to start attempt");
795+
this.shutdownExecution("retryImmediately: failed to start attempt");
790796
return;
791797
}
792798

@@ -797,7 +803,7 @@ export class RunExecution {
797803
if (executeError) {
798804
this.sendDebugLog("failed to execute run for retry", { error: executeError.message });
799805

800-
this.shutdown("retryImmediately: failed to execute run");
806+
this.shutdownExecution("retryImmediately: failed to execute run");
801807
return;
802808
}
803809
}
@@ -841,7 +847,7 @@ export class RunExecution {
841847
await this.taskRunProcessProvider.suspendProcess(flush, this.taskRunProcess);
842848

843849
// No services should be left running after this line - let's make sure of it
844-
this.shutdown(`exitTaskRunProcessWithoutFailingRun: ${reason}`);
850+
this.shutdownExecution(`exitTaskRunProcessWithoutFailingRun: ${reason}`);
845851
}
846852

847853
/**
@@ -1012,10 +1018,10 @@ export class RunExecution {
10121018
}
10131019

10141020
this.executionAbortController.abort();
1015-
this.shutdown("abortExecution");
1021+
this.shutdownExecution("abortExecution");
10161022
}
10171023

1018-
private shutdown(reason: string) {
1024+
private shutdownExecution(reason: string) {
10191025
if (this.isShuttingDown) {
10201026
this.sendDebugLog(`[shutdown] ${reason} (already shutting down)`, {
10211027
firstShutdownReason: this.shutdownReason,

packages/cli-v3/src/entryPoints/managed/taskRunProcessProvider.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ export class TaskRunProcessProvider {
205205
await process.cleanup(true);
206206
}
207207

208+
async killProcess(process: TaskRunProcess): Promise<void> {
209+
this.sendDebugLog("Killing process");
210+
211+
// If this was our persistent process, clear it
212+
if (this.persistentProcess?.pid === process.pid) {
213+
this.persistentProcess = null;
214+
this.executionCount = 0;
215+
}
216+
217+
// Kill the process
218+
await this.cleanupProcess(process);
219+
}
220+
208221
/**
209222
* Forces cleanup of any persistent process
210223
*/
@@ -287,7 +300,7 @@ export class TaskRunProcessProvider {
287300
}
288301

289302
private async cleanupProcess(taskRunProcess: TaskRunProcess): Promise<void> {
290-
if (taskRunProcess) {
303+
if (taskRunProcess && taskRunProcess.pid !== undefined) {
291304
this.sendDebugLog("Cleaning up TaskRunProcess", { pid: taskRunProcess.pid });
292305

293306
await taskRunProcess.kill("SIGKILL").catch(() => {});

0 commit comments

Comments
 (0)