Skip to content

fix(platform): stop the manifest file watcher when the context ends - #839

Merged
mwbrooks merged 18 commits into
mainfrom
mwbrooks-fix-close-manifest-watcher
Nov 24, 2026
Merged

mwbrooks merged 18 commits into
mainfrom
mwbrooks-fix-close-manifest-watcher

Conversation

@mwbrooks

@mwbrooks mwbrooks commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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, so w.Start blocked forever and a test of that path would hang. @WilliamBergamin suggested it in review on #831 (a PR).

  • Adds closeWatcherOnDone, which waits for the watcher to start, closes it once the context ends, and drains events and errors until it closes. Draining stops w.Start from blocking on an event send that nobody reads after watchManifestEvents returns.
  • Adds a case to Test_LocalServer_WatchManifest that watches a temp folder on a live context, checks WatchManifest keeps running, then cancels and expects nil

Stacked on #831, which adds Test_LocalServer_WatchManifest. WatchApp has the same open watcher. It is left for a separate PR, because it also waits on the delegated server.

Testing

  1. Run go test ./internal/pkg/platform/... -run Test_LocalServer_WatchManifest -race -v
  2. Optional: remove the go closeWatcherOnDone(ctx, w) line and confirm the new test fails with "did not return after the context ended"

Requirements

@mwbrooks mwbrooks added this to the Next Release milestone Nov 23, 2026
@mwbrooks mwbrooks added code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment labels Nov 23, 2026
@mwbrooks mwbrooks self-assigned this Nov 23, 2026
…to mwbrooks-fix-close-manifest-watcher

# Conflicts:
#	internal/pkg/platform/localserver_test.go
@mwbrooks
mwbrooks changed the base branch from main to mwbrooks-test-watch-manifest November 23, 2026 22:09
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.81%. Comparing base (8cc2a23) to head (8c1d82d).

Files with missing lines Patch % Lines
internal/pkg/platform/localserver.go 88.88% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mwbrooks
mwbrooks marked this pull request as ready for review November 23, 2026 22:13
@mwbrooks
mwbrooks requested a review from a team as a code owner November 23, 2026 22:13

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧠 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 🙏

Comment on lines +413 to +420
for {
select {
case <-w.Event:
case <-w.Error:
case <-w.Closed:
return
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧪 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)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in commit 170f9e0, thanks for the test! 🙇

Comment thread internal/pkg/platform/localserver.go Outdated
// Begin watching for manifest file changes
go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app)

// Stop the watcher when the context ends so Start returns

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🪓 nitpick (non-blocking): The function name already says this, so the comment could go.

Suggested change
// Stop the watcher when the context ends so Start returns

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed in commit 170f9e0, thanks!

Comment thread internal/pkg/platform/localserver.go Outdated
return w.Start(time.Millisecond * 100)
}

// closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🪓 nitpick (non-blocking): Same here, the drain test above could carry the why instead of the docstring.

Suggested change
// closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed in commit 170f9e0, thanks!

Base automatically changed from mwbrooks-test-watch-manifest to main November 23, 2026 23:10
…nifest-watcher

# Conflicts:
#	internal/pkg/platform/localserver_test.go

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🙏 @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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🪓 nitpick (non-blocking): Could the why move into the test name and the comment go? Something like Test_closeWatcherOnDone_drainsAnUnreadEvent.

Suggested change
// Start blocks sending an event that nobody reads after the context ends, so closing must drain it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Renamed in commit 928e415, thanks! 🙇

go func() { started <- w.Start(time.Millisecond) }()
ctx, cancel := context.WithCancel(t.Context())
cancel()
time.Sleep(50 * time.Millisecond)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@WilliamBergamin
WilliamBergamin dismissed their stale review November 24, 2026 14:27

The blocking concerns are resolved; see the latest review.

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin - I've requested a re-review. @zimeg is away, so I'll need an approval from you. ✅

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 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? 🙏

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin for the second look! 🙇 Both notes are addressed in the threads. Could you take a refresh review when you have a moment?

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin! 🙇 The red build was a proxy.golang.org stream error while downloading modules. I merged main in (4d6148f), which starts a fresh CI run. Could you take a look once it's green?

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💯 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 🚀

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin for the careful reviews and for checking that the test hangs without the drain! 🙇

@mwbrooks
mwbrooks enabled auto-merge (squash) November 24, 2026 19:03
@mwbrooks
mwbrooks merged commit 0bde480 into main Nov 24, 2026
14 checks passed
@mwbrooks
mwbrooks deleted the mwbrooks-fix-close-manifest-watcher branch November 24, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants