Skip to content

@Sse() stream hangs when event data can't be serialized (BigInt, circular object, number) #18024

Closed

Description

@OmBhandwaldar

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

If an @Sse() handler emits an event whose data can't be serialized, the stream just stops. The client gets the events sent before it and then nothing: no event: error, none of the following events, and the response is never ended. Nothing is logged on the server.

It happens with:

  • a BigInt (e.g. from a Prisma BigInt column) or a circular object in data, where JSON.stringify throws
  • a number or a boolean as data, e.g. { data: 1 }, or of(1, 2) returned directly, which Nest wraps as { data: 1 }. serializeSseLines() calls .split() on it.

If it's the first event, the client gets a 200 with the SSE headers and an empty stream that never ends, instead of a 500.

SseStream._transform commits the headers and then serializes the data (sse-stream.ts#L6-L19, #L153). The throw escapes stream.write(), so the transform callback is never called and the stream stays stuck mid-write. The event: error written by catchError in RouterResponseController.sse() (router-response-controller.ts#L211) and the final end() are queued behind it and never sent. Since the headers are already committed by then, a bad first event takes the same path instead of rejecting.

Minimum reproduction code

https://github.com/OmBhandwaldar/nest-sse-unserializable-event

Steps to reproduce

  1. npm install && npm start
  2. curl -N --max-time 3 http://localhost:3000/sse prints event 1, then waits until curl times out
  3. curl -i --max-time 3 http://localhost:3000/sse-first returns 200 with the SSE headers, no events, and never ends
  4. curl -N --max-time 3 http://localhost:3000/sse-number sends no events and never ends

Expected behavior

Numbers and booleans sent like other values (data: 1). For data that really can't be serialized, the same handling as other errors in the stream: an event: error with the message (Do not know how to serialize a BigInt) and the end of the response, or a 500 if no event was sent yet.

NestJS version

12.1.2

Packages versions

"@nestjs/common": "12.1.2",
"@nestjs/core": "12.1.2",
"@nestjs/platform-express": "12.1.2",
"rxjs": "^7.8.2"

Node.js version

24.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Same with FastifyAdapter, and also reproduced on 11.2.7. I have a fix with tests and can open a PR.

Activity

  1. kamilmysliwiec commented on Oct 8, 2026

    @kamilmysliwiec
    Member

    lets track this here #18025

  2. added a commit that references this issue on Oct 8, 2026
    e2b2851
  3. added a commit that references this issue on Oct 8, 2026
    f4b5ff4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions