From 812fb95273781f4b030b6dd854f9ac542b39e750 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Fri, 1 Dec 2023 10:23:29 +0400 Subject: [PATCH] fix: prevent connIO from panicking in race between Close and Enqueue (#10948) Spotted during a code read. ConnIO unlocks the mutex before attempting to write to the response channel, which could allow another goroutine to call Close() and close the channel, causing a panic. Fix is to hold the mutex. This won't cause a deadlock because the `select{}` has a `default` case, so we won't block even if the receiver isn't keeping up. --- enterprise/tailnet/connio.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/enterprise/tailnet/connio.go b/enterprise/tailnet/connio.go index 9454279adc..83c1d8a2b9 100644 --- a/enterprise/tailnet/connio.go +++ b/enterprise/tailnet/connio.go @@ -197,9 +197,8 @@ func (c *connIO) UniqueID() uuid.UUID { func (c *connIO) Enqueue(resp *proto.CoordinateResponse) error { atomic.StoreInt64(&c.lastWrite, time.Now().Unix()) c.mu.Lock() - closed := c.closed - c.mu.Unlock() - if closed { + defer c.mu.Unlock() + if c.closed { return xerrors.New("connIO closed") } select {