Skip to content

Commit

Permalink
dnsproxy: shared_client: fix fail-safe mechanism
Browse files Browse the repository at this point in the history
If a shared client exchange fell into the fail-safe timeout of one
minute, but the handler loop (due to either an error, closing or a
_very_ delayed response) would write to the now reader-less channel, it
would block all future progress of this shared client. Prevent that from
happening by buffering the channel for the one message it will receive.

The corresponding, backportable change cilium/dns is cilium/dns#15.

Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
  • Loading branch information
bimmlerd authored and julianwiedmann committed Oct 29, 2024
1 parent 911ed9f commit 392821c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/fqdn/dnsproxy/shared_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ func (c *SharedClient) ExchangeSharedContext(ctx context.Context, m *dns.Msg) (r
timeout := getTimeoutForRequest(c.Client)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
respCh := make(chan sharedClientResponse)
// The handler loop writes our response or an error to this channel. If we fall into the timeout
// below, the handling loop would block indefinitely on writing if this channel is not buffered.
respCh := make(chan sharedClientResponse, 1)
select {
case c.requests <- request{ctx: ctx, msg: m, ch: respCh}:
case <-ctx.Done():
Expand All @@ -374,7 +376,8 @@ func (c *SharedClient) ExchangeSharedContext(ctx context.Context, m *dns.Msg) (r
select {
case resp := <-respCh:
return resp.msg, resp.rtt, resp.err
// This is just fail-safe mechanism in case there is another similar issue
// This is a fail-safe mechanism which prevents leaking this goroutine if the handling loop
// fails to write a response on our channel.
case <-time.After(time.Minute):
return nil, 0, fmt.Errorf("timeout waiting for response")
}
Expand Down

0 comments on commit 392821c

Please sign in to comment.