Skip to content

fix(maticjs): disable HTTP keep-alive on metadata fetches to fix Premature close#484

Open
MaximusHaximus wants to merge 1 commit into
masterfrom
fix/maticjs-disable-keepalive
Open

fix(maticjs): disable HTTP keep-alive on metadata fetches to fix Premature close#484
MaximusHaximus wants to merge 1 commit into
masterfrom
fix/maticjs-disable-keepalive

Conversation

@MaximusHaximus

Copy link
Copy Markdown
Contributor

Problem

@maticnetwork/maticjs loads network config + contract ABIs at runtime from static.polygon.technology via node-fetch. Since Node 19 made HTTP keep-alive the default, node-fetch reuses a socket the CDN has idle-closed; the next gzip response dies mid-decompression as FetchError: Premature close, which surfaces as the misleading network <net> - <ver> is not supported. It reproduces 100% from datacenter/CI egress and intermittently elsewhere, and the retry added in #481 can't recover because every reused socket is already stale.

Fix

Pass node-fetch a keepAlive: false agent for these fetches (Node build only — window.fetch ignores agent). They run once per client init and are cached, so a fresh connection per request only costs a TLS handshake at startup. webpack resolve.fallback maps http/https to empty stubs so the web/UMD/standalone bundles still build (the browser branch never executes them).

Evidence

Isolated in CI on the same runner that fails today (5 attempts each, .../mainnet/v1/index.json): node-fetch default keep-alive 0/5; keepAlive:false 5/5. curl + Node's built-in fetch (undici) reach origin 30/30, confirming it's the node-fetch keep-alive path, not a network/CDN block.

Validation

pnpm --filter @maticnetwork/maticjs typecheck + build (all three webpack targets) + test (36 passing) green locally.

…ature close

node-fetch reuses keep-alive sockets (default since Node 19) that the upstream
CDN has idle-closed; the next gzip response dies mid-decompression as
`FetchError: Premature close`, mislabeled "network <net> - <ver> is not
supported". Pass node-fetch a keepAlive:false agent for the network-config and
ABI fetches (Node build only; window.fetch ignores it). webpack resolve.fallback
maps http/https to empty stubs so the web/UMD bundles still build.
@MaximusHaximus MaximusHaximus marked this pull request as ready for review June 29, 2026 18:20
@claude

claude Bot commented Jun 29, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

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