Skip to content

bug: maxDurationMs does not interrupt the subscription when not yielding #6991

Closed

Description

@testerez

Provide environment information

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @tanstack/react-query: ^5.80.3 => 5.90.5 
    @trpc/client: canary => 11.7.1-canary.1+3ae1c3651 
    @trpc/next: canary => 11.7.1-canary.1+3ae1c3651 
    @trpc/react-query: canary => 11.7.1-canary.1+3ae1c3651 
    @trpc/server: canary => 11.7.1-canary.1+3ae1c3651 
    next: ^15.3.1 => 15.5.6 
    react: ^19.1.0 => 19.2.0 
    typescript: ^5.9.2 => 5.9.3 

Describe the bug

When testing subscriptions on Vercel, I was surprised to experience timeouts of my lambdas, even though I've set sse.maxDurationMs to a duration shorter than my lambda's max duration.

After running more tests, I figured the subscription was correctly terminated but only when it yields a value:
https://stackblitz.com/edit/github-rmw4rvvn-lqfxkrsa?file=src%2Fpages%2Fapi%2Ftrpc%2F%5Btrpc%5D.ts

If I do not yield anything from the subscription handler, then it's never terminated and the signal is never aborted:
https://stackblitz.com/edit/github-rmw4rvvn-ertp6cer?file=src%2Fpages%2Fapi%2Ftrpc%2F%5Btrpc%5D.ts

Btw, I also noticed that when the subscription is terminated by maxDurationMs, it takes some time for the client to notice and reconnect. It would be great if there was a built-in way to proactively reconnect from the client in order to minimise the latency during reconnection. Maybe that could be a default behaviour when maxDurationMs is set? Please let me know if I'm missing something here.

Link to reproduction

https://stackblitz.com/edit/github-rmw4rvvn-ertp6cer?file=src%2Fpages%2Fapi%2Ftrpc%2F%5Btrpc%5D.ts

To reproduce

Just look at the console log. Notice that we never log "🚫 subscription ended" but continue loggign 🔄 loop ${count} even though the 2s maxDurationMs is passed.

Additional information

No response

👨‍👧‍👦 Contributing

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

Activity

  1. added
    ✅ accepted-PRs-welcome
    on Oct 28, 2025
  2. Nick-Lucas commented on Oct 29, 2025

    @Nick-Lucas
    Contributor

    Hey, thanks for sharing the stackblitz, that's always very helpful!

    So my first instinct is you'll need to check the abort signal in appropriate places, similar to this:

          opts.signal?.addEventListener('abort', () => {
            console.log('🚫 signal aborted');
          });
          const isAbortedPromise = new Promise((resolve, reject) => {
            opts.signal?.addEventListener('abort', reject);
          });
          try {
            while (!opts.signal?.aborted) {
              ++count;
              console.log(`[${id}] 🔄 loop ${count}`);
    
              // We do no yield anything, subscription will never stop (bug)
              // yield tracked(
              //   `${count}`,
              //   `[${id}] 📬 new data (count: ${count}, sub id: ${id})`
              // );
              await Promise.race([
                new Promise((r) => setTimeout(r, 1000)),
                isAbortedPromise,
              ]);
            }
            console.log(`[${id}] ✅ done`);
          } catch (error) {
            console.error(`[${id}] ❌ error`, error);
          } finally {
            console.log('🚫 subscription ended');
          }

    Without checking the abort signal yourself there's no way for the JS that's written to escape without yielding control to the consumer of the generator.

    That said my code there isn't working because the abort signal doesn't seem to be turning true when I refresh the preview area. I've seen this before where the web framework isn't appropriately propagating the signal and that can either be a bug in dev mode (seen that on cloudflare) or a configuration issue, but I'm not a nextjs expert so it's hard to say what to look at

    Image
  3. Nick-Lucas commented on Oct 29, 2025

    @Nick-Lucas
    Contributor

    Ah sorry, belay the first bit I can see it's being checked in the while condition, fuzzy morning brain!

    The issue seems to be that abort isn't even being propagated though, so the latter note is something we'd need to check :)

  4. testerez commented on Oct 29, 2025

    @testerez
    Author

    Thanks for having a look @Nick-Lucas !

    If you look at my working example, you can see that the abort signal is sent after the iterator returned. That's probably the issue...

    Image
  5. Nick-Lucas commented on Oct 29, 2025

    @Nick-Lucas
    Contributor

    I think that might be a product of how we convert the nodejs IncomingMessage type (which nextjs uses) to a Request and AbortController - it fires the abort on connection close as well as on abort

    export function incomingMessageToRequest(
    req: NodeHTTPRequest,
    res: NodeHTTPResponse,
    opts: {
    /**
    * Max body size in bytes. If the body is larger than this, the request will be aborted
    */
    maxBodySize: number | null;
    },
    ): Request {
    const ac = new AbortController();
    const onAbort = () => {
    res.off('close', onAbort);
    req.off('aborted', onAbort);
    // abort the request
    ac.abort();
    };
    res.once('close', onAbort);
    req.once('aborted', onAbort);

    I would need to investigate more, but it's possible @juliusmarminge already knows something about this behaviour

  6. nk1tz commented on Dec 3, 2025

    @nk1tz

    +1

  7. github-actions commented on Dec 20, 2025

    @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.

  8. locked as resolved and limited conversation to collaborators on Dec 20, 2025
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

    ✅ accepted-PRs-welcome🐛 bug: unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions