Skip to content

bug: a subscription that yields undefined ends with "undefined is not valid JSON" over httpSubscriptionLink (SSE) #8143

Closed

Description

@Santhosh-x23

Provide environment information

System: Windows 11
Node: 24.14.0
npmPackages:
  @trpc/server: 11.29.5
  @trpc/client: 11.29.5
  eventsource: 3.0.7 (passed to httpSubscriptionLink)

Describe the bug

A subscription that yields undefined ends with an error when it is used over httpSubscriptionLink (SSE) without a transformer.

The server writes the event as the literal text data: undefined, because JSON.stringify(undefined) returns undefined. The client then calls JSON.parse('undefined'), which throws. The client closes the connection and the subscription ends with onError "undefined" is not valid JSON.

Raw event stream from the server for yield 1; yield undefined; yield 3:

event: connected
data: {}

data: 1

data: undefined

data: 3

event: return
data:

Why I think this is a bug and not a limit:

  • The procedure types allow it: subscription<$Output extends AsyncIterable<any, void, any>>.
  • With a transformer such as superjson the same subscription works. The client gets onData 1, onData undefined, onData 3, onComplete.
  • The JSONL encoder in jsonl.ts handles undefined on purpose (if (value === undefined) return [[]]).
  • A payload-less "something changed" signal (yield;) is a natural thing to write.

Expected: onData(undefined), as with a transformer. If undefined is not meant to be supported, a clear error on the server would be better than a broken stream.

Link to reproduction

https://github.com/Santhosh-x23/trpc-sse-undefined-repro

To reproduce

git clone https://github.com/Santhosh-x23/trpc-sse-undefined-repro
cd trpc-sse-undefined-repro
npm install
npm start          # fails: onError "undefined" is not valid JSON
npm run start:superjson   # works

The core of it:

const appRouter = t.router({
  signal: t.procedure.subscription(async function* () {
    yield 1;
    yield undefined;
    yield 3;
  }),
});

Additional information

Where it happens (packages/server/src/unstable-core-do-not-import/stream/sse.ts):

  • Producer: chunk.data = JSON.stringify(serialize(chunk.data)). With the default identity serialize, undefined becomes undefined and is written as data: undefined.
  • Consumer: deserialize(JSON.parse(msg.data)) in the 'message' listener. The thrown error is caught, eventSource.close() runs and the stream errors.

Possible fixes, for you to choose between:

  1. Write an empty data: for undefined and have the consumer map an empty data to undefined. This matches the superjson result, but an old client talking to a new server still fails.
  2. Write data: null. Old clients work, but the client sees null instead of undefined.

I'm happy to send a PR with a regression test once you say which behavior you want.

AI disclosure: I used Claude (Anthropic) to help investigate this and draft the report. I ran the reproduction myself and the output above is real.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. added a commit that references this issue on Nov 23, 2026
    8edb20a
  2. github-actions commented on Nov 24, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  3. locked as resolved and limited conversation to collaborators on Nov 24, 2026
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