[miniflare] A Worker answering a WebSocket upgrade with 204 or 304 crashes the process

Open Beginner friendly
#17,215 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
api, backend

Research direction

Start in packages/miniflare/src/http/fetch.ts at the unexpected-response handler, which the issue identifies as passing the response stream to the Response constructor. Check how it handles 204, 205, and 304, then run the relevant Miniflare fetch tests. Done means dispatchFetch resolves with the Worker's 204 or 304 response instead of crashing the process.

Written by the indexing model from the issue text.

Description

What versions & operating system are you using?

miniflare 5.20261123.1-beta (built from main at 41bacf7ce), undici 7.30.2, ws 8.21.0, Node.js 26.13.1, Linux x64

Please provide a link to a minimal reproduction

No repository; this script reproduces it:

import { Miniflare } from 'miniflare';

const mf = new Miniflare({
  workers: [{
    config: {
      name: '', compatibilityDate: '2026-11-01',
      manifest: { mainModule: 'index.mjs', modules: { 'index.mjs': { type: 'esm', contents: 'export default { fetch() { return new Response(null, { status: 204 }); } }' } } },
      env: {}, exports: {},
    },
  }],
});

try {
  const res = await mf.dispatchFetch('http://localhost/', { headers: { Upgrade: 'websocket' } });
  console.log('resolved', res.status);
} catch (error) {
  console.log('caught', error.message);
}
Describe the Bug

When a Worker answers a WebSocket upgrade with a 204 or 304, dispatchFetch crashes the Node.js process instead of returning the response, and the catch above never runs.

The unexpected-response handler in packages/miniflare/src/http/fetch.ts passes the response stream as the body, new Response(req, { status: req.statusCode, headers }), and undici rejects a body for 204, 205, and 304.

It throws inside the ws event, so callers of dispatchFetch, such as @cloudflare/vite-plugin in vite dev, can't catch it.

Expected: dispatchFetch resolves to the Worker's 204.

Passing null as the body for those statuses would fix it.

Please provide any relevant error logs
TypeError: Response constructor: Invalid response status code 204
    at initializeResponse (undici/lib/web/fetch/response.js:528:27)
    at new Response (undici/lib/web/fetch/response.js:145:5)
    at new _Response (miniflare/dist/src/index.js:35880:9)
    at WebSocket.<anonymous> (miniflare/dist/src/index.js:36655:25)
    at ClientRequest.<anonymous> (ws/lib/websocket.js:929:27)
Dominant language
TypeScript
Stars
4.7k
Forks
1.7k
Avg merge
1d 22h
Merged PRs (30d)
282

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.