feat: add worker pool mode for process reuse#257
Open
Hanfee wants to merge 3 commits into
Open
Conversation
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>
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
DifySeccompis now called once at process startup (viaSANDBOX_UID/SANDBOX_GIDenv vars) instead of per-request — seccomp filters are one-way and cannot be applied multiple timesNODEJS_POOL_READYsignalbufio.Readerper Node.js pool process to prevent buffered data lossChanges
internal/core/runner/nodejs/pool_runner.gointernal/core/runner/nodejs/pool_init_script.jsinternal/core/runner/python/pool_runner.gointernal/core/runner/python/pool_init_script.py_arm_seccomp()called once inmain()internal/pool/Test plan
🤖 Generated with Claude Code