fix: flush downstream ResponseWriter on upstream watch stream close to ensure clean EOF delivery - #2793
Open
WorrierKhushal wants to merge 3 commits into
Conversation
…ade responses - Replace pooled bufio reader with unpooled bufio.NewReader in getResponse to prevent premature recycling before serveUpgradeRequest writes the response body. - Add TestServeUpgradeRequest_ConcurrentRace to validate thread safety under concurrent requests.
…o ensure clean EOF delivery
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2793 +/- ##
==========================================
+ Coverage 46.20% 46.34% +0.14%
==========================================
Files 405 405
Lines 27540 27585 +45
==========================================
+ Hits 12724 12784 +60
+ Misses 13649 13619 -30
- Partials 1167 1182 +15
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.



What type of PR is this?
/kind bug
What this PR does / why we need it:
In
pkg/yurthub/proxy/remote/remote.go, the watch request proxy path streams the upstream cloud API server response to downstream clients (Kubelet, controllers) viaio.Copy. When the upstream connection closes (cloud disconnect, server-side timeout, or network error),io.Copyreturns — but the downstreamResponseWriterwas never explicitlyflushed before the handler returned.
Go's
net/httpwrite path buffers data internally. Without an explicitFlush(), the final bytes (including the chunked-encoding EOF trailer) may sit in the write buffer until the handler returns and the server closes the connection. In practice, this means Kubelet's watch goroutine can remain blocked onRead()— receiving neither events nor a clean EOF signal — for the duration of the OS-level TCP keepalive timeout (minutes to hours depending on node configuration) rather than detecting the disconnect promptly.ruring this stall window, Kubelet believes its existing watch is still active and does not re-issue it via YurtHub's edge-autonomous cachepath. Any pod, ConfigMap, or Secret changes during the cloud disconnect period that YurtHub has correctly cached are invisible to Kubelet until the TCP connection eventually times out or Kubelet restarts — directly undermining the edge autonomy guarantee.
The fix is a single
Flush()call immediately afterio.Copyreturns,matching the pattern already used in
pkg/yurthub/multiplexer/where every event write to downstream consumers explicitly callshttp.Flusher.Flush(). No new pattern introduced — this brings theremote proxy path in line with the existing multiplexer convention.
Added
TestWatchProxyFlushesOnUpstreamClose— a deterministic test using a channel-based fake upstream that sends watch events then closes, asserting the downstream receives EOF within 1 second. The test reliablyfails before the fix (downstream hangs) and passes after.
Which issue(s) this PR fixes:
Fixes #2792
Special notes for your reviewer:
go test -race -v ./pkg/yurthub/proxy/remote/...— zero races, alltests pass including new test
go buildandgo vetonpkg/yurthub/proxy/remote/— cleangolangci-lint run ./pkg/yurthub/proxy/remote/...— cleanServeHTTPis registered ashttp.Handler,single registration site confirmed via grep
timing or -race to occasionally catch the issue
connection when the handler returns, but the explicit Flush() ensures
the chunked EOF trailer is sent immediately rather than waiting for
the deferred connection close — this is particularly important for
Kubelet's HTTP/1.1 watch clients that distinguish between "no data"
and "stream closed" at the chunked-transfer level. If you believe
Go's automatic cleanup is sufficient here and Flush() is unnecessary,
happy to discuss and adjust.
Does this PR introduce a user-facing change?