os, net: define and use os.ErrDeadlineExceeded

If an I/O operation fails because a deadline was exceeded,
return os.ErrDeadlineExceeded. We used to return poll.ErrTimeout,
an internal error, and told users to check the Timeout method.
However, there are other errors with a Timeout method that returns true,
notably syscall.ETIMEDOUT which is returned for a keep-alive timeout.
Checking errors.Is(err, os.ErrDeadlineExceeded) should permit code
to reliably tell why it failed.

This change does not affect the handling of net.Dialer.Deadline,
nor does it change the handling of net.DialContext when the context
deadline is exceeded. Those cases continue to return an error
reported as "i/o timeout" for which Timeout is true, but that error
is not os.ErrDeadlineExceeded.

Fixes #31449

Change-Id: I0323f42e944324c6f2578f00c3ac90c24fe81177
Reviewed-on: https://go-review.googlesource.com/c/go/+/228645
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
Ian Lance Taylor 2020-04-17 15:42:12 -07:00
parent 396833caef
commit d422f54619
20 changed files with 196 additions and 108 deletions

View file

@ -10,7 +10,6 @@ import (
"context"
"errors"
"fmt"
"internal/poll"
"io/ioutil"
"os"
"path"
@ -480,7 +479,7 @@ func TestGoLookupIPWithResolverConfig(t *testing.T) {
break
default:
time.Sleep(10 * time.Millisecond)
return dnsmessage.Message{}, poll.ErrTimeout
return dnsmessage.Message{}, os.ErrDeadlineExceeded
}
r := dnsmessage.Message{
Header: dnsmessage.Header{
@ -993,7 +992,7 @@ func TestRetryTimeout(t *testing.T) {
if s == "192.0.2.1:53" {
deadline0 = deadline
time.Sleep(10 * time.Millisecond)
return dnsmessage.Message{}, poll.ErrTimeout
return dnsmessage.Message{}, os.ErrDeadlineExceeded
}
if deadline.Equal(deadline0) {
@ -1131,7 +1130,7 @@ func TestStrictErrorsLookupIP(t *testing.T) {
}
makeTimeout := func() error {
return &DNSError{
Err: poll.ErrTimeout.Error(),
Err: os.ErrDeadlineExceeded.Error(),
Name: name,
Server: server,
IsTimeout: true,
@ -1247,7 +1246,7 @@ func TestStrictErrorsLookupIP(t *testing.T) {
Questions: q.Questions,
}, nil
case resolveTimeout:
return dnsmessage.Message{}, poll.ErrTimeout
return dnsmessage.Message{}, os.ErrDeadlineExceeded
default:
t.Fatal("Impossible resolveWhich")
}
@ -1372,7 +1371,7 @@ func TestStrictErrorsLookupTXT(t *testing.T) {
switch q.Questions[0].Name.String() {
case searchX:
return dnsmessage.Message{}, poll.ErrTimeout
return dnsmessage.Message{}, os.ErrDeadlineExceeded
case searchY:
return mockTXTResponse(q), nil
default:
@ -1387,7 +1386,7 @@ func TestStrictErrorsLookupTXT(t *testing.T) {
var wantRRs int
if strict {
wantErr = &DNSError{
Err: poll.ErrTimeout.Error(),
Err: os.ErrDeadlineExceeded.Error(),
Name: name,
Server: server,
IsTimeout: true,
@ -1415,7 +1414,7 @@ func TestDNSGoroutineRace(t *testing.T) {
fake := fakeDNSServer{rh: func(n, s string, q dnsmessage.Message, t time.Time) (dnsmessage.Message, error) {
time.Sleep(10 * time.Microsecond)
return dnsmessage.Message{}, poll.ErrTimeout
return dnsmessage.Message{}, os.ErrDeadlineExceeded
}}
r := Resolver{PreferGo: true, Dial: fake.DialContext}