Skip to content

feat: implement process isolation for JavaScript execution to prevent hangs#5

Open
my-local-agent[bot] wants to merge 1 commit into
mainfrom
feat/process-isolation-for-js-execution
Open

feat: implement process isolation for JavaScript execution to prevent hangs#5
my-local-agent[bot] wants to merge 1 commit into
mainfrom
feat/process-isolation-for-js-execution

Conversation

@my-local-agent

Copy link
Copy Markdown

Summary

This PR addresses issue xpert-ai#234 by implementing true process isolation for JavaScript code execution, preventing indefinite hangs from affecting the main process. Instead of adding a timeout parameter (which is a band-aid solution), this approach provides robust process-level protection.

Problem

The current implementation executes JavaScript code directly in the main process using isolated-vm. This has several issues:

  1. No reliable timeout: isolated-vm's timeout parameter doesn't work in all scenarios (e.g., when waiting for promises)
  2. Process vulnerability: Hanging code can block the entire main process
  3. Inconsistent architecture: Python execution already uses process isolation, but JavaScript doesn't

Solution

Created a dedicated worker process (js-worker.js) that:

  • Runs isolated-vm code in a separate process
  • Communicates via stdin/stdout
  • Can be forcefully terminated with SIGKILL on timeout
  • Provides the same architecture as Python execution

Changes

New Files

  • packages/server-ai/src/sandbox/commands/handlers/js-worker.js: Worker process for isolated JavaScript execution
  • packages/server-ai/src/sandbox/commands/handlers/vm.handler.spec.ts: Unit tests for the new implementation

Modified Files

  • packages/server-ai/src/sandbox/commands/handlers/vm.handler.ts: Refactored to use worker process instead of direct execution
  • packages/server-ai/project.json: Added .js worker files to build assets

Key Benefits

True Process Isolation: Code crashes/hangs won't affect the main process
Reliable Timeout: Worker process can be killed anytime (5-second timeout, consistent with Python)
Memory Protection: Worker has separate memory space with 128MB limit
Consistent Architecture: Matches Python execution pattern
No Breaking Changes: Maintains the same API interface

Testing

  • Added comprehensive unit tests covering:
    • Simple code execution
    • Complex return values
    • Empty parameters
    • Syntax and runtime error handling
    • Timeout scenarios (infinite loops)

Notes

The worker includes robust module resolution to find isolated-vm in various build environments, ensuring compatibility with different deployment setups.

Fixes xpert-ai#234

… hangs

- Create js-worker.js: isolated worker process for JavaScript code execution using isolated-vm
- Modify vm.handler.ts: spawn worker process instead of running code directly in main process
- Add process-level timeout protection (5 seconds, consistent with Python)
- Add vm.handler.spec.ts: unit tests for JavaScript execution
- Update project.json: include .js worker files in build assets

This addresses issue xpert-ai#234 by providing true process isolation for JavaScript code execution,
preventing indefinite hangs from affecting the main process. The worker process can be
forcefully terminated on timeout, ensuring system stability.

Key benefits:
- True process isolation: code crashes/hangs won't affect main process
- Reliable timeout: worker can be killed anytime
- Memory protection: worker has separate memory space
- Consistent architecture: matches Python execution pattern
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.

Add support for execution timeout in isolated-vm for Node.js 20

0 participants