From 3a8dcb8093dfc7bb079b907e7455e6d3f1682208 Mon Sep 17 00:00:00 2001 From: catzhang Date: Sat, 25 Jul 2020 16:17:06 -0700 Subject: [PATCH 1/2] Replace fs sync operations with async versions --- src/server/container.ts | 64 ++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/server/container.ts b/src/server/container.ts index 963ac00..fc20b14 100644 --- a/src/server/container.ts +++ b/src/server/container.ts @@ -122,11 +122,21 @@ export default class Container { private saveCodeFiles = (code: string, includeFileData: Buffer | null): void => { // Create data directory and save code from request console.log(`${this.logPrefix}Saving code to ${this.codeHostPath}`); - if (!fs.existsSync(this.dataHostPath)) fs.mkdirSync(this.dataHostPath); - fs.writeFileSync(this.codeHostPath, code); + fs.access(this.dataHostPath, fs.constants.F_OK, (errPathExists) => { + if (errPathExists) { + fs.mkdir(this.dataHostPath, (err) => { if (err) throw err; }); + } + }); + fs.writeFile(this.codeHostPath, code, (err) => { + if (err) throw err; + console.log(`${this.logPrefix}Saved file to ${this.codeHostPath}`); + }); if (includeFileData) { console.log(`${this.logPrefix}Writing include file to ${this.includeFileHostPath}`); - fs.writeFileSync(this.includeFileHostPath, includeFileData); + fs.writeFile(this.includeFileHostPath, includeFileData, (err) => { + if (err) throw err; + console.log(`${this.logPrefix}Saved file to ${this.includeFileHostPath}`); + }); } }; @@ -311,8 +321,12 @@ export default class Container { // Remove uploaded file. We don't care about errors, in case the file // was already removed (or was never successfully created to begin // with) - try { fs.unlinkSync(this.codeHostPath); } catch { /* ignore */ } - try { fs.unlinkSync(this.includeFileHostPath); } catch { /* ignore */ } + fs.unlink(this.codeHostPath, () => { + console.log(`${this.logPrefix}Deleted ${this.codeHostPath}`); + }); + fs.unlink(this.includeFileHostPath, () => { + console.log(`${this.logPrefix}Deleted ${this.includeFileHostPath}`); + }); // Shut down gdb server, if it's running if (this.gdbMiServer) { this.gdbMiServer.close(); @@ -478,27 +492,25 @@ export default class Container { // have started yet, and there's not much we can do if (!this.containerId) return; - let cpuUsageNs; - try { - // TODO: get rid of synchronous read - cpuUsageNs = parseInt( - fs.readFileSync( - `/sys/fs/cgroup/cpu/docker/${this.containerId}/cpuacct.usage`, - ).toString(), - 10, - ); - } catch (exc) { - console.warn(`${this.logPrefix}Error loading cgroup CPU usage!`, exc); - return; - } - const cpuUsageMs = cpuUsageNs / 1000000; - console.debug(`${this.logPrefix}Current CPU time used: ${cpuUsageMs}ms`); - if (cpuUsageMs > MAX_CPU_TIME) { - console.warn(`${this.logPrefix}Container ${this.containerName} exceeded its CPU ` - + 'quota! Killing container'); - childProcess.execFile('docker', ['kill', this.containerName]); - this.showErrorBanner('The program exceeded its CPU quota.'); - } + fs.readFile( + `/sys/fs/cgroup/cpu/docker/${this.containerId}/cpuacct.usage`, + (err, data) => { + if (err) { + console.warn(`${this.logPrefix}Error loading cgroup CPU usage!`, err); + return; + } + + const cpuUsageNs = parseInt(data.toString(), 10); + const cpuUsageMs = cpuUsageNs / 1000000; + console.debug(`${this.logPrefix}Current CPU time used: ${cpuUsageMs}ms`); + if (cpuUsageMs > MAX_CPU_TIME) { + console.warn(`${this.logPrefix}Container ${this.containerName} exceeded its CPU ` + + 'quota! Killing container'); + childProcess.execFile('docker', ['kill', this.containerName]); + this.showErrorBanner('The program exceeded its CPU quota.'); + } + } + ); }, 1000); }; From 2535f14b8a95c7a825963265d077ee9d4878ba3f Mon Sep 17 00:00:00 2001 From: catzhang Date: Sat, 25 Jul 2020 16:29:56 -0700 Subject: [PATCH 2/2] Fix lint errors --- src/server/container.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/container.ts b/src/server/container.ts index 7a68eca..601b6ed 100644 --- a/src/server/container.ts +++ b/src/server/container.ts @@ -327,10 +327,10 @@ export default class Container { // with) fs.unlink(this.codeHostPath, () => { console.log(`${this.logPrefix}Deleted ${this.codeHostPath}`); - }); + }); fs.unlink(this.includeFileHostPath, () => { console.log(`${this.logPrefix}Deleted ${this.includeFileHostPath}`); - }); + }); // Shut down gdb server, if it's running if (this.gdbMiServer) { this.gdbMiServer.close(); @@ -497,7 +497,7 @@ export default class Container { fs.readFile( `/sys/fs/cgroup/cpu/docker/${this.containerId}/cpuacct.usage`, - (err, data) => { + (err, data) => { if (err) { console.warn(`${this.logPrefix}Error loading cgroup CPU usage!`, err); return; @@ -512,7 +512,7 @@ export default class Container { childProcess.execFile('docker', ['kill', this.containerName]); this.showErrorBanner('The program exceeded its CPU quota.'); } - } + }, ); }, 1000); };