Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions services/code.service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* globals gc */
const util = require('util')
const exec = util.promisify(require('child_process').exec)
Expand Down Expand Up @@ -227,12 +228,15 @@ const _executeCode = async (req, res, response) => {

// Check if there is no compilation error
if (response.compileMessage === '') {
let command
const memoryReportPath = `/tmp/memory_report_${process.pid}_${Date.now()}.txt`;

let command;
if (language === 'java') {
// Remove ulimit as a temp fix
command = `cd /tmp/ && timeout ${langConfig.timeout}s ${langConfig.run}`
command = `cd /tmp/ && /usr/bin/time -f "%M" -o ${memoryReportPath} timeout ${langConfig.timeout}s ${langConfig.run}`;
} else {
command = `cd /tmp/ && ulimit -v ${langConfig.memory} && ulimit -m ${langConfig.memory} && timeout ${langConfig.timeout}s ${langConfig.run}`
// Execute command with memory limits and resource monitoring for non-Java languages
command = `cd /tmp/ && ulimit -v ${langConfig.memory} && ulimit -m ${langConfig.memory} && /usr/bin/time -f "%M" -o ${memoryReportPath} timeout ${langConfig.timeout}s ${langConfig.run}`;
}

// Check if there is any input that is to be provided to code execution
Expand All @@ -244,14 +248,36 @@ const _executeCode = async (req, res, response) => {
}

const outputLog = await _runScript(command, res, true)

let memoryKB = null
try {
await fs.promises.access(memoryReportPath, fs.constants.F_OK)
const memoryReport = await fs.promises.readFile(memoryReportPath, 'utf8')
memoryKB = parseInt(memoryReport.trim(), 10)
} catch (err) {
console.error(`Memory report not found or failed to read: ${err.message}`)
}

const isAlpine = fs.existsSync('/etc/alpine-release');

if (memoryKB) {
response.memory = isAlpine ? memoryKB / 4 : memoryKB;
} else {
response.memory = null;
}

console.log('Memory used:', response.memory);

response.output =
outputLog.error !== undefined
? _prepareErrorMessage(outputLog, language, command)
: outputLog.result.stdout

if (outputLog.error) {
response.error = 1
}
} else {
}
else {
response.error = 1
}
} catch (e) {
Expand Down Expand Up @@ -399,7 +425,7 @@ const _getAiScore = async (langConfig, question, response, points, userAnswer, r

const _executeStatement = (db, sql) => {
return new Promise((resolve, reject) => {
db.all(sql, function(err, rows) {
db.all(sql, function (err, rows) {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -887,14 +913,14 @@ const _postCleanUp = async (type, staticServerInstance = undefined, jasmineServe
await _cleanUpDir(appConfig.multifile.workingDir, appConfig.multifile.submissionFileDownloadPath)
switch (type) {
case FRONTEND_STATIC_JASMINE:
if(staticServerInstance) {
if (staticServerInstance) {
staticServerInstance.close(() => {
logger.info('Exiting static server in post cleanup')
})
}
break
case FRONTEND_REACT_JASMINE:
if(jasmineServer) {
if (jasmineServer) {
logger.info('Exiting react setup server in post cleanup')
process.kill(-jasmineServer.pid)
}
Expand All @@ -917,7 +943,7 @@ const _executeMultiFile = async (req, res, response) => {
let result
if (req?.non_editable_files) {
const isValidSubmission = await _checkIntegrity(req.non_editable_files)
if(!isValidSubmission) throw new Error(`A non editable file has been modified, exiting...`)
if (!isValidSubmission) throw new Error(`A non editable file has been modified, exiting...`)
}
switch (req.type) {
case FRONTEND_STATIC_JASMINE:
Expand Down
Loading