Skip to content

Commit

Permalink
fix(core): fix possible memory leak warning from task orchestrator
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Sep 21, 2020
1 parent 6c7b306 commit 6d9ff59
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/workspace/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { cliCommand } from '../core/file-utils';
import { ProjectGraph } from '../core/project-graph';
import { AffectedEventType, Task } from './tasks-runner';
import { getOutputs, unparse } from './utils';
import { fork } from 'child_process';
import { ChildProcess, fork } from 'child_process';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { output } from '../utils/output';
import * as path from 'path';
import * as fs from 'fs';
import { appRootPath } from '../utils/app-root';
import * as dotenv from 'dotenv';
Expand All @@ -16,11 +15,15 @@ export class TaskOrchestrator {
cache = new Cache(this.options);
cli = cliCommand();

private processes: ChildProcess[] = [];

constructor(
private readonly initiatingProject: string | undefined,
private readonly projectGraph: ProjectGraph,
private readonly options: DefaultTasksRunnerOptions
) {}
) {
this.setupOnProcessExitListener();
}

async run(tasksInStage: Task[]) {
const { cached, rest } = await this.splitTasksIntoCachedAndNotCached(
Expand Down Expand Up @@ -166,9 +169,7 @@ export class TaskOrchestrator {
stdio: ['inherit', 'pipe', 'pipe', 'ipc'],
env,
});
process.addListener('SIGINT', () => {
p.kill('SIGINT');
});
this.processes.push(p);
let out = [];
let outWithErr = [];
p.stdout.on('data', (chunk) => {
Expand Down Expand Up @@ -235,9 +236,7 @@ export class TaskOrchestrator {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env,
});
process.addListener('SIGINT', () => {
p.kill('SIGINT');
});
this.processes.push(p);
p.on('exit', (code) => {
if (code === null) code = 2;
// we didn't print any output as we were running the command
Expand Down Expand Up @@ -336,6 +335,15 @@ export class TaskOrchestrator {
...args,
];
}

private setupOnProcessExitListener() {
// Forward SIGINTs to all forked processes
process.addListener('SIGINT', () => {
this.processes.forEach((p) => {
p.kill('SIGINT');
});
});
}
}

function parseEnv(path: string) {
Expand Down

0 comments on commit 6d9ff59

Please sign in to comment.