From cc506bcaa66e2baefc49e98a14ba8ccdf128395a Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Mon, 13 Nov 2023 09:51:02 +0100 Subject: [PATCH 1/2] Fix closing order --- xfr.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/xfr.go b/xfr.go index 0a831c880..63608b2de 100644 --- a/xfr.go +++ b/xfr.go @@ -80,8 +80,10 @@ func (t *Transfer) In(q *Msg, a string) (env chan *Envelope, err error) { func (t *Transfer) inAxfr(q *Msg, c chan *Envelope) { first := true - defer t.Close() - defer close(c) + defer func() { + t.Close() + close(c) + }() timeout := dnsTimeout if t.ReadTimeout != 0 { timeout = t.ReadTimeout @@ -131,8 +133,10 @@ func (t *Transfer) inIxfr(q *Msg, c chan *Envelope) { axfr := true n := 0 qser := q.Ns[0].(*SOA).Serial - defer t.Close() - defer close(c) + defer func() { + t.Close() + close(c) + }() timeout := dnsTimeout if t.ReadTimeout != 0 { timeout = t.ReadTimeout From 252c106d00d1eb918514e51af3c10c230dfd879b Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Mon, 13 Nov 2023 15:22:03 +0100 Subject: [PATCH 2/2] Comment to make clear that the close order is deliberate --- xfr.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xfr.go b/xfr.go index 63608b2de..05b3c5add 100644 --- a/xfr.go +++ b/xfr.go @@ -81,6 +81,9 @@ func (t *Transfer) In(q *Msg, a string) (env chan *Envelope, err error) { func (t *Transfer) inAxfr(q *Msg, c chan *Envelope) { first := true defer func() { + // First close the connection, then the channel. This allows functions blocked on + // the channel to assume that the connection is closed and no further operations are + // pending when they resume. t.Close() close(c) }() @@ -134,6 +137,9 @@ func (t *Transfer) inIxfr(q *Msg, c chan *Envelope) { n := 0 qser := q.Ns[0].(*SOA).Serial defer func() { + // First close the connection, then the channel. This allows functions blocked on + // the channel to assume that the connection is closed and no further operations are + // pending when they resume. t.Close() close(c) }()