Repository navigation
vstreamer: stop the throttle goroutine when parseEvents returns - #22171
goingforstudying-ctrl wants to merge 3 commits into
Conversation
parseEvents starts throttleEvents on the caller's context, but returns on its own: io.EOF at a stop position, or any error from the binlog connection. Nothing cancelled the goroutine, so it outlived parseEvents: - a goroutine that had already read the next binlog event stayed blocked forwarding it to throttledEvents until the caller's context ended, holding the event the whole time; - a goroutine sitting in the fully-throttled branch sent on throttlerErrs after fullyThrottledTimeout, but parseEvents had already closed the channel via defer. The send panicked and crashed vttablet. The copy-phase fast-forward vstreamer hits this: it ends with io.EOF at its stop position, and its app is the non-exempt "vstreamer". Since vitessio#21887, a DDL that stays uncommitted past --schema-change-reload-timeout fails the schema tracker's and messager's vstreams on each retry (~35s), leaving one more leaked goroutine behind every time. Derive a context for the goroutine that is cancelled when parseEvents returns, and stop closing throttlerErrs: it has a buffer of one and gets at most one send, so leaving it unclosed is safe, while a late send can no longer panic on a closed channel. The regression test drives parseEvents without MySQL and panics on the old code as the orphaned goroutine reports fully-throttled after parseEvents has returned. Fixes vitessio#22118 Signed-off-by: goingforstudying-ctrl <goingforstudying@gmail.com>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
There was a problem hiding this comment.
🟡 Changes recommended
The regression test can pass without the cancellation fix and lacks an explicit worker-exit assertion, while its background context can leave workers running after test failure or timeout.
2 open issues
What changed in this PR
This PR addresses #22118 by tying the vstreamer throttle worker’s lifetime to parseEvents, preventing leaks and late-send panics when parsing ends before the caller’s context.
Changes:
- Cancel a derived context when
parseEventsreturns. - Leave the buffered throttler error channel open to avoid racing with a late send.
- Add a regression test for parsing ending while the worker is fully throttled.
| File | Description |
|---|---|
go/vt/vttablet/tabletserver/vstreamer/vstreamer.go |
Adds worker cancellation and removes error-channel closure. |
go/vt/vttablet/tabletserver/vstreamer/vstreamer_throttler_goroutine_test.go |
Adds a regression scenario with a caller context that outlives parsing. |
Review effort: Balanced
mattlord
left a comment
There was a problem hiding this comment.
-
Blocking: I agree with Copilot's thread that the test should assert that the throttler goroutine exits, and cover the goroutine blocked forwarding an event. I ran the mutation: with
context.WithCancelanddefer cancel()removed fromvstreamer.go:420-421,TestParseEventsStopsThrottlerGoroutinestill passes without-race. Under-raceit fails only because the race detector catches the leaked goroutine readingfullyThrottledTimeoutatvstreamer.go:442while the test's cleanup restores it, so only the Unit Test (Race) job would notice, and only by accident. The blocked-forwarding leak, which #22118 lists first, isn't exercised at all. I think that we should check the goroutine directly while the parent context is still live, for example withassert.Neverthat the test engine'sthrottledCountsgrows by more than one afterparseEventsreturns, withfullyThrottledTimeoutleft at its default so that the timeout can't end the goroutine instead. That would also replace the 6.5s of sleeps. And then add a case where the goroutine has read an event that the main loop never takes. What do you think? -
I agree with Copilot's thread on using a context derived from
t.Context()invstreamer_throttler_goroutine_test.go:91. -
I think that we should pass empty names to
stats.NewCounterandstats.NewCountersWithSingleLabelinvstreamer_throttler_goroutine_test.go:73-74, aswraperror_test.go:52does. Named stats are published to expvar, sogo test -count=2 -run TestParseEventsStopsThrottlerGoroutinepanics on the second run withReuse of exported var name: TestParseEventsVstreamerThrottledCounts.
Thanks!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #22171 +/- ##
===========================================
+ Coverage 69.67% 81.76% +12.09%
===========================================
Files 1614 14 -1600
Lines 216793 3055 -213738
===========================================
- Hits 151044 2498 -148546
+ Misses 65749 557 -65192
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:
|
The previous test passed even without the derived context: the worker sent into the buffered throttlerErrs and left at its timeout, so nothing failed. Drive parseEvents from a t.Context-derived context, wait for the worker to reach the fully-throttled branch on the throttled counter instead of sleeping, and assert the counter stays frozen once parseEvents returns, inside a window shorter than fullyThrottledTimeout. Also cover the second leak path: a worker blocked forwarding an event cannot leave through fullyThrottledTimeout at all. Park parseEvents in vs.send on the heartbeat path, hand the worker an event it cannot forward, and assert the parseEvents goroutines are gone while the caller's context is still alive. Signed-off-by: goingforstudying-ctrl <goingforstudying@gmail.com>
There was a problem hiding this comment.
🟡 Changes recommended
The regression tests can fail spuriously on a post-cancellation counter increment and panic under repeated execution because they register fixed process-wide metric names.
2 open issues
2 resolved since last review
Review effort: Balanced
mattlord
left a comment
There was a problem hiding this comment.
-
Blocking: The DCO check fails because the two new commits, c4052c1 and 4f8589a, have no
Signed-off-by:trailer (the first commit has one). I think thatgit rebase --signoff HEAD~2and a force-push will fix it. -
Thanks for the test rework. All three earlier points look addressed to me. With
context.WithCancelanddefer cancel()removed fromvstreamer.go:420-421, both tests now fail without-race, and-count=2passes. InTestParseEventsStopsThrottlerGoroutine, the wait for the worker to exit isfullyThrottledTimeout/2, which is 1s. With the fix, the worker should exit within one throttle check (about 250ms), so the margin is fine locally, but it seems like it could flake on a loaded race runner, and the repo's guideline is CI timeouts of at least 30 seconds. I wonder if we should raisefullyThrottledTimeoutin the test to something like 2 minutes and wait up to 30s forparseEventsGone. A leaked worker would still be caught long before its timeout. OnceparseEventsGoneholds, theassert.Neveron the counter seems redundant too, so the test could drop it and save a second.
Thanks!
The tests registered counters under fixed process-wide names, so a second run under -count=2 panicked on the duplicate expvar publish. Keep them unnamed. Cancellation is delivered to the throttle worker's select but the worker can still have an increment in flight when parseEvents returns, so an immediate counter freeze check could flap. Wait for the worker to actually be gone, with a deadline under fullyThrottledTimeout, before asserting the counter stays frozen. Signed-off-by: goingforstudying-ctrl <goingforstudying@gmail.com>
4f8589a to
ed2bde3
Compare
|
All addressed in the latest push:
|
mattlord
left a comment
There was a problem hiding this comment.
Thanks for the updates! The DCO check passes now, and the test changes look good to me. With the derived context and defer cancel() removed, both tests still fail, -race -count=2 passes at this head, and the full vstreamer package passes too.
Looks good to me.

Description
parseEventslaunchesthrottleEventson the caller's context but returns on its own — io.EOF at a stop position, or any error from the binlog connection — and nothing cancelled the goroutine. Two ways that bites, from #22118:throttledEventsuntil the caller's context ends, holding the event the whole time;throttlerErrsafterfullyThrottledTimeout— butparseEventsalready closed the channel via defer, so the send panics and crashes vttablet. The copy-phase fast-forward vstreamer can hit this one: it ends with io.EOF at its stop position and its app is the non-exempt "vstreamer".Since #21887 the leak also has a production trigger: a DDL that stays uncommitted past
--schema-change-reload-timeoutfails the schema tracker's and messager's vstreams on each retry (~35s), and every retry leaves one more goroutine behind.The change derives a context for the goroutine that is cancelled when
parseEventsreturns, and stops closingthrottlerErrs— it has a buffer of one and at most one send, so leaving it unclosed is safe while the late send can no longer panic on a closed channel. Behavior when the caller's context ends first is unchanged.I think this should be back-ported to release-23.0 and release-24.0 together with #21887, per the issue.
Motivation
How do you use Vitess? (pick one)
Did you run this change against a real Vitess deployment?
No deployment involved; the bug reproduces in the vstreamer package alone. The new
TestParseEventsStopsThrottlerGoroutinedrivesparseEventswithout MySQL: a nil-throttler client plusthrottlerapp.TestingAlwaysThrottledNameparks the goroutine in the fully-throttled branch, then the errs channel endsparseEventswhile the caller's context stays alive. On main the test binary panics withsend on closed channelat vstreamer.go:434 — the orphaned goroutine reporting fully-throttled after the channel was closed. With the change the goroutine observes the derived cancellation and the test passes. Also ran the existingTestHeartbeat,TestFullyThrottledTimeoutandTestVStreamerThrottledCountsagainst the change, all green, plus gofmt and go vet.Related Issue(s)
Fixes #22118
Checklist
Deployment Notes
None.
AI Disclosure
The investigation, the change and the regression test were done with an AI coding assistant, under my direction and review.