Repository navigation
fix(platform): stop the manifest file watcher when the context ends - #839
Conversation
…to mwbrooks-fix-close-manifest-watcher # Conflicts: # internal/pkg/platform/localserver_test.go
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #839 +/- ##
==========================================
+ Coverage 80.78% 80.81% +0.03%
==========================================
Files 247 247
Lines 18859 18868 +9
==========================================
+ Hits 15235 15249 +14
+ Misses 3624 3619 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
WilliamBergamin
left a comment
There was a problem hiding this comment.
🧠 Thanks @mwbrooks! Waiting on w.Wait() before closing is a nice catch, since Close is a no-op until Start marks the watcher running.
One blocking comment on a test for the drain loop, the rest are optional 🙏
| for { | ||
| select { | ||
| case <-w.Event: | ||
| case <-w.Error: | ||
| case <-w.Closed: | ||
| return | ||
| } | ||
| } |
There was a problem hiding this comment.
🧪 issue (blocking): Nothing tests the drain loop: swapping it for a plain w.Close() still passes Test_LocalServer_WatchManifest. Could we add a test with one unread event pending? This one passes here and hangs without the drain (needs path/filepath):
func Test_closeWatcherOnDone_drainsPendingEvents(t *testing.T) {
dir := t.TempDir()
manifest := filepath.Join(dir, "manifest.json")
require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600))
w := watcher.New()
require.NoError(t, w.AddRecursive(dir))
later := time.Now().Add(time.Hour)
require.NoError(t, os.Chtimes(manifest, later, later))
started := make(chan error, 1)
go func() { started <- w.Start(time.Millisecond) }()
ctx, cancel := context.WithCancel(t.Context())
cancel()
time.Sleep(50 * time.Millisecond)
done := make(chan struct{})
go func() { closeWatcherOnDone(ctx, w); close(done) }()
select {
case <-done:
case <-time.After(time.Second):
require.FailNow(t, "closeWatcherOnDone did not return with an unread event")
}
require.NoError(t, <-started)
}There was a problem hiding this comment.
Added in commit 170f9e0, thanks for the test! 🙇
| // Begin watching for manifest file changes | ||
| go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app) | ||
|
|
||
| // Stop the watcher when the context ends so Start returns |
There was a problem hiding this comment.
🪓 nitpick (non-blocking): The function name already says this, so the comment could go.
| // Stop the watcher when the context ends so Start returns |
| return w.Start(time.Millisecond * 100) | ||
| } | ||
|
|
||
| // closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes |
There was a problem hiding this comment.
🪓 nitpick (non-blocking): Same here, the drain test above could carry the why instead of the docstring.
| // closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes |
…to mwbrooks-fix-close-manifest-watcher # Conflicts: # internal/pkg/platform/localserver_test.go
…nifest-watcher # Conflicts: # internal/pkg/platform/localserver_test.go
WilliamBergamin
left a comment
There was a problem hiding this comment.
🙏 @mwbrooks thanks, the new Test_closeWatcherOnDone fails when the drain is swapped for a plain Close, which covers the case I was worried about. Left two optional comments, nothing blocking 💯
| } | ||
|
|
||
| func Test_closeWatcherOnDone(t *testing.T) { | ||
| // Start blocks sending an event that nobody reads after the context ends, so closing must drain it |
There was a problem hiding this comment.
🪓 nitpick (non-blocking): Could the why move into the test name and the comment go? Something like Test_closeWatcherOnDone_drainsAnUnreadEvent.
| // Start blocks sending an event that nobody reads after the context ends, so closing must drain it |
| go func() { started <- w.Start(time.Millisecond) }() | ||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
| time.Sleep(50 * time.Millisecond) |
There was a problem hiding this comment.
🤔 question (non-blocking): On a slow runner Start may not have reached its blocked send within 50ms. The test would then pass even with a plain Close. It held up 200/200 locally, so this is only something to keep in mind if you see a cheap way to wait on the pending event.
There was a problem hiding this comment.
Good point! The event channel is unbuffered, so the test can't see the pending send without reading it. I'll keep the sleep for now and revisit if it flakes in CI.
The blocking concerns are resolved; see the latest review.
|
Thanks @WilliamBergamin - I've requested a re-review. @zimeg is away, so I'll need an approval from you. ✅ |
WilliamBergamin
left a comment
There was a problem hiding this comment.
🏷️ Thanks @mwbrooks! The new test name says what the drain protects, so the comment could go.
The CircleCI build and e2e jobs are red on this head, but make build-ci and the goreleaser snapshot pass locally, so could you re-run them? 🙏
|
Thanks @WilliamBergamin for the second look! 🙇 Both notes are addressed in the threads. Could you take a refresh review when you have a moment? |
|
Thanks @WilliamBergamin! 🙇 The red build was a proxy.golang.org stream error while downloading modules. I merged |
WilliamBergamin
left a comment
There was a problem hiding this comment.
💯 Thanks @mwbrooks, the drain-until-Closed shape makes WatchManifest return cleanly, and the new test hangs without it.
CI is green after the main merge, and the sleep tradeoff in the test thread sounds good to revisit only if it flakes 🚀
|
Thanks @WilliamBergamin for the careful reviews and for checking that the test hangs without the drain! 🙇 |
Changelog
N/A - internal cleanup with no user-facing change.
Summary
This pull request lets maintainers test the manifest watching path of
WatchManifest, because it now returns once the context ends. Before, nothing closed the file watcher, sow.Startblocked forever and a test of that path would hang. @WilliamBergamin suggested it in review on #831 (a PR).closeWatcherOnDone, which waits for the watcher to start, closes it once the context ends, and drains events and errors until it closes. Draining stopsw.Startfrom blocking on an event send that nobody reads afterwatchManifestEventsreturns.Test_LocalServer_WatchManifestthat watches a temp folder on a live context, checksWatchManifestkeeps running, then cancels and expectsnilStacked on #831, which adds
Test_LocalServer_WatchManifest.WatchApphas the same open watcher. It is left for a separate PR, because it also waits on the delegated server.Testing
go test ./internal/pkg/platform/... -run Test_LocalServer_WatchManifest -race -vgo closeWatcherOnDone(ctx, w)line and confirm the new test fails with "did not return after the context ended"Requirements