Fix the issue that SSE connection can not be disconnected in some cases [INS-5464]#8722
Merged
Conversation
gatzjames
previously approved these changes
May 15, 2025
Contributor
gatzjames
left a comment
There was a problem hiding this comment.
🙏👏 Awesome write-up and explanation!
Electron does not provide a way to make net.request bypass other protocol handlers.
Curious if you tried the bypassCustomProtocolHandlers option in net.fetch.
We should double check the system Certificates and Proxy options work as before
but overall looks good to me! 👍
df4f091 to
b1c7377
Compare
gatzjames
previously approved these changes
May 26, 2025
b1c7377 to
bda406f
Compare
f44e1e0 to
4d8bd38
Compare
gatzjames
approved these changes
May 28, 2025
4d8bd38 to
441fefe
Compare
RoamingLost
pushed a commit
to RoamingLost/insomnia
that referenced
this pull request
Aug 6, 2025
…es [INS-5464] (Kong#8722) * fix the issue that SSE connection can not be disconnected in some cases * follow system proxy by default for SSE request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When connecting the Insomnia client to a locally running backend service, switching between multiple organizations causes HTTP requests to remain in a pending state.

I captured and analyzed the client’s network traffic and found that when switching organizations, the old SSE connections are not being closed. Once the number of SSE connections exceeds 6, all new requests remain in a pending state.

In the renderer process, the EventSource.close method is explicitly called to close old connections when switching organizations, but it clearly doesn’t take effect.
In the Insomnia app, we are not sending requests directly from the renderer process — instead, they are forwarded from the main process using electron.net.fetch.
net.fetch is indeed built on top of Chromium's native networking library, so it makes sense that it's subject to Chromium's per-domain connection limits.
The root cause of the issue was that under certain network conditions, Electron's net.fetch method fails to properly close SSE (Server-Sent Events) connections.
To work around this problem, I explored alternative solutions. I eventually found that using net.request inside protocol.handle, and manually constructing a Response object, provided a viable workaround. By using a ReadableStream as the Response.body and listening for its cancel event, I was able to accurately detect when the SSE connection was closed from the renderer process.
However, this workaround introduced a new problem. Since we're also forwarding regular HTTP requests through protocol.handle, the SSE requests made with net.request are inadvertently intercepted again by the handler designed for HTTP forwarding. That handler uses net.fetch, which brings us back to the original issue—SSE connections still can't be properly closed.
Electron does not provide a way to make net.request bypass other protocol handlers.
As a result, the only viable solution is to avoid using Electron’s built-in networking APIs for SSE entirely. Instead, I switched to using node-libcurl, which successfully establishes and closes SSE connections as expected.