test(releaseupdate): fix a data race and a sleep in the install-cancellation test

Found by a full-repo 'go test -race ./...' sweep. internal/releaseupdate was
the only package that failed it.

The race: blockingInstallerStub.Install assigned s.gotCancelled lazily --

    if s.gotCancelled == nil { s.gotCancelled = make(chan struct{}) }

Install runs on the caller's goroutine (Service.Install calls it), while the
test goroutine reads s.gotCancelled. Two goroutines, one unsynchronised field.
The channel is now built by a newBlockingInstallerStub constructor, so no
goroutine ever writes it after the stub is handed to the test. Fixing it at
the producer rather than guarding the read.

The sleep: the test asserted 'the installer did not observe cancellation' with

    select { case <-s.gotCancelled: ... case <-time.After(50*time.Millisecond): }

which is a race against the clock to prove a negative, and is weaker than the
thing it stands in for -- a channel nothing has closed is simply not ready.
It is now a select/default, asserted synchronously.

The assertion was also unfalsifiable in one direction: if the channel were
never closed at all, the old test passed for the wrong reason. It now also
asserts the positive half -- after Install returns, the detached context is
released, so the observer MUST have fired -- which is what makes the negative
check meaningful.

A second constructor of the same stub is updated too; it relied on the lazy
assignment and would have closed a nil channel.

Reproduced before the fix (race detector, same test):
  WARNING: DATA RACE ... service_test.go:201
  --- FAIL: TestServiceInstallSurvivesCallerContextCancellation
After: 3/3 clean runs under -race.
This commit is contained in:
sam committed 2026-09-14 10:14:27 +00:00
1 parent bd748e8ce2
commit e2bf2d1b24
1 file changed
+26 -7
+26 -7
View File
@@ -184,7 +184,7 @@ func TestServiceInstallRefusesAnUnknownInstallType(t *testing.T) {
// rollback ever attempted, which is exactly the failure mode this feature
// exists to prevent.
func TestServiceInstallSurvivesCallerContextCancellation(t *testing.T) {
installer := &blockingInstallerStub{unblock: make(chan struct{}), started: make(chan struct{})}
installer := newBlockingInstallerStub()
service := Service{Installer: installer, InstallType: "binary"}
ctx, cancel := context.WithCancel(context.Background())
@@ -197,16 +197,28 @@ func TestServiceInstallSurvivesCallerContextCancellation(t *testing.T) {
<-installer.started
cancel() // simulate the triggering request/handler being torn down mid-install
// The installer must not observe cancellation. Assert it synchronously:
// the stub records cancellation by closing a channel, and a channel that
// nothing has closed is not ready. No sleep -- a 50ms guess is both a
// flake and a weaker check than asking the channel directly.
select {
case <-installer.gotCancelled:
t.Fatal("installer observed its context as cancelled after only the caller's context was cancelled")
case <-time.After(50 * time.Millisecond):
default:
}
close(installer.unblock)
if err := <-done; err != nil {
t.Fatalf("Install() error = %v, want nil", err)
}
// After Install returns, the detached context is released, so the
// cancellation observer must have fired. This is the positive half: the
// check above is only meaningful if the channel can close at all.
select {
case <-installer.gotCancelled:
case <-time.After(2 * time.Second):
t.Fatal("installer never observed cancellation after Install returned")
}
}
// TestServiceInstallRefusesConcurrentInstalls: Telegram and the web
@@ -215,7 +227,7 @@ func TestServiceInstallSurvivesCallerContextCancellation(t *testing.T) {
// The authoritative guard against two installs racing the same binaries
// and .prev backups has to live here, not duplicated per caller.
func TestServiceInstallRefusesConcurrentInstalls(t *testing.T) {
installer := &blockingInstallerStub{unblock: make(chan struct{}), started: make(chan struct{})}
installer := newBlockingInstallerStub()
service := Service{Installer: installer, InstallType: "binary"}
done := make(chan error, 1)
@@ -285,7 +297,9 @@ func (s *catalogStub) Check(context.Context) (Snapshot, error) { return s.snapsh
// blockingInstallerStub lets a test hold Install() open until it chooses to
// release it, and records whether the context it was given ever reported
// cancellation.
// cancellation. gotCancelled is built by the constructor, never lazily inside
// Install: Install runs on the caller's goroutine while the test reads the
// field, so assigning it there is a data race (found by -race).
type blockingInstallerStub struct {
unblock chan struct{}
started chan struct{}
@@ -293,12 +307,17 @@ type blockingInstallerStub struct {
calls int
}
func newBlockingInstallerStub() *blockingInstallerStub {
return &blockingInstallerStub{
unblock: make(chan struct{}),
started: make(chan struct{}),
gotCancelled: make(chan struct{}),
}
}
func (s *blockingInstallerStub) Install(ctx context.Context, _ Snapshot, _ InstallMeta, _ func(string)) (Result, error) {
s.calls++
close(s.started)
if s.gotCancelled == nil {
s.gotCancelled = make(chan struct{})
}
go func() {
<-ctx.Done()
close(s.gotCancelled)