Avoid reusing temp files between code runs#25
Open
flaviens wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
execwithexecFileand passes the script path as an argument (no shell command string). - Creates a unique temporary directory per run (via
mkdtemp) and removes it in afinallyblock. - 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 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 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 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
execFileand an argument array.Rationale
The previous implementation reused a shared temporary filename for each language. Overlapping
run-codecalls 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 cinpm run buildrun-codeJavaScript calls returning independentalpha/betaoutputs and leaving0newmcp-code-runner-*temp directories.Note:
npm cireports existing dependency audit findings (2 moderate,3 high); this PR does not change dependencies.