Skip to content

fix: avoid node-fetch@2 gzip "Premature close" crash loop on Node 24 - #53

Open
chrisgrocki wants to merge 1 commit into
tabrindle:masterfrom
chrisgrocki:fix/node24-premature-close
Open

fix: avoid node-fetch@2 gzip "Premature close" crash loop on Node 24#53
chrisgrocki wants to merge 1 commit into
tabrindle:masterfrom
chrisgrocki:fix/node24-premature-close

Conversation

@chrisgrocki

Copy link
Copy Markdown

Fixes #52.

Problem

On Node.js 24, every api.js request fails with FetchError: ... Premature close, thrown from node-fetch@2's gzip (Gunzip) stream. The error is asynchronous and uncaught, so it becomes an uncaughtException; Homebridge's CLI self-SIGTERMs on any uncaughtException, so under hb-service the entire bridge crash-loops every ~10s — not just the Leviton platform.

It's client-side, not a Leviton outage. The same request on Node 24 fails 4/4 with gzip on, and succeeds 4/4 (200 OK) with compress: false. See #52 for the full root-cause analysis and reproduction.

Change

Wrap node-fetch once to default compress: false, so requests skip the failing gzip-decode path. Minimal and low-risk: a single, well-commented line covering all call sites; callers can still override. Responses come back uncompressed (these are small JSON payloads).

This is the verified minimal fix. The longer-term direction (noted in #52) is migrating off the deprecated node-fetch@2 to native fetch (Node 18+), which I'm happy to follow up on separately.

Testing

On Node v24.17.0, against the live API with a valid token: before — 4/4 Premature close; after — 4/4 200 OK. In a running Homebridge install, the bridge stops crash-looping and the Leviton accessories load.

On Node 18+ (notably Node 24), node-fetch@2's gzip decode throws
"Premature close" on Leviton's responses. The error is async and uncaught,
becomes an uncaughtException, and Homebridge self-SIGTERMs on it — crash-looping
the whole bridge under hb-service. Defaulting compress:false skips the failing
gzip path; verified 4/4 200 OK on Node 24 where gzip failed 4/4.

Fixes tabrindle#52.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJbj8QvW5GdZbcDAXgqMHV
@jeff-winn

Copy link
Copy Markdown

I can confirm this will fix it. I modified the files locally using vi with the changes proposed and it addressed the issue here as well.

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.

Node 24: node-fetch@2 gzip decode throws "Premature close", crash-looping the entire bridge

2 participants