Skip to content

Commit 7869b88

Browse files
Merge 8256418 into 1ad3f76
2 parents 1ad3f76 + 8256418 commit 7869b88

9 files changed

Lines changed: 267 additions & 26 deletions

File tree

‎.changeset/envd-uploads-http1.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'e2b': patch
3+
---
4+
5+
Fix `TypeError: fetch failed` / `NGHTTP2_ENHANCE_YOUR_CALM` on concurrent `files.write()` calls. Node's HTTP/2 client queues each request body in full against a 10 MB per-session memory budget and resets the stream whose response arrives while it is exhausted; since undici 8 every envd request of a process shares one session, so parallel or large uploads reset each other and the reads next to them. `files.write()` uploads now go over HTTP/1.1 on a pool of their own (one request per connection, no shared budget; `E2B_ENVD_UPLOAD_CONNECTIONS`, default 50), and an envd dispatcher whose HTTP/2 session Node reports as out of memory is retired and replaced instead of failing every later request. Nothing is retried: a failed write may already have been applied.

‎packages/js-sdk/src/connectionConfig.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ export interface ConnectionOpts {
138138
* filesystem, PTY). `'2'` multiplexes streams over shared connections;
139139
* `'1.1'` pins them to HTTP/1.1 with one connection per concurrent
140140
* request — for example when an intermediary on the path retires or
141-
* mishandles long-lived HTTP/2 connections. Only applies in Node.
141+
* mishandles long-lived HTTP/2 connections. File uploads (`files.write()`)
142+
* always use HTTP/1.1, whatever this option says, so that their bodies
143+
* cannot exhaust Node's per-session HTTP/2 memory and reset other requests.
144+
* Only applies in Node.
142145
*
143146
* @default E2B_HTTP_VERSION // environment variable or `'2'`
144147
*/

‎packages/js-sdk/src/envd/http2.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ type EnvdFetchOptions = {
1919
// keep sharing a single dispatcher while each distinct proxy URL gets its own.
2020
const envdFetchers = new Map<string, typeof fetch>()
2121
const envdRpcFetchers = new Map<string, typeof fetch>()
22+
const envdUploadFetchers = new Map<string, typeof fetch>()
2223
const DEFAULT_ENVD_CONNECTION_LIMIT = 10
2324
const DEFAULT_ENVD_RPC_CONNECTION_LIMIT = 200
25+
const DEFAULT_ENVD_UPLOAD_CONNECTION_LIMIT = 50
2426

2527
export function createEnvdFetchForRuntime(
2628
currentRuntime = runtime,
@@ -92,3 +94,40 @@ export function getEnvdRpcConnectionLimit(): number {
9294
DEFAULT_ENVD_RPC_CONNECTION_LIMIT
9395
)
9496
}
97+
98+
/**
99+
* Fetch for `/files` uploads, always HTTP/1.1. Node's HTTP/2 client queues
100+
* each buffered request body in full against the session's memory budget
101+
* (`maxSessionMemory`, 10 MB) and resets whichever stream's response headers
102+
* arrive while it is exhausted (ENHANCE_YOUR_CALM), so uploads multiplexed
103+
* with everything else on the shared envd session fail each other and the
104+
* reads next to them. HTTP/1.1 gives each upload a connection of its own; the
105+
* pool is sized for many concurrent uploads per process since the request
106+
* timeout keeps running while a request waits for a free connection.
107+
*/
108+
export function createEnvdUploadFetch({
109+
proxy,
110+
}: Pick<FetchOpts, 'proxy'> = {}): typeof fetch {
111+
const key = fetcherKey(proxy, '1.1')
112+
113+
const cached = envdUploadFetchers.get(key)
114+
if (cached) {
115+
return cached
116+
}
117+
118+
const envdUploadFetch = createEnvdFetchForRuntime(runtime, {
119+
connectionLimit: getEnvdUploadConnectionLimit(),
120+
proxy,
121+
httpVersion: '1.1',
122+
})
123+
envdUploadFetchers.set(key, envdUploadFetch)
124+
125+
return envdUploadFetch
126+
}
127+
128+
export function getEnvdUploadConnectionLimit(): number {
129+
return parsePositiveIntEnv(
130+
'E2B_ENVD_UPLOAD_CONNECTIONS',
131+
DEFAULT_ENVD_UPLOAD_CONNECTION_LIMIT
132+
)
133+
}

‎packages/js-sdk/src/sandbox/filesystem/index.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Username,
1616
wrapStreamWithConnectionCleanup,
1717
} from '../../connectionConfig'
18+
import { createEnvdUploadFetch } from '../../envd/http2'
1819

1920
import {
2021
checkSandboxHealth,
@@ -387,11 +388,16 @@ export class Filesystem {
387388
private readonly defaultWatchRecursive = false
388389
private readonly checkHealth: SandboxHealthCheck
389390

391+
/** `/files` uploads go over HTTP/1.1, see {@link createEnvdUploadFetch}. */
392+
private readonly uploadFetch: (request: Request) => Promise<Response>
393+
390394
constructor(
391395
transport: Transport,
392396
private readonly envdApi: EnvdApiClient,
393397
private readonly connectionConfig: ConnectionConfig
394398
) {
399+
const uploadFetch = createEnvdUploadFetch({ proxy: connectionConfig.proxy })
400+
this.uploadFetch = (request) => uploadFetch(request)
395401
this.rpc = createClient(FilesystemService, transport)
396402
this.checkHealth = () => checkSandboxHealth(this.envdApi)
397403
}
@@ -734,6 +740,7 @@ export class Filesystem {
734740
headers,
735741
signal,
736742
body: {},
743+
fetch: this.uploadFetch,
737744
// Streaming request bodies require half-duplex mode.
738745
...(streamed && {
739746
duplex: 'half' as const,
@@ -797,6 +804,7 @@ export class Filesystem {
797804
writeOpts?.signal
798805
),
799806
body: {},
807+
fetch: this.uploadFetch,
800808
})
801809
.catch(async (err) => {
802810
throw await handleEnvdApiFetchError(err, this.checkHealth)

‎packages/js-sdk/src/undici.ts‎

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,46 @@ type UndiciRequestInit = RequestInit & {
99
duplex?: 'half'
1010
}
1111

12+
type UndiciDispatcher = { close(): Promise<void> }
13+
14+
type UndiciAgentOptions = {
15+
allowH2: boolean
16+
connections?: number
17+
}
18+
1219
export type UndiciModule = {
13-
Agent: new (options: { allowH2: boolean; connections?: number }) => unknown
14-
ProxyAgent: new (options: {
15-
uri: string
16-
allowH2: boolean
17-
connections?: number
18-
proxyTunnel: true
19-
}) => unknown
20+
Agent: new (options: UndiciAgentOptions) => UndiciDispatcher
21+
ProxyAgent: new (
22+
options: UndiciAgentOptions & { uri: string; proxyTunnel: true }
23+
) => UndiciDispatcher
2024
fetch: unknown
2125
}
2226

27+
// Node's HTTP/2 client charges queued request-body bytes against a per-session
28+
// memory budget (`maxSessionMemory`, 10 MB) and resets the stream whose
29+
// response headers arrive while the budget is exhausted — with
30+
// ENHANCE_YOUR_CALM, or INTERNAL_ERROR where the reset is deferred. Bytes of
31+
// streams destroyed before they were flushed stay charged until the session
32+
// closes (nodejs/node#67113), so such a reset marks the session as poisoned.
33+
const POISONED_HTTP2_SESSION_RST =
34+
/NGHTTP2_(ENHANCE_YOUR_CALM|INTERNAL_ERROR)\b/
35+
36+
/**
37+
* Whether a `fetch` rejection is Node's HTTP/2 client resetting the stream
38+
* itself because the session's memory budget is exhausted. The session stays
39+
* unusable for every later request, so the dispatcher holding it must go.
40+
*/
41+
export function isPoisonedHTTP2SessionError(err: unknown): boolean {
42+
if (!(err instanceof Error) || !(err.cause instanceof Error)) {
43+
return false
44+
}
45+
const cause = err.cause as Error & { code?: unknown }
46+
return (
47+
cause.code === 'ERR_HTTP2_STREAM_ERROR' &&
48+
POISONED_HTTP2_SESSION_RST.test(cause.message)
49+
)
50+
}
51+
2352
const UNDICI_MIN_NODE = '22.19.0'
2453

2554
export async function loadUndici(
@@ -84,7 +113,9 @@ export function createRuntimeFetch(
84113
/**
85114
* Build a fetch bound to a bounded undici dispatcher (HTTP/2 enabled unless
86115
* `httpVersion` is `'1.1'`, `connections` origin connections, optional proxy
87-
* tunnel). Falls back to the global fetch when undici cannot be loaded.
116+
* tunnel). A dispatcher whose HTTP/2 session Node reports as out of memory is
117+
* retired and replaced, so one poisoned session does not fail every later
118+
* request. Falls back to the global fetch when undici cannot be loaded.
88119
*/
89120
export async function buildDispatchedFetch(options: {
90121
connections: number
@@ -99,30 +130,44 @@ export async function buildDispatchedFetch(options: {
99130
}
100131

101132
const { Agent, ProxyAgent, fetch: undiciFetch } = undici
102-
const allowH2 = (options.httpVersion ?? DEFAULT_HTTP_VERSION) === '2'
103-
const dispatcher = options.proxy
104-
? new ProxyAgent({
105-
uri: options.proxy,
106-
allowH2,
107-
connections: options.connections,
108-
proxyTunnel: true,
109-
})
110-
: new Agent({
111-
allowH2,
112-
connections: options.connections,
113-
})
133+
const agentOptions: UndiciAgentOptions = {
134+
allowH2: (options.httpVersion ?? DEFAULT_HTTP_VERSION) === '2',
135+
connections: options.connections,
136+
}
137+
const createDispatcher = () =>
138+
options.proxy
139+
? new ProxyAgent({
140+
uri: options.proxy,
141+
...agentOptions,
142+
proxyTunnel: true,
143+
})
144+
: new Agent(agentOptions)
114145
const fetchWithDispatcher = undiciFetch as unknown as (
115146
input: RequestInfo | URL,
116147
init?: UndiciRequestInit
117148
) => Promise<Response>
118149

119-
return ((input, init) => {
150+
let dispatcher = createDispatcher()
151+
152+
return (async (input, init) => {
120153
const request = toUndiciRequestInput(input, init)
154+
const used = dispatcher
121155

122-
return fetchWithDispatcher(request.input, {
123-
...request.init,
124-
dispatcher,
125-
})
156+
try {
157+
return await fetchWithDispatcher(request.input, {
158+
...request.init,
159+
dispatcher: used,
160+
})
161+
} catch (err) {
162+
// Only the first failure of a dispatcher replaces it; its other in-flight
163+
// requests fail the same way and must not retire the replacement.
164+
if (used === dispatcher && isPoisonedHTTP2SessionError(err)) {
165+
dispatcher = createDispatcher()
166+
// Graceful: requests still running on the old session finish first.
167+
void used.close().catch(() => {})
168+
}
169+
throw err
170+
}
126171
}) as typeof fetch
127172
}
128173

‎packages/js-sdk/tests/envd/http2.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ afterEach(() => {
44
vi.restoreAllMocks()
55
vi.resetModules()
66
delete process.env.E2B_ENVD_RPC_CONNECTIONS
7+
delete process.env.E2B_ENVD_UPLOAD_CONNECTIONS
78
})
89

910
test('uses undici with HTTP/2 enabled in Node', async () => {
@@ -297,3 +298,16 @@ test('uses global fetch outside Node', async () => {
297298
undefined
298299
)
299300
})
301+
302+
test('getEnvdUploadConnectionLimit defaults to 50 and honours E2B_ENVD_UPLOAD_CONNECTIONS', async () => {
303+
const { getEnvdUploadConnectionLimit } = await import('../../src/envd/http2')
304+
expect(getEnvdUploadConnectionLimit()).toBe(50)
305+
306+
process.env.E2B_ENVD_UPLOAD_CONNECTIONS = '8'
307+
expect(getEnvdUploadConnectionLimit()).toBe(8)
308+
309+
process.env.E2B_ENVD_UPLOAD_CONNECTIONS = 'bogus'
310+
expect(() => getEnvdUploadConnectionLimit()).toThrow(
311+
/E2B_ENVD_UPLOAD_CONNECTIONS/
312+
)
313+
})

‎packages/js-sdk/tests/sandbox/httpVersion.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { afterEach, expect, test, vi } from 'vitest'
33
const mocks = vi.hoisted(() => ({
44
createEnvdFetch: vi.fn(() => vi.fn()),
55
createEnvdRpcFetch: vi.fn(() => vi.fn()),
6+
createEnvdUploadFetch: vi.fn(() => vi.fn()),
67
}))
78

89
vi.mock('@connectrpc/connect-web', () => ({
@@ -12,6 +13,7 @@ vi.mock('@connectrpc/connect-web', () => ({
1213
vi.mock('../../src/envd/http2', () => ({
1314
createEnvdFetch: mocks.createEnvdFetch,
1415
createEnvdRpcFetch: mocks.createEnvdRpcFetch,
16+
createEnvdUploadFetch: mocks.createEnvdUploadFetch,
1517
}))
1618

1719
afterEach(() => {
@@ -73,3 +75,14 @@ test('E2B_HTTP_VERSION=1.1 pins envd fetchers to HTTP/1.1', async () => {
7375
httpVersion: '1.1',
7476
})
7577
})
78+
79+
test('files.write uploads get the HTTP/1.1 upload fetcher for the configured proxy', async () => {
80+
await createSandbox({ httpVersion: '2', proxy: 'http://127.0.0.1:8080' })
81+
82+
expect(mocks.createEnvdUploadFetch).toHaveBeenCalledWith({
83+
proxy: 'http://127.0.0.1:8080',
84+
})
85+
expect(mocks.createEnvdFetch.mock.calls[0][0]).toMatchObject({
86+
httpVersion: '2',
87+
})
88+
})

‎packages/js-sdk/tests/sandbox/rpcHeaders.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ vi.mock('@connectrpc/connect-web', () => ({
1515
vi.mock('../../src/envd/http2', () => ({
1616
createEnvdFetch: vi.fn(() => vi.fn()),
1717
createEnvdRpcFetch: vi.fn(() => mocks.rpcFetch),
18+
createEnvdUploadFetch: vi.fn(() => vi.fn()),
1819
}))
1920

2021
test('does not pass custom connection headers to envd RPC requests', async () => {

0 commit comments

Comments
 (0)