Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6461: fix(channel): cancel completed supervisor contexts (#7281)

LinYushenabde90ce9f3e
diff --git a/server/internal/integrations/channel/engine/supervisor.go b/server/internal/integrations/channel/engine/supervisor.go
index 3aab7040c3..ec51b671ac 100644
--- a/server/internal/integrations/channel/engine/supervisor.go
+++ b/server/internal/integrations/channel/engine/supervisor.go
@@ -589,7 +589,14 @@ func (s *Supervisor) startSupervisor(parent context.Context, inst Installation)
}
s.wg.Add(1)
s.mu.Unlock()
- go s.supervise(ctx, inst, id, gen, done)
+ go func() {
+ defer s.wg.Done()
+ // A supervisor can exit without an explicit cancellation when lease
+ // acquisition is contended or fails. Always detach its child context
+ // from Run's long-lived parent when the goroutine returns.
+ defer cancel()
+ s.supervise(ctx, inst, id, gen, done)
+ }()
}
// leaseToken composes the per-supervisor lease token: the process-wide
@@ -615,7 +622,6 @@ func leaseToken(nodeID string, gen uint64) string {
// while it runs → on exit, release + back off → repeat. Returns when ctx is
// cancelled.
func (s *Supervisor) supervise(ctx context.Context, inst Installation, id string, gen uint64, done chan<- struct{}) {
- defer s.wg.Done()
defer close(done)
defer func() {
// Only clear the map entry if it still belongs to us — gen
diff --git a/server/internal/integrations/channel/engine/supervisor_test.go b/server/internal/integrations/channel/engine/supervisor_test.go
index 9fe18d1188..568e12ca29 100644
--- a/server/internal/integrations/channel/engine/supervisor_test.go
+++ b/server/internal/integrations/channel/engine/supervisor_test.go
@@ -158,6 +158,30 @@ func (f *fakeStore) leaseHolder(id pgtype.UUID) (string, bool) {
return owner, ok
}
+type contextCapturingContendedLeaseStore struct {
+ acquireCtx chan context.Context
+}
+
+func (s *contextCapturingContendedLeaseStore) ListHeldWSLeases(_ context.Context, _ []pgtype.UUID) (map[string]struct{}, error) {
+ return map[string]struct{}{}, nil
+}
+
+func (s *contextCapturingContendedLeaseStore) TryAcquireWSLease(ctx context.Context, _ AcquireLeaseParams) error {
+ select {
+ case s.acquireCtx <- ctx:
+ default:
+ }
+ return ErrLeaseNotAcquired
+}
+
+func (s *contextCapturingContendedLeaseStore) RenewWSLease(_ context.Context, _ AcquireLeaseParams) error {
+ return errors.New("unexpected lease renewal")
+}
+
+func (s *contextCapturingContendedLeaseStore) ReleaseWSLease(_ context.Context, _ ReleaseLeaseParams) error {
+ return nil
+}
+
// fakeChannel is a channel.Channel whose Connect behaves per a script
// (default: block until ctx is cancelled). It records connect/disconnect
// counts and captures the injected handler.
@@ -389,6 +413,44 @@ func TestSupervisorInjectsHandler(t *testing.T) {
sup.Wait()
}
+func TestSupervisorCancelsChildContextAfterContendedAcquire(t *testing.T) {
+ q := newFakeStore()
+ instID := uuidFromString(t, "21212121-2121-2121-2121-212121212121")
+ inst := activeInst(instID, "fp1")
+ leases := &contextCapturingContendedLeaseStore{acquireCtx: make(chan context.Context, 1)}
+
+ fc := &fakeChannel{typ: channel.TypeFeishu}
+ var builds int32
+ sup := NewSupervisor(q, leases, fakeRegistry(fc, &builds, nil), nil, fastConfig())
+ parentCtx, parentCancel := context.WithCancel(context.Background())
+ defer func() {
+ parentCancel()
+ sup.wg.Wait()
+ }()
+
+ // Start one supervisor directly so the test observes exactly one lease
+ // attempt; Run would continue sweeping and could start another attempt.
+ sup.startSupervisor(parentCtx, inst)
+ var acquireCtx context.Context
+ select {
+ case acquireCtx = <-leases.acquireCtx:
+ case <-time.After(300 * time.Millisecond):
+ t.Fatal("supervisor did not attempt to acquire the lease")
+ }
+
+ select {
+ case <-acquireCtx.Done():
+ if !errors.Is(acquireCtx.Err(), context.Canceled) {
+ t.Fatalf("acquire context error = %v, want context.Canceled", acquireCtx.Err())
+ }
+ case <-time.After(300 * time.Millisecond):
+ t.Fatal("supervisor returned after lease contention without cancelling its child context")
+ }
+ if got := atomic.LoadInt32(&builds); got != 0 {
+ t.Fatalf("contended lease should not build a channel, builds=%d", got)
+ }
+}
+
func TestSupervisorSkipsWhenAnotherReplicaHoldsLease(t *testing.T) {
q := newFakeStore()
instID := uuidFromString(t, "22222222-2222-2222-2222-222222222222")