fix: make pid optional in RuntimeWorker interface

pid may be undefined if worker hasn't started yet.
Update implementations to return number | undefined.
This commit is contained in:
Koushik Dutta
2026-04-02 12:51:33 -07:00
parent 621c5537d4
commit 2763f4af2c
3 changed files with 20 additions and 20 deletions

View File

@@ -5,7 +5,7 @@ import { RuntimeWorker, RuntimeWorkerOptions } from "./runtime-worker";
export abstract class ChildProcessWorker extends EventEmitter implements RuntimeWorker {
public pluginId: string;
protected worker: child_process.ChildProcess;
protected worker: child_process.ChildProcess | undefined;
killPromise: Promise<void>;
get childProcess() {
@@ -19,18 +19,18 @@ export abstract class ChildProcessWorker extends EventEmitter implements Runtime
}
setupWorker() {
this.worker.on('close', () => this.emit('error', new Error('close')));
this.worker.on('disconnect', () => this.emit('error', new Error('disconnect')));
this.worker.on('exit', (code, signal) => this.emit('exit', code, signal));
this.worker.on('error', e => this.emit('error', e));
this.worker!.on('close', () => this.emit('error', new Error('close')));
this.worker!.on('disconnect', () => this.emit('error', new Error('disconnect')));
this.worker!.on('exit', (code, signal) => this.emit('exit', code, signal));
this.worker!.on('error', e => this.emit('error', e));
// aggressively catch errors
// ECONNRESET can be raised when the child process is killed
for (const stdio of this.worker.stdio || []) {
for (const stdio of this.worker!.stdio || []) {
if (stdio)
stdio.on('error', e => this.emit('error', e));
}
this.killPromise = once(this.worker, 'exit').then(() => { }).catch(() => { });
this.killPromise = once(this.worker!, 'exit').then(() => { }).catch(() => { });
}
get pid() {
@@ -38,11 +38,11 @@ export abstract class ChildProcessWorker extends EventEmitter implements Runtime
}
get stdout() {
return this.worker.stdout;
return this.worker!.stdout!;
}
get stderr() {
return this.worker.stderr;
return this.worker!.stderr!;
}
kill(): void {

View File

@@ -13,7 +13,7 @@ import { RuntimeWorkerOptions } from "./runtime-worker";
export class PythonRuntimeWorker extends ChildProcessWorker {
static {
if (!fs.existsSync(process.env.SCRYPTED_PYTHON_PATH)) {
if (!process.env.SCRYPTED_PYTHON_PATH || !fs.existsSync(process.env.SCRYPTED_PYTHON_PATH)) {
try {
const py = new PortablePython(packagedPythonVersion);
const portablePython = py.executablePath;
@@ -99,7 +99,7 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
const setup = () => {
const types = require.resolve('@scrypted/types');
const PYTHONPATH = types.substring(0, types.indexOf('types') + 'types'.length);
this.worker = child_process.spawn(pythonPath, args, {
this.worker = child_process.spawn(pythonPath as string, args, {
cwd: options.unzippedPath,
// stdin, stdout, stderr, peer in, peer out
stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'],
@@ -112,8 +112,8 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
});
this.setupWorker();
this.worker.stdout.pipe(this.stdout);
this.worker.stderr.pipe(this.stderr);
this.worker.stdout!.pipe(this.stdout);
this.worker.stderr!.pipe(this.stderr);
};
let pluginPythonVersion: string = options.packageJson.scrypted.pythonVersion?.[os.platform()]?.[os.arch()] || options.packageJson.scrypted.pythonVersion?.default;
@@ -125,8 +125,8 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
// if the plugin requests a specific python, then install it via portable python
if (!pluginPythonVersion) {
setup();
this.peerin = this.worker.stdio[3] as Writable;
this.peerout = this.worker.stdio[4] as Readable;
this.peerin = this.worker!.stdio[3]! as Writable;
this.peerout = this.worker!.stdio[4]! as Readable;
return;
}
@@ -135,8 +135,8 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
if (envPython && fs.existsSync(envPython)) {
pythonPath = envPython;
setup();
this.peerin = this.worker.stdio[3] as Writable;
this.peerout = this.worker.stdio[4] as Readable;
this.peerin = this.worker!.stdio[3]! as Writable;
this.peerout = this.worker!.stdio[4]! as Readable;
return;
}
@@ -146,8 +146,8 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
const finishSetup = () => {
setup();
peerin.pipe(this.worker.stdio[3] as Writable);
(this.worker.stdio[4] as Readable).pipe(peerout);
peerin.pipe(this.worker!.stdio[3]! as Writable);
(this.worker!.stdio[4]! as Readable).pipe(peerout);
};
const pyVersion = require('py/package.json').version;

View File

@@ -13,7 +13,7 @@ export interface RuntimeWorkerOptions {
}
export interface RuntimeWorker {
pid: number;
pid: number | undefined;
stdout: Readable;
stderr: Readable;
killPromise: Promise<any>;