From 2916e3efd66168a471a491e226cc504d5a0426e6 Mon Sep 17 00:00:00 2001 From: AdaAibaby Date: Tue, 30 Jun 2026 11:36:12 +0800 Subject: [PATCH 1/2] feat(js-sdk): expose E2B_ENVD_CONNECTIONS env var for file REST API connection limit\n\nThe envd REST file API (files.read/files.write) had its undici connection\npool hardcoded to 10 connections with no way to override it, while the\nRPC transport (commands) allowed 200 connections configurable via\nE2B_ENVD_RPC_CONNECTIONS. Under high concurrency this 20x gap caused\nsocket write contention and crashes on the file API path.\n\n- Add getEnvdConnectionLimit() reading E2B_ENVD_CONNECTIONS (default 10)\n- Pass connectionLimit into createEnvdFetch() instead of relying on the\n hardcoded fallback\n- Add tests for the new env var (valid + malformed values)" --- packages/js-sdk/src/envd/http2.ts | 10 ++++++++-- packages/js-sdk/tests/envd/http2.test.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.ts index 1de93db57c..271fa7a2a3 100644 --- a/packages/js-sdk/src/envd/http2.ts +++ b/packages/js-sdk/src/envd/http2.ts @@ -90,9 +90,8 @@ export function createEnvdFetch(proxy?: string): typeof fetch { return cached } - // Keep one origin connection for short envd REST calls. If ALPN falls back - // to h1, this favors connection pressure over per-sandbox throughput. const envdFetch = createEnvdFetchForRuntime(runtime, { + connectionLimit: getEnvdConnectionLimit(), inflightLimit: getEnvdInflightLimit(), proxy, }) @@ -119,6 +118,13 @@ export function createEnvdRpcFetch(proxy?: string): typeof fetch { return envdRpcFetch } +export function getEnvdConnectionLimit(): number { + return parsePositiveIntEnv( + 'E2B_ENVD_CONNECTIONS', + DEFAULT_ENVD_CONNECTION_LIMIT + ) +} + export function getEnvdRpcConnectionLimit(): number { return parsePositiveIntEnv( 'E2B_ENVD_RPC_CONNECTIONS', diff --git a/packages/js-sdk/tests/envd/http2.test.ts b/packages/js-sdk/tests/envd/http2.test.ts index 01ca903553..39251961f5 100644 --- a/packages/js-sdk/tests/envd/http2.test.ts +++ b/packages/js-sdk/tests/envd/http2.test.ts @@ -5,6 +5,7 @@ afterEach(() => { vi.resetModules() vi.doUnmock('undici') vi.doUnmock('../../src/utils') + delete process.env.E2B_ENVD_CONNECTIONS delete process.env.E2B_ENVD_RPC_CONNECTIONS delete process.env.E2B_ENVD_INFLIGHT_REQUESTS delete process.env.E2B_ENVD_RPC_INFLIGHT_REQUESTS @@ -158,6 +159,22 @@ test('can create a bounded dispatcher for RPC streams', async () => { expect(agents).toEqual([{ allowH2: true, connections: 100 }]) }) +test('reads envd connection limit from env', async () => { + process.env.E2B_ENVD_CONNECTIONS = '50' + + const { getEnvdConnectionLimit } = await import('../../src/envd/http2') + + expect(getEnvdConnectionLimit()).toBe(50) +}) + +test('getEnvdConnectionLimit throws on malformed env value', async () => { + process.env.E2B_ENVD_CONNECTIONS = 'bogus' + + const { getEnvdConnectionLimit } = await import('../../src/envd/http2') + + expect(() => getEnvdConnectionLimit()).toThrow(/E2B_ENVD_CONNECTIONS/) +}) + test('reads RPC stream dispatcher connection limit from env', async () => { process.env.E2B_ENVD_RPC_CONNECTIONS = '200' From 477d78b66e407248bed9b2a1ab83ca8373504bdb Mon Sep 17 00:00:00 2001 From: AdaAibaby Date: Tue, 30 Jun 2026 14:04:12 +0800 Subject: [PATCH 2/2] chore: add changeset for E2B_ENVD_CONNECTIONS env var --- .changeset/envd-connection-limit-env-var.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/envd-connection-limit-env-var.md diff --git a/.changeset/envd-connection-limit-env-var.md b/.changeset/envd-connection-limit-env-var.md new file mode 100644 index 0000000000..87bf348dbb --- /dev/null +++ b/.changeset/envd-connection-limit-env-var.md @@ -0,0 +1,5 @@ +--- +"e2b": patch +--- + +Expose `E2B_ENVD_CONNECTIONS` environment variable to configure the envd REST file API connection pool limit (default remains 10). Previously the connection count was hardcoded with no runtime override, unlike the RPC transport which already supported `E2B_ENVD_RPC_CONNECTIONS`.