Skip to content

Avoid reusing temp files between code runs#25

Open
flaviens wants to merge 1 commit into
formulahendry:mainfrom
flaviens:fix-temp-file-reuse
Open

Avoid reusing temp files between code runs#25
flaviens wants to merge 1 commit into
formulahendry:mainfrom
flaviens:fix-temp-file-reuse

Conversation

@flaviens

Copy link
Copy Markdown

Summary

  • Create a per-run temporary directory and source file instead of reusing one filename per language.
  • Execute the configured interpreter with execFile and an argument array.
  • Remove the temporary directory after each tool invocation completes.

Rationale

The previous implementation reused a shared temporary filename for each language. Overlapping run-code calls could interfere with each other, and temporary files were left behind after execution. This change isolates each invocation and cleans up its temporary files.

Validation

  • npm ci
  • npm run build
  • MCP in-memory smoke test with two concurrent run-code JavaScript calls returning independent alpha / beta outputs and leaving 0 new mcp-code-runner-* temp directories.

Note: npm ci reports existing dependency audit findings (2 moderate, 3 high); this PR does not change dependencies.

@flaviens
flaviens marked this pull request as ready for review June 13, 2026 03:02
Copilot AI review requested due to automatic review settings June 13, 2026 03:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens and cleans up the code-execution flow by avoiding shell invocation and ensuring temporary artifacts are removed after execution.

Changes:

  • Replaces exec with execFile and passes the script path as an argument (no shell command string).
  • Creates a unique temporary directory per run (via mkdtemp) and removes it in a finally block.
  • Adds a simple splitCommand() helper to support executors with arguments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/server.ts
Comment on lines +39 to +52
try {
const result = await executeCommand(executor, filePath);

return {
content: [
{
type: "text",
text: result,
},
],
};
return {
content: [
{
type: "text",
text: result,
},
],
};
} finally {
await fs.promises.rm(tmpDir, { recursive: true, force: true });
}
Comment thread src/server.ts
Comment on lines +86 to 100
if (!command) {
reject("Error: Executor is required.");
return;
}

console.debug(`Executing command: ${command} ${[...args, filePath].join(" ")}`);
execFile(command, [...args, filePath], (error: any, stdout: string, stderr: string) => {
if (error) {
reject(`Error: ${error.message}`);
return;
}
if (stderr) {
reject(`Stderr: ${stderr}`);
return;
}
Comment thread src/server.ts
Comment on lines +77 to +84
function splitCommand(command: string): string[] {
const parts = command.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) ?? [];
return parts.map((part) => part.replace(/^["']|["']$/g, ""));
}

async function executeCommand(executor: string, filePath: string): Promise<string> {
return new Promise((resolve, reject) => {
console.debug(`Executing command: ${command}`);
exec(command, (error: any, stdout: string, stderr: string) => {
const [command, ...args] = splitCommand(executor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants