- Notifications
You must be signed in to change notification settings - Fork329
Fix net.Conn deadlines tearing down the connection when no reads or writes are active#350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
…rites are activeWe were unconditionally canceling the read/write contexts when thedeadline timer fired, even if there were no active reads or writes.We instead only cancel the context if there is an active operation,otherwise we set a flag so that future calls (without the deadline beingreset) will fail.Updatestailscale/tailscale#5921
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,7 +6,9 @@ import ( | ||
"io" | ||
"math" | ||
"net" | ||
"os" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
@@ -43,15 +45,26 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { | ||
msgType: msgType, | ||
} | ||
var writeCancel context.CancelFunc | ||
nc.writeContext, writeCancel = context.WithCancel(ctx) | ||
nc.writeTimer = time.AfterFunc(math.MaxInt64, func() { | ||
nc.afterWriteDeadline.Store(true) | ||
if nc.writing.Load() { | ||
writeCancel() | ||
} | ||
}) | ||
if !nc.writeTimer.Stop() { | ||
<-nc.writeTimer.C | ||
} | ||
var readCancel context.CancelFunc | ||
nc.readContext, readCancel = context.WithCancel(ctx) | ||
nc.readTimer = time.AfterFunc(math.MaxInt64, func() { | ||
nc.afterReadDeadline.Store(true) | ||
if nc.reading.Load() { | ||
readCancel() | ||
} | ||
}) | ||
if !nc.readTimer.Stop() { | ||
<-nc.readTimer.C | ||
} | ||
@@ -63,11 +76,15 @@ type netConn struct { | ||
c *Conn | ||
msgType MessageType | ||
writeTimer *time.Timer | ||
writeContext context.Context | ||
writing atomic.Bool | ||
afterWriteDeadline atomic.Bool | ||
readTimer *time.Timer | ||
readContext context.Context | ||
reading atomic.Bool | ||
afterReadDeadline atomic.Bool | ||
readMu sync.Mutex | ||
eofed bool | ||
@@ -81,16 +98,34 @@ func (c *netConn) Close() error { | ||
} | ||
func (c *netConn) Write(p []byte) (int, error) { | ||
if c.afterWriteDeadline.Load() { | ||
return 0, os.ErrDeadlineExceeded | ||
} | ||
if swapped := c.writing.CompareAndSwap(false, true); !swapped { | ||
panic("Concurrent writes not allowed") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. concurrent writes should be allowed | ||
} | ||
defer c.writing.Store(false) | ||
err := c.c.Write(c.writeContext, c.msgType, p) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return len(p), nil | ||
} | ||
func (c *netConn) Read(p []byte) (int, error) { | ||
if c.afterReadDeadline.Load() { | ||
return 0, os.ErrDeadlineExceeded | ||
} | ||
c.readMu.Lock() | ||
defer c.readMu.Unlock() | ||
if swapped := c.reading.CompareAndSwap(false, true); !swapped { | ||
panic("Concurrent reads not allowed") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. concurrent reads should be allowed | ||
} | ||
defer c.reading.Store(false) | ||
if c.eofed { | ||
return 0, io.EOF | ||
@@ -151,16 +186,18 @@ func (c *netConn) SetWriteDeadline(t time.Time) error { | ||
if t.IsZero() { | ||
c.writeTimer.Stop() | ||
} else { | ||
c.writeTimer.Reset(time.Until(t)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. nice, didn't realize we had | ||
} | ||
c.afterWriteDeadline.Store(false) | ||
return nil | ||
} | ||
func (c *netConn) SetReadDeadline(t time.Time) error { | ||
if t.IsZero() { | ||
c.readTimer.Stop() | ||
} else { | ||
c.readTimer.Reset(time.Until(t)) | ||
} | ||
c.afterReadDeadline.Store(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. while highly unlikely this can race with the timer routine as it's possible the timer has already set off and the timer goroutine has already set | ||
return nil | ||
} |