Skip to content

Commit 903cabb

Browse files
purpleideaLuap99
authored andcommitted
dbus: Don't deadlock on shutdown
When cancelling the ctx, if we have a pending channel send, then we'll deadlock. The correct pattern is to abort if ctx exits. We also close the channels when the sender closes.
1 parent 359a569 commit 903cabb

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

dbus/subscription.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func (c *Conn) SubscribeUnitsCustomContext(ctx context.Context, interval time.Du
120120
errChan := make(chan error, buffer)
121121

122122
go func() {
123+
defer close(statusChan)
124+
defer close(errChan)
123125
for {
124126
timerChan := time.After(interval)
125127

@@ -150,18 +152,24 @@ func (c *Conn) SubscribeUnitsCustomContext(ctx context.Context, interval time.Du
150152
old = cur
151153

152154
if len(changed) != 0 {
153-
statusChan <- changed
155+
select {
156+
case statusChan <- changed:
157+
case <-ctx.Done():
158+
return
159+
}
154160
}
155161
} else {
156-
errChan <- err
162+
select {
163+
case errChan <- err:
164+
case <-ctx.Done():
165+
return
166+
}
157167
}
158168

159169
select {
160170
case <-timerChan:
161171
continue
162172
case <-ctx.Done():
163-
close(statusChan)
164-
close(errChan)
165173
return
166174
}
167175
}

0 commit comments

Comments
 (0)