mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 11:07:10 +00:00
fix(agent): race in readWithDeadline clobbers conn deadline (#7)
* refactor(docs): inline pandoc build into Makefile, drop scripts dir * fix(agent): wait for deadline watcher before returning readWithDeadline's watcher goroutine could clobber the conn's read deadline with time.Unix(1, 0) after the main function reset it to zero. When the Accept ctx was canceled shortly after Accept returned, the watcher raced with close(done) in select and sometimes picked ctx.Done() even though we were already done reading, leaving the conn unusable for the next read (instant i/o timeout on step 1 snapshot). Synchronize on the watcher's exit before resetting the deadline so it can never override the reset. * test(agent): cover readWithDeadline race on Accept ctx cancel Drives Accept with a short-timeout ctx, cancels it right after Accept returns, then does a Snapshot. Reliably fails without the readWithDeadline synchronization fix (watcher goroutine overwrites the deadline to past).
This commit is contained in:
2 files changed
+52
-2
No files matched your search
@@ -92,11 +92,11 @@ func (c *Conn) Close() error {
|
||||
func readWithDeadline(ctx context.Context, conn net.Conn) (Message, error) {
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
_ = conn.SetReadDeadline(deadline)
|
||||
defer conn.SetReadDeadline(time.Time{})
|
||||
}
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
exited := make(chan struct{})
|
||||
go func() {
|
||||
defer close(exited)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = conn.SetReadDeadline(time.Unix(1, 0))
|
||||
@@ -104,6 +104,9 @@ func readWithDeadline(ctx context.Context, conn net.Conn) (Message, error) {
|
||||
}
|
||||
}()
|
||||
message, err := ReadMessage(conn)
|
||||
close(done)
|
||||
<-exited
|
||||
_ = conn.SetReadDeadline(time.Time{})
|
||||
if err != nil && ctx.Err() != nil {
|
||||
return Message{}, ctx.Err()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user