From afeced85a7f3523aed3655c3d498ecdc9314ef02 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Wed, 16 Sep 2020 12:46:31 -0700 Subject: [PATCH] fix: clean up worker subprocess spawning fixes #1777 --- packages/SwingSet/src/controller.js | 22 ++++++++++++++----- .../SwingSet/src/spawnSubprocessWorker.js | 10 ++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/SwingSet/src/controller.js b/packages/SwingSet/src/controller.js index e6ed646780e..ad6418803dd 100644 --- a/packages/SwingSet/src/controller.js +++ b/packages/SwingSet/src/controller.js @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; +import process from 'process'; import re2 from 're2'; import { Worker } from 'worker_threads'; import * as babelCore from '@babel/core'; @@ -353,6 +354,7 @@ export async function buildVatController( }`, ); + // this launches a worker in a Node.js thread (aka "Worker") function makeNodeWorker() { // TODO: after we move away from `-r esm` and use real ES6 modules, point // this at nodeWorkerSupervisor.js instead of the CJS intermediate @@ -362,17 +364,25 @@ export async function buildVatController( return new Worker(supercode); } + // launch a worker in a subprocess (which runs Node.js) + function startSubprocessWorkerNode() { + const supercode = require.resolve( + './kernel/vatManager/subprocessSupervisor.js', + ); + return startSubprocessWorker(process.execPath, ['-r', 'esm', supercode]); + } + + let startSubprocessWorkerXS; + const xsWorkerBin = locateWorkerBin({ resolve: path.resolve }); + if (fs.existsSync(xsWorkerBin)) { + startSubprocessWorkerXS = () => startSubprocessWorker(xsWorkerBin); + } + function writeSlogObject(_obj) { // TODO sqlite // console.log(`--slog ${JSON.stringify(obj)}`); } - const startSubprocessWorkerNode = () => startSubprocessWorker(); - const xsWorkerBin = locateWorkerBin({ resolve: path.resolve }); - const startSubprocessWorkerXS = fs.existsSync(xsWorkerBin) - ? () => startSubprocessWorker({ execPath: xsWorkerBin, args: [] }) - : undefined; - const kernelEndowments = { waitUntilQuiescent, hostStorage, diff --git a/packages/SwingSet/src/spawnSubprocessWorker.js b/packages/SwingSet/src/spawnSubprocessWorker.js index dbe5af44616..e02008d6225 100644 --- a/packages/SwingSet/src/spawnSubprocessWorker.js +++ b/packages/SwingSet/src/spawnSubprocessWorker.js @@ -1,5 +1,4 @@ // this file is loaded by the controller, in the start compartment -import process from 'process'; import { spawn } from 'child_process'; import Netstring from 'netstring-stream'; @@ -10,19 +9,14 @@ function parentLog(first, ...args) { // console.error(`--parent: ${first}`, ...args); } -const supercode = require.resolve( - './kernel/vatManager/subprocessSupervisor.js', -); // we send on fd3, and receive on fd4. We pass fd1/2 (stdout/err) through, so // console log/err from the child shows up normally. We don't use Node's // built-in serialization feature ('ipc') because the child process won't // always be Node. const stdio = harden(['inherit', 'inherit', 'inherit', 'pipe', 'pipe']); -export function startSubprocessWorker(options) { - const execPath = options.execPath || process.execPath; - const args = options.args || ['-r', 'esm', supercode]; - const proc = spawn(execPath, args, { stdio }); +export function startSubprocessWorker(execPath, procArgs = []) { + const proc = spawn(execPath, procArgs, { stdio }); const toChild = Netstring.writeStream(); toChild.pipe(proc.stdio[3]);