Skip to content

Fix: Prevent infinite recursion when closing streamable sessions#11

Closed
joshuasundance-swca wants to merge 2 commits into
hustcc:mainfrom
joshuasundance-swca:streamable-session-recursion
Closed

Fix: Prevent infinite recursion when closing streamable sessions#11
joshuasundance-swca wants to merge 2 commits into
hustcc:mainfrom
joshuasundance-swca:streamable-session-recursion

Conversation

@joshuasundance-swca

Copy link
Copy Markdown

Summary

This PR resolves an infinite recursion bug that occurred when closing a streamable HTTP session. Previously, the transport.onclose handler called server.close(), which recursively triggered another close, resulting in a stack overflow (RangeError: Maximum call stack size exceeded).

Details

  • Root Cause:
    The transport.onclose handler was calling server.close(), which in turn called transport.close(), re-triggering onclose and causing infinite recursion.
  • Solution:
    The call to server.close() has been removed from the transport.onclose handler. Now, the handler only cleans up the activeTransports map, allowing the SDK to manage the closing lifecycle as designed.

Testing

  • Started the service, established a session, and issued a DELETE request.
  • Verified that the session closes cleanly and the server remains stable.
  • No more RangeError: Maximum call stack size exceeded errors observed.

Related Issue

Closes #10


Disclaimer

I am not a strong TypeScript developer, and this fix was made with the help of AI assistance. However, based on testing, the change does accomplish the expected behavior and resolves the infinite recursion issue.

If there are any concerns about implementation details, please review and suggest improvements as needed! Thank you. :)

@hustcc

hustcc commented Sep 12, 2025

Copy link
Copy Markdown
Owner

Maybe it will cause other problem!

Can help to change to express in sse and streamable? like this.

@joshuasundance-swca

Copy link
Copy Markdown
Author

Thank you for your comment. I thought about the original, with its comment about memory leaks, and made additional changes.

Now you'll see this message in the logs:

⚠️ Transport already closed, skipping onclose handler to avoid infinite recursion

In the new changes, I think the isServerClosed flag is unnecessary. But this latest change should

  1. Avoid infinite recursion
  2. Avoid memory leaks

All changes are tested for integration with LangChain in a docker compose setup, but as I said, I do not consider myself a strong TypeScript developer and I'm not sure this is the ideal solution.


I think it's a great idea to use express, but I do not have much firsthand experience with it... I'm more of a Python+fastapi person. 🐍

@hustcc

hustcc commented Sep 12, 2025

Copy link
Copy Markdown
Owner

Ok, I will process this problem this weekend.🫡

@hustcc

hustcc commented Sep 13, 2025

Copy link
Copy Markdown
Owner

@joshuasundance-swca Can help to test in your case?

@joshuasundance-swca

Copy link
Copy Markdown
Author

Closing in favor of #12 👀 which I've tested

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.

Infinite Recursion When Closing Streamable Sessions (RangeError: Maximum call stack size exceeded)

2 participants