Skip to content

Commit 24c679f

Browse files
committed
ovsdb: handle closed callback channel corner case
1 parent 04e70e6 commit 24c679f

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

ovsdb/client.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,16 @@ func (c *Client) rpc(ctx context.Context, method string, out interface{}, args .
146146
// Await RPC completion or cancelation.
147147
select {
148148
case <-ctx.Done():
149-
// RPC canceled; clean up callback.
150-
return c.cancelCallback(ctx, req.ID)
151-
case res := <-ch:
149+
// RPC canceled. Producer cleans up the callback.
150+
return ctx.Err()
151+
case res, ok := <-ch:
152+
if !ok {
153+
// Channel was closed by producer after a context cancelation,
154+
// and woke up this consumer. The select statement happened
155+
// to pick this case even though the context was canceled.
156+
return ctx.Err()
157+
}
158+
152159
// RPC complete.
153160
return rpcResult(res, &r)
154161
}
@@ -232,18 +239,6 @@ func (c *Client) doCallback(id int, res rpcResponse) {
232239
delete(c.callbacks, id)
233240
}
234241

235-
// cancelCallback is invoked when an RPC is canceled by its context.
236-
func (c *Client) cancelCallback(ctx context.Context, id int) error {
237-
// RPC canceled; acquire the callback mutex and clean up the callback
238-
// for this RPC.
239-
c.cbMu.Lock()
240-
defer c.cbMu.Unlock()
241-
242-
delete(c.callbacks, id)
243-
244-
return ctx.Err()
245-
}
246-
247242
func panicf(format string, a ...interface{}) {
248243
panic(fmt.Sprintf(format, a...))
249244
}

0 commit comments

Comments
 (0)