[sandbox] Catch broken pipes
This commit is contained in:
parent
6e673cf390
commit
bd1623c09c
@ -11,7 +11,13 @@ import { runCpp } from './run/cpp.js';
|
||||
|
||||
export const timeoutSeconds = 30;
|
||||
|
||||
const RunResultKind = z.enum(['CompileFailed', 'TimeLimitExceeded', 'Completed', 'SandboxError', 'RunError']);
|
||||
const RunResultKind = z.enum([
|
||||
'CompileFailed',
|
||||
'TimeLimitExceeded',
|
||||
'Completed',
|
||||
'SandboxError',
|
||||
'RunError'
|
||||
]);
|
||||
export type RunResultKind = z.infer<typeof RunResultKind>;
|
||||
|
||||
const RunResult = z
|
||||
|
@ -4,7 +4,6 @@ import util from 'util';
|
||||
import { RunResult, timeoutSeconds } from '../index.js';
|
||||
import { IRunner, IRunnerParams, IRunnerReturn } from './types.js';
|
||||
import kill from 'tree-kill';
|
||||
import * as fs from 'fs';
|
||||
|
||||
const execPromise = util.promisify(exec);
|
||||
|
||||
|
@ -14,76 +14,80 @@ export const runCSharp: IRunner = async function (params: {
|
||||
console.log(`- RUN: ${params.srcDir}`);
|
||||
const child = spawn('dotnet run', { shell: true, cwd: params.srcDir });
|
||||
|
||||
let outputBuffer = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stdout.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
params.outputCallback?.(data.toString());
|
||||
});
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
params.outputCallback?.(data.toString());
|
||||
});
|
||||
try {
|
||||
let outputBuffer = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stdout.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
params.outputCallback?.(data.toString());
|
||||
});
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
params.outputCallback?.(data.toString());
|
||||
});
|
||||
|
||||
let runStartTime = performance.now();
|
||||
child.stdin.write(params.input);
|
||||
child.stdin.end();
|
||||
let runStartTime = performance.now();
|
||||
child.stdin.write(params.input);
|
||||
child.stdin.end();
|
||||
|
||||
let timeLimitExceeded = false;
|
||||
let completedNormally = false;
|
||||
let timeLimitExceeded = false;
|
||||
let completedNormally = false;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
runResult: new Promise<RunResult>((resolve) => {
|
||||
child.on('close', () => {
|
||||
completedNormally = !timeLimitExceeded;
|
||||
return {
|
||||
success: true,
|
||||
runResult: new Promise<RunResult>((resolve) => {
|
||||
child.on('close', () => {
|
||||
completedNormally = !timeLimitExceeded;
|
||||
|
||||
let runEndTime = performance.now();
|
||||
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
||||
let runEndTime = performance.now();
|
||||
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
||||
|
||||
if (completedNormally) {
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve({
|
||||
kind: 'Completed',
|
||||
output: outputBuffer,
|
||||
exitCode: child.exitCode!,
|
||||
runtimeMilliseconds
|
||||
});
|
||||
} else {
|
||||
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
|
||||
resolve({
|
||||
kind: 'TimeLimitExceeded',
|
||||
output: outputBuffer,
|
||||
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
|
||||
});
|
||||
}
|
||||
});
|
||||
if (completedNormally) {
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve({
|
||||
kind: 'Completed',
|
||||
output: outputBuffer,
|
||||
exitCode: child.exitCode!,
|
||||
runtimeMilliseconds
|
||||
});
|
||||
} else {
|
||||
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
|
||||
resolve({
|
||||
kind: 'TimeLimitExceeded',
|
||||
output: outputBuffer,
|
||||
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let timeoutHandle = setTimeout(() => {
|
||||
if (completedNormally) {
|
||||
return;
|
||||
}
|
||||
let timeoutHandle = setTimeout(() => {
|
||||
if (completedNormally) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
||||
timeLimitExceeded = true;
|
||||
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
||||
timeLimitExceeded = true;
|
||||
|
||||
child.stdin.end();
|
||||
child.stdin.destroy();
|
||||
child.stdout.destroy();
|
||||
child.stderr.destroy();
|
||||
child.stdin.end();
|
||||
child.stdin.destroy();
|
||||
child.stdout.destroy();
|
||||
child.stderr.destroy();
|
||||
if (child.pid !== undefined) {
|
||||
kill(child.pid);
|
||||
}
|
||||
}, timeoutSeconds * 1000);
|
||||
}),
|
||||
killFunc() {
|
||||
if (child.pid !== undefined) {
|
||||
kill(child.pid);
|
||||
}
|
||||
}, timeoutSeconds * 1000);
|
||||
}),
|
||||
killFunc() {
|
||||
if (child.pid !== undefined) {
|
||||
if (!completedNormally && !timeLimitExceeded) {
|
||||
kill(child.pid);
|
||||
params.outputCallback?.('\n[Manually stopped]');
|
||||
if (!completedNormally && !timeLimitExceeded) {
|
||||
kill(child.pid);
|
||||
params.outputCallback?.('\n[Manually stopped]');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
} catch (error) {
|
||||
return { success: false, runResult: { kind: 'RunError' } };
|
||||
}
|
||||
};
|
||||
|
@ -35,73 +35,77 @@ export const runJava: IRunner<IRunnerParamsJava> = async function (
|
||||
console.log(`- RUN: ${params.mainClass}`);
|
||||
const runCommand = `java -cp "${join(params.srcDir, 'build')}" ${params.mainClass}`;
|
||||
|
||||
let outputBuffer = '';
|
||||
const child = spawn(runCommand, { shell: true });
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stdout.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
});
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
});
|
||||
try {
|
||||
let outputBuffer = '';
|
||||
const child = spawn(runCommand, { shell: true });
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stdout.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
});
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
outputBuffer += data.toString();
|
||||
});
|
||||
|
||||
let runStartTime = performance.now();
|
||||
child.stdin.write(params.input);
|
||||
child.stdin.end();
|
||||
let runStartTime = performance.now();
|
||||
child.stdin.write(params.input);
|
||||
child.stdin.end();
|
||||
|
||||
let timeLimitExceeded = false;
|
||||
let completedNormally = false;
|
||||
let timeLimitExceeded = false;
|
||||
let completedNormally = false;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
runResult: new Promise<RunResult>(async (resolve) => {
|
||||
child.on('close', () => {
|
||||
completedNormally = !timeLimitExceeded;
|
||||
return {
|
||||
success: true,
|
||||
runResult: new Promise<RunResult>(async (resolve) => {
|
||||
child.on('close', () => {
|
||||
completedNormally = !timeLimitExceeded;
|
||||
|
||||
let runEndTime = performance.now();
|
||||
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
||||
let runEndTime = performance.now();
|
||||
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
||||
|
||||
if (completedNormally) {
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve({
|
||||
kind: 'Completed',
|
||||
output: outputBuffer,
|
||||
exitCode: child.exitCode!,
|
||||
runtimeMilliseconds
|
||||
});
|
||||
} else {
|
||||
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
|
||||
resolve({
|
||||
kind: 'TimeLimitExceeded',
|
||||
output: outputBuffer,
|
||||
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
|
||||
});
|
||||
}
|
||||
});
|
||||
if (completedNormally) {
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve({
|
||||
kind: 'Completed',
|
||||
output: outputBuffer,
|
||||
exitCode: child.exitCode!,
|
||||
runtimeMilliseconds
|
||||
});
|
||||
} else {
|
||||
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
|
||||
resolve({
|
||||
kind: 'TimeLimitExceeded',
|
||||
output: outputBuffer,
|
||||
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let timeoutHandle = setTimeout(() => {
|
||||
if (completedNormally) {
|
||||
return;
|
||||
}
|
||||
let timeoutHandle = setTimeout(() => {
|
||||
if (completedNormally) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
||||
timeLimitExceeded = true;
|
||||
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
||||
timeLimitExceeded = true;
|
||||
|
||||
child.stdin.end();
|
||||
child.stdin.destroy();
|
||||
child.stdout.destroy();
|
||||
child.stderr.destroy();
|
||||
child.kill('SIGKILL');
|
||||
}, timeoutSeconds * 1000);
|
||||
}),
|
||||
killFunc() {
|
||||
if (child.pid !== undefined) {
|
||||
if (!completedNormally && !timeLimitExceeded) {
|
||||
kill(child.pid);
|
||||
params.outputCallback?.('\n[Manually stopped]');
|
||||
child.stdin.end();
|
||||
child.stdin.destroy();
|
||||
child.stdout.destroy();
|
||||
child.stderr.destroy();
|
||||
child.kill('SIGKILL');
|
||||
}, timeoutSeconds * 1000);
|
||||
}),
|
||||
killFunc() {
|
||||
if (child.pid !== undefined) {
|
||||
if (!completedNormally && !timeLimitExceeded) {
|
||||
kill(child.pid);
|
||||
params.outputCallback?.('\n[Manually stopped]');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
} catch (error) {
|
||||
return { success: false, runResult: { kind: 'RunError' } };
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user