Skip to content

Fix the issue that SSE connection can not be disconnected in some cases [INS-5464]#8722

Merged
yaoweiprc merged 8 commits into
developfrom
fix/app-freeze-sse-error
May 29, 2025
Merged

Fix the issue that SSE connection can not be disconnected in some cases [INS-5464]#8722
yaoweiprc merged 8 commits into
developfrom
fix/app-freeze-sse-error

Conversation

@yaoweiprc
Copy link
Copy Markdown
Contributor

When connecting the Insomnia client to a locally running backend service, switching between multiple organizations causes HTTP requests to remain in a pending state.
image

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

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.

@yaoweiprc yaoweiprc requested a review from a team May 15, 2025 12:50
@yaoweiprc yaoweiprc changed the title Fix the issue that SSE connection can not be disconnected in some cases Fix the issue that SSE connection can not be disconnected in some cases [INS-5464] May 15, 2025
gatzjames
gatzjames previously approved these changes May 15, 2025
Copy link
Copy Markdown
Contributor

@gatzjames gatzjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏👏 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! 👍

Comment thread packages/insomnia/src/main/api.protocol.ts
Comment thread packages/insomnia/src/main/api.protocol.ts
Comment thread packages/insomnia/src/main/api.protocol.ts Outdated
@yaoweiprc yaoweiprc force-pushed the fix/app-freeze-sse-error branch 3 times, most recently from df4f091 to b1c7377 Compare May 22, 2025 10:08
gatzjames
gatzjames previously approved these changes May 26, 2025
@yaoweiprc yaoweiprc force-pushed the fix/app-freeze-sse-error branch from b1c7377 to bda406f Compare May 28, 2025 02:22
@yaoweiprc yaoweiprc force-pushed the fix/app-freeze-sse-error branch from f44e1e0 to 4d8bd38 Compare May 28, 2025 09:42
@yaoweiprc yaoweiprc enabled auto-merge (squash) May 28, 2025 09:52
@yaoweiprc yaoweiprc force-pushed the fix/app-freeze-sse-error branch from 4d8bd38 to 441fefe Compare May 29, 2025 00:45
@yaoweiprc yaoweiprc merged commit 6cff4d2 into develop May 29, 2025
10 checks passed
@yaoweiprc yaoweiprc deleted the fix/app-freeze-sse-error branch May 29, 2025 00:59
cwangsmv pushed a commit that referenced this pull request Jun 10, 2025
…es [INS-5464] (#8722)

* fix the issue that SSE connection can not be disconnected in some cases
* follow system proxy by default for SSE request
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
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.

2 participants