Repository navigation
fix: Close the response body when a stream stops - #114
Open
kinyoklion wants to merge 2 commits into
Open
kinyoklion wants to merge 2 commits into
kinyoklion wants to merge 2 commits into
Conversation
EventSource stopped a stream by cancelling the request, but it did not close the response body when the stream stopped on the reading thread, for example when interrupt() is called inside the anyEvents() loop. On HTTP/2, cancelling only resets the stream, so the request stays attached to the connection and the connection is never released. The reading thread now closes the input stream whenever it stops using a stream, after the connection closer has run.
kinyoklion
commented
Oct 12, 2026
|
|
||
| sleepNotifier.notify(); // in case we're sleeping in a reconnect delay, wake us up | ||
| } | ||
| if (oldReadingThread == Thread.currentThread()) { |
Member
Author
There was a problem hiding this comment.
This is outside the synchronized block because it closing the connection is a potentially blocking call.
kinyoklion
marked this pull request as ready for review
October 12, 2026 17:21
This branch has not been deployed
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.
Summary
EventSourcestops a stream by cancelling the OkHttp call, but when the stream stops on the reading thread (for example,interrupt()called inside theanyEvents()loop), it never closes the response body. OkHttp requires the body to be closed. On HTTP/2, cancelling only resets the stream, so the call stays attached to the connection: the connection is never released, the reconnected stream is multiplexed onto it, and if that connection later dies silently, the next reconnect can reuse it and wait for another read timeout.The reading thread now closes the input stream whenever it stops using a stream, after the connection closer has run. The close stays on the reading thread because the stream is not safe to close while another thread reads from it. Custom
ConnectStrategyimplementations now also see their input stream closed when a stream stops; thegetInputStream()Javadoc says so.New tests use real HTTP/2 connections (MockWebServer, added as a test dependency) and fail without this change.
Note
Overview
Fixes a leak where stopping an SSE stream cancelled the OkHttp request but left the response body open. On HTTP/2, that kept the connection tied to the active response and could break reconnect behavior (stale connections, read timeouts).
EventSourcenow tracks the connectInputStreamand, on the reading thread only, runsdiscardEventParser()after the connection closer—clearing the parser and closing the stream so the HTTP response is released. The same path runs on errors and end-of-stream.ConnectStrategy.Result#getInputStream()Javadoc documents this contract for custom strategies.Adds
mockwebserverfor tests:EventSourceStreamReleaseTestasserts closer-then-close ordering acrossinterrupt/stop/close(same or other thread), andHttpConnectStrategyHttp2Testverifies the OkHttp connection pool goes idle and reconnects on a fresh connection when needed.Reviewed by Cursor Bugbot for commit 3114c80. Bugbot is set up for automated code reviews on this repo. Configure here.