Skip to content

Commit

Permalink
fix: clean up worker subprocess spawning
Browse files Browse the repository at this point in the history
fixes #1777
  • Loading branch information
warner committed Sep 21, 2020
1 parent e3c969b commit afeced8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
22 changes: 16 additions & 6 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions packages/SwingSet/src/spawnSubprocessWorker.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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]);
Expand Down

0 comments on commit afeced8

Please sign in to comment.