Skip to content

feat: add worker pool mode for process reuse#257

Open
Hanfee wants to merge 3 commits into
langgenius:mainfrom
Hanfee:feat/worker-pool
Open

feat: add worker pool mode for process reuse#257
Hanfee wants to merge 3 commits into
langgenius:mainfrom
Hanfee:feat/worker-pool

Conversation

@Hanfee

@Hanfee Hanfee commented May 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a worker pool mode for both Python and Node.js runners that keeps a fixed number of processes alive and reuses them across requests, eliminating the per-request fork overhead
  • Pool size is configurable; processes are pre-warmed at startup and returned to the pool after each execution
  • Fix seccomp invocation: DifySeccomp is now called once at process startup (via SANDBOX_UID/SANDBOX_GID env vars) instead of per-request — seccomp filters are one-way and cannot be applied multiple times
  • Fix Node.js stderr goroutine leak after NODEJS_POOL_READY signal
  • Use a persistent bufio.Reader per Node.js pool process to prevent buffered data loss

Changes

File Description
internal/core/runner/nodejs/pool_runner.go NodeJS pool executor: persistent reader, env vars, goroutine fix
internal/core/runner/nodejs/pool_init_script.js Worker script: koffi+seccomp at startup, sync readline loop
internal/core/runner/python/pool_runner.go Python pool executor: SANDBOX_UID/GID env vars
internal/core/runner/python/pool_init_script.py Worker script: _arm_seccomp() called once in main()
internal/pool/ Generic pool abstraction (TaskExecutor interface)

Test plan

  • Unit tests for pool task execution (Python + Node.js)
  • Integration tests: stdout/stderr capture, timeout, preload, enable_network
  • Seccomp called exactly once per process lifecycle
  • Process reuse verified across multiple sequential requests

🤖 Generated with Claude Code

wuhan11 and others added 3 commits April 29, 2026 14:13
Introduces an optional process pool that keeps Python and Node.js worker
processes alive between requests, communicating via stdin/stdout JSON
protocol instead of forking a new process for every execution.

Key changes:
- internal/pool/: new pool package (RuntimePool, TaskExecutor interface,
  PoolConfig, PoolStats, error definitions)
- internal/core/runner/python/pool_runner.go + pool_init_script.py:
  persistent Python worker process with XOR-encrypted stdin/stdout protocol
- internal/core/runner/nodejs/pool_runner.go + pool_init_script.js:
  persistent Node.js worker using isolated-vm for per-request V8 isolation
- internal/service/pool.go: global pool singleton init/shutdown
- internal/service/{python,nodejs,check}.go: route requests to pool or
  original fork mode based on config
- internal/server/server.go: call service.InitPool() at startup
- internal/types/config.go + internal/static/config.go: WorkerPoolConfig
  struct with env-var overrides (WORKER_POOL_ENABLED, etc.)
- conf/config.yaml: worker_pool section (disabled by default)

Pool mode is opt-in: set worker_pool.enabled: true in config.yaml or
WORKER_POOL_ENABLED=true env var.  The original fork+seccomp mode remains
the default and is unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- internal/pool/pool_test.go: 13 pure-Go unit tests covering
  config validation, pool lifecycle, submit/shutdown, stats tracking,
  and concurrent access. All run on macOS without Linux .so files.
  Coverage: 85% of pool package statements.

- internal/core/runner/python/pool_runner_test.go: integration tests
  for PythonPoolExecutor (build tag: integration). Covers basic
  execution, stderr capture, syntax errors, preload, process reuse,
  and shutdown. Skips automatically when python.so is absent.

- internal/core/runner/nodejs/pool_runner_test.go: integration tests
  for NodeJSPoolExecutor (build tag: integration). Mirrors the Python
  suite. Skips when nodejs.so is absent.

- tests/integration_tests/pool_integration_test.go: end-to-end pool
  mode tests (build tag: integration). Python and Node.js suites cover
  basic execution, arithmetic, preload, error propagation, concurrency,
  process reuse, pool-vs-fork parity, and timeout behaviour.

Run unit tests locally (no Linux env needed):
  GOOS=darwin GOARCH=arm64 go test ./internal/pool/... -v

Run integration tests (requires Linux sandbox env + worker_pool.enabled=true):
  go test -tags integration ./tests/integration_tests/... -v -timeout 120s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Call DifySeccomp once at process startup instead of per-request;
  seccomp filters are one-way and cannot be applied multiple times
- Pass SANDBOX_UID/SANDBOX_GID via env vars to pool worker processes
  so uid/gid are no longer hardcoded or carried in each request payload
- Fix Node.js stderr goroutine leak: keep draining stderr after
  NODEJS_POOL_READY signal so the pipe buffer never blocks
- Use a persistent bufio.Reader per process (nodejs pool) to avoid
  buffered data loss when re-creating reader on each request
- Python pool_init_script: extract _arm_seccomp() called once in
  main() before accepting requests; read uid/gid from env vars

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant