Skip to content

Commit 0ac052c

Browse files
authored
Tweaks to the interruptStream (viamrobotics#4259)
My original plan was to move this to using a `StoppableWorkers`, so that we definitely shut down the background goroutine when we call `closeStream()` (right now, it cancels the context and returns without waiting for the goroutine to actually stop). However, this is trickier than I realized: the context that is canceled is _not_ descended from the background context: it's passed in as an argument and might be canceled by something else! So, this PR is some preliminary cleanup without using a `StoppableWorkers` yet. Thanks to Olivia for pointing out that we don't actually need to wait for the background goroutine to start up, and can return immediately without those syncing up. Tried on an rpi5 with a modular board component and a modular ultrasonic sensor (so, streaming interrupts from one module to the RDK, and then streaming those same interrupts from the RDK to another module): everything works great!
1 parent ac02ee6 commit 0ac052c

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

components/board/client.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,14 @@ func (c *client) StreamTicks(ctx context.Context, interrupts []DigitalInterrupt,
204204
client: c,
205205
}
206206

207-
c.mu.Lock()
208-
c.interruptStreams = append(c.interruptStreams, stream)
209-
c.mu.Unlock()
210-
211207
err = stream.startStream(ctx, interrupts, ch)
212208
if err != nil {
213209
return err
214210
}
211+
212+
c.mu.Lock()
213+
defer c.mu.Unlock()
214+
c.interruptStreams = append(c.interruptStreams, stream)
215215
return nil
216216
}
217217

components/board/interrupt_stream.go

+6-26
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111

1212
type interruptStream struct {
1313
*client
14-
streamCancel context.CancelFunc
15-
streamRunning bool
16-
streamReady chan bool
17-
streamMu sync.Mutex
14+
streamCancel context.CancelFunc
15+
streamMu sync.Mutex
1816

1917
activeBackgroundWorkers sync.WaitGroup
2018
extra *structpb.Struct
@@ -28,9 +26,6 @@ func (s *interruptStream) startStream(ctx context.Context, interrupts []DigitalI
2826
return ctx.Err()
2927
}
3028

31-
s.streamRunning = true
32-
s.streamReady = make(chan bool)
33-
s.activeBackgroundWorkers.Add(1)
3429
ctx, cancel := context.WithCancel(ctx)
3530
s.streamCancel = cancel
3631

@@ -62,30 +57,15 @@ func (s *interruptStream) startStream(ctx context.Context, interrupts []DigitalI
6257
// Create a background go routine to receive from the server stream.
6358
// We rely on calling the Done function here rather than in close stream
6459
// since managed go calls that function when the routine exits.
60+
s.activeBackgroundWorkers.Add(1)
6561
utils.ManagedGo(func() {
66-
s.recieveFromStream(ctx, stream, ch)
62+
s.receiveFromStream(ctx, stream, ch)
6763
},
6864
s.activeBackgroundWorkers.Done)
69-
70-
select {
71-
case <-ctx.Done():
72-
return ctx.Err()
73-
case <-s.streamReady:
74-
return nil
75-
}
65+
return nil
7666
}
7767

78-
func (s *interruptStream) recieveFromStream(ctx context.Context, stream pb.BoardService_StreamTicksClient, ch chan Tick) {
79-
defer func() {
80-
s.streamMu.Lock()
81-
defer s.streamMu.Unlock()
82-
s.streamRunning = false
83-
}()
84-
// Close the stream ready channel so the above function returns.
85-
if s.streamReady != nil {
86-
close(s.streamReady)
87-
}
88-
s.streamReady = nil
68+
func (s *interruptStream) receiveFromStream(ctx context.Context, stream pb.BoardService_StreamTicksClient, ch chan Tick) {
8969
defer s.closeStream()
9070

9171
// repeatly receive from the stream

0 commit comments

Comments
 (0)