@@ -422,7 +422,13 @@ async function executeCode(language, code, stdin, expectedOutput, runTests = fal
422422 }
423423
424424 try {
425- output = await runProgram ( executionConfig . runCommand , executionConfig . runArgs , stdin ) ;
425+ output = await runProgram (
426+ executionConfig . runCommand ,
427+ executionConfig . runArgs ,
428+ stdin ,
429+ 3000 ,
430+ uniqueDir // pass temp dir
431+ ) ;
426432 } catch ( executionError ) {
427433 console . error ( 'Test execution failed:' , executionError ) ;
428434 response . state = 'failed' ;
@@ -434,7 +440,13 @@ async function executeCode(language, code, stdin, expectedOutput, runTests = fal
434440 }
435441 } else {
436442 try {
437- output = await runProgram ( executionConfig . runCommand , executionConfig . runArgs , stdin ) ;
443+ output = await runProgram (
444+ executionConfig . runCommand ,
445+ executionConfig . runArgs ,
446+ stdin ,
447+ 3000 ,
448+ uniqueDir // pass temp dir
449+ ) ;
438450 } catch ( executionError ) {
439451 console . error ( 'Program execution failed:' , executionError ) ;
440452 if ( language . toLowerCase ( ) === 'python' ) {
@@ -548,7 +560,7 @@ function compileCode(command, args, cwd) {
548560 * @param {number } [timeout=3000] - Timeout in milliseconds.
549561 * @returns {Promise<string> } - The program's stdout.
550562 */
551- function runProgram ( command , args , stdin = '' , timeout = 3000 ) {
563+ function runProgram ( command , args , stdin = '' , timeout = 3000 , workingDir = null ) {
552564 return new Promise ( ( resolve , reject ) => {
553565 const shell = 'bash' ;
554566 const wrapperArgs = [
@@ -565,24 +577,11 @@ function runProgram(command, args, stdin = '', timeout = 3000) {
565577 let killedByEvaluator = false ;
566578
567579 const proc = spawn ( shell , wrapperArgs , {
568- cwd : path . dirname ( command ) ,
580+ cwd : workingDir || process . cwd ( ) , // fixed
569581 detached : true ,
570- stdio : [ 'pipe' , 'pipe' , 'pipe' ]
582+ stdio : [ 'pipe' , 'pipe' , 'pipe' ]
571583 } ) ;
572584
573- // Resource monitor (memory/threads)
574- const interval = setInterval ( async ( ) => {
575- try {
576- // Example: use pidusage or ps to get memory/threads
577- // let info = await pidusage(proc.pid);
578- // if (info.memory > 300 * 1024 * 1024 || info.threadCount > 200) {
579- // killedByEvaluator = true;
580- // killGroup(proc.pid);
581- // clearInterval(interval);
582- // }
583- } catch ( _ ) { }
584- } , 100 ) ;
585-
586585 const killGroup = ( pid ) => {
587586 killedByEvaluator = true ;
588587 try { process . kill ( - pid , 'SIGTERM' ) ; } catch ( _ ) { }
@@ -592,7 +591,6 @@ function runProgram(command, args, stdin = '', timeout = 3000) {
592591 const timer = setTimeout ( ( ) => {
593592 if ( ! finished ) {
594593 killGroup ( proc . pid ) ;
595- clearInterval ( interval ) ;
596594 finished = true ;
597595 return reject ( new Error ( 'Execution timed out' ) ) ;
598596 }
@@ -610,10 +608,8 @@ function runProgram(command, args, stdin = '', timeout = 3000) {
610608 if ( finished ) return ;
611609
612610 clearTimeout ( timer ) ;
613- clearInterval ( interval ) ;
614611 finished = true ;
615612
616- // If killed via signal (timeout/resource limits)
617613 if ( signal || killedByEvaluator ) {
618614 const reason = signal
619615 ? `terminated by signal ${ signal } `
@@ -624,7 +620,6 @@ function runProgram(command, args, stdin = '', timeout = 3000) {
624620 return reject ( err ) ;
625621 }
626622
627- // Normal exit code check
628623 if ( code !== 0 ) {
629624 const err = new Error ( `Execution failed with code ${ code } ` ) ;
630625 err . stdout = stdout ;
@@ -638,7 +633,6 @@ function runProgram(command, args, stdin = '', timeout = 3000) {
638633 proc . on ( 'error' , err => {
639634 if ( finished ) return ;
640635 clearTimeout ( timer ) ;
641- clearInterval ( interval ) ;
642636 finished = true ;
643637 reject ( new Error ( `Failed to start process: ${ err . message } ` ) ) ;
644638 } ) ;
0 commit comments