Skip to content

Commit 4075ef0

Browse files
authored
xds: fix panic involving double close of channel in xDS transport (#5959)
1 parent 7bf6a58 commit 4075ef0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

xds/internal/xdsclient/transport/loadreport.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func (t *Transport) lrsStartStream() {
6262

6363
ctx, cancel := context.WithCancel(context.Background())
6464
t.lrsCancelStream = cancel
65+
66+
// Create a new done channel everytime a new stream is created. This ensures
67+
// that we don't close the same channel multiple times (from lrsRunner()
68+
// goroutine) when multiple streams are created and closed.
69+
t.lrsRunnerDoneCh = make(chan struct{})
6570
go t.lrsRunner(ctx)
6671
}
6772

@@ -78,6 +83,9 @@ func (t *Transport) lrsStopStream() {
7883

7984
t.lrsCancelStream()
8085
t.logger.Infof("Stopping LRS stream")
86+
87+
// Wait for the runner goroutine to exit. The done channel will be
88+
// recreated when a new stream is created.
8189
<-t.lrsRunnerDoneCh
8290
}
8391

xds/internal/xdsclient/transport/loadreport_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s) TestReportLoad(t *testing.T) {
5454
NodeProto: nodeProto,
5555
}
5656

57-
// Create a transport to the fake server.
57+
// Create a transport to the fake management server.
5858
tr, err := transport.New(transport.Options{
5959
ServerCfg: serverCfg,
6060
UpdateHandler: func(transport.ResourceUpdate) error { return nil }, // No ADS validation.
@@ -190,4 +190,16 @@ func (s) TestReportLoad(t *testing.T) {
190190
if _, err := mgmtServer.LRSStreamCloseChan.Receive(ctx); err != nil {
191191
t.Fatal("Timeout waiting for LRS stream to close")
192192
}
193+
194+
// Calling the load reporting API again should result in the creation of a
195+
// new LRS stream. This ensures that creating and closing multiple streams
196+
// works smoothly.
197+
_, cancelLRS3 := tr.ReportLoad()
198+
if err != nil {
199+
t.Fatalf("Failed to start LRS load reporting: %v", err)
200+
}
201+
if _, err := mgmtServer.LRSStreamOpenChan.Receive(ctx); err != nil {
202+
t.Fatalf("Timeout when waiting for LRS stream to be created: %v", err)
203+
}
204+
cancelLRS3()
193205
}

xds/internal/xdsclient/transport/transport.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ func New(opts Options) (*Transport, error) {
202202
versions: make(map[string]string),
203203
nonces: make(map[string]string),
204204
adsRunnerDoneCh: make(chan struct{}),
205-
lrsRunnerDoneCh: make(chan struct{}),
206205
}
207206

208207
// This context is used for sending and receiving RPC requests and

0 commit comments

Comments
 (0)