Only resolve and destroy streams on job close

This commit is contained in:
Omar Brikaa 2022-09-30 14:22:29 +02:00
parent b98b208d0d
commit 993d5c6fb0
1 changed files with 19 additions and 10 deletions

View File

@ -126,6 +126,11 @@ class Job {
this.#active_timeouts = []; this.#active_timeouts = [];
this.logger.debug('Cleared the active timeouts'); this.logger.debug('Cleared the active timeouts');
this.cleanup_processes();
this.logger.debug(`Finished exit cleanup`);
}
close_cleanup() {
for (const proc of this.#active_parent_processes) { for (const proc of this.#active_parent_processes) {
proc.stderr.destroy(); proc.stderr.destroy();
if (!proc.stdin.destroyed) { if (!proc.stdin.destroyed) {
@ -135,10 +140,7 @@ class Job {
proc.stdout.destroy(); proc.stdout.destroy();
} }
this.#active_parent_processes = []; this.#active_parent_processes = [];
this.logger.debug('Destroyed parent processes writables'); this.logger.debug('Destroyed processes writables');
this.cleanup_processes();
this.logger.debug(`Finished exit cleanup`);
} }
async safe_call(file, args, timeout, memory_limit, eventBus = null) { async safe_call(file, args, timeout, memory_limit, eventBus = null) {
@ -153,7 +155,10 @@ class Job {
]; ];
const timeout_call = [ const timeout_call = [
'timeout', '-s', '9', Math.ceil(timeout / 1000), 'timeout',
'-s',
'9',
Math.ceil(timeout / 1000),
]; ];
if (memory_limit >= 0) { if (memory_limit >= 0) {
@ -231,14 +236,17 @@ class Job {
} }
}); });
proc.on('exit', (code, signal) => { proc.on('exit', () => this.exit_cleanup());
this.exit_cleanup();
proc.on('close', (code, signal) => {
this.close_cleanup();
resolve({ stdout, stderr, code, signal, output }); resolve({ stdout, stderr, code, signal, output });
}); });
proc.on('error', err => { proc.on('error', err => {
this.exit_cleanup(); this.exit_cleanup();
this.close_cleanup();
reject({ error: err, stdout, stderr, output }); reject({ error: err, stdout, stderr, output });
}); });
@ -370,11 +378,11 @@ class Job {
const proc_id_int = parse_int(proc_id); const proc_id_int = parse_int(proc_id);
// Skip over any processes that aren't ours. // Skip over any processes that aren't ours.
if(ruid != this.uid && euid != this.uid) return -1; if (ruid != this.uid && euid != this.uid) return -1;
if (state == 'Z'){ if (state == 'Z') {
// Zombie process, just needs to be waited, regardless of the user id // Zombie process, just needs to be waited, regardless of the user id
if(!to_wait.includes(proc_id_int)) if (!to_wait.includes(proc_id_int))
to_wait.push(proc_id_int); to_wait.push(proc_id_int);
return -1; return -1;
@ -466,6 +474,7 @@ class Job {
this.logger.info(`Cleaning up job`); this.logger.info(`Cleaning up job`);
this.exit_cleanup(); // Run process janitor, just incase there are any residual processes somehow this.exit_cleanup(); // Run process janitor, just incase there are any residual processes somehow
this.close_cleanup();
await this.cleanup_filesystem(); await this.cleanup_filesystem();
remaining_job_spaces++; remaining_job_spaces++;