[go] database/sql: Replace ctx.Done() with ctx.Err()

529 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Dec 14, 2020, 7:40:08 PM12/14/20
to goph...@pubsubhelper.golang.org, Ayan George, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

database/sql: Replace ctx.Done() with ctx.Err()

```
Prior to this commit, we used ctx.Done() to test for cancellation
without multiplexing over other channels like:

select {
default:
case <-ctx.Done():
// handle cancellation
}

context.Context.Done() lazily allocates the internal channel used and
there is some overhead to entering a select block.

This commit replaces lone ctx.Done() calls with ctx.Err() like below:

if err := ctx.Err(); err != nil {
// handle cancellation
}

This potentially avoids the channel allocation, avoids the overhead of
unnecessarily entering a select block, and saves a few lines and indent
levels. Still, it is probably more correct to use ctx.Err() when not
multiplexing over other channels.

benchstat output shows a measurable but negligible performance
improvement and no change to memory footprint.

name old time/op new time/op delta
ConcurrentDBExec-8 6.28ms ± 0% 6.27ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentStmtQuery-8 5.61ms ± 0% 5.52ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentStmtExec-8 4.29ms ± 0% 4.28ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxQuery-8 5.55ms ± 0% 5.57ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxExec-8 1.40ms ± 0% 1.39ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxStmtQuery-8 1.63ms ± 0% 1.63ms ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxStmtExec-8 454µs ± 0% 447µs ± 0% ~ (p=1.000 n=1+1)
ConcurrentRandom-8 4.66ms ± 0% 4.63ms ± 0% ~ (p=1.000 n=1+1)
ManyConcurrentQueries-8 10.4µs ± 0% 10.3µs ± 0% ~ (p=1.000 n=1+1)

name old alloc/op new alloc/op delta
ConcurrentDBExec-8 2.49MB ± 0% 2.50MB ± 0% ~ (p=1.000 n=1+1)
ConcurrentStmtQuery-8 2.25MB ± 0% 2.25MB ± 0% ~ (p=1.000 n=1+1)
ConcurrentStmtExec-8 1.71MB ± 0% 1.71MB ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxQuery-8 2.37MB ± 0% 2.44MB ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxExec-8 587kB ± 0% 587kB ± 0% ~ (all equal)
ConcurrentTxStmtQuery-8 745kB ± 0% 745kB ± 0% ~ (p=1.000 n=1+1)
ConcurrentTxStmtExec-8 108kB ± 0% 108kB ± 0% ~ (p=1.000 n=1+1)
ConcurrentRandom-8 1.63MB ± 0% 1.66MB ± 0% ~ (p=1.000 n=1+1)
ManyConcurrentQueries-8 3.76kB ± 0% 3.76kB ± 0% ~ (all equal)

name old allocs/op new allocs/op delta
ConcurrentDBExec-8 14.6k ± 0% 14.6k ± 0% ~ (all equal)
ConcurrentStmtQuery-8 17.1k ± 0% 17.1k ± 0% ~ (all equal)
ConcurrentStmtExec-8 3.17k ± 0% 3.17k ± 0% ~ (all equal)
ConcurrentTxQuery-8 19.6k ± 0% 19.6k ± 0% ~ (all equal)
ConcurrentTxExec-8 14.1k ± 0% 14.1k ± 0% ~ (all equal)
ConcurrentTxStmtQuery-8 19.2k ± 0% 19.2k ± 0% ~ (all equal)
ConcurrentTxStmtExec-8 2.17k ± 0% 2.17k ± 0% ~ (all equal)
ConcurrentRandom-8 13.9k ± 0% 13.9k ± 0% ~ (p=1.000 n=1+1)
ManyConcurrentQueries-8 17.0 ± 0% 17.0 ± 0% ~ (all equal)

```

This PR will be imported into Gerrit with the title and first
comment (this text) used to generate the subject and body of
the Gerrit change.

**Please ensure you adhere to every item in this list.**

More info can be found at https://github.com/golang/go/wiki/CommitMessage

+ The PR title is formatted as follows: `net/http: frob the quux before blarfing`
+ The package name goes before the colon
+ The part after the colon uses the verb tense + phrase that completes the blank in,
"This change modifies Go to ___________"
+ Lowercase verb after the colon
+ No trailing period
+ Keep the title as short as possible. ideally under 76 characters or shorter
+ No Markdown
+ The first PR comment (this one) is wrapped at 76 characters, unless it's
really needed (ASCII art, table, or long link)
+ If there is a corresponding issue, add either `Fixes #1234` or `Updates #1234`
(the latter if this is not a complete fix) to this comment
+ If referring to a repo other than `golang/go` you can use the
`owner/repo#issue_number` syntax: `Fixes golang/tools#1234`
+ We do not use Signed-off-by lines in Go. Please don't add them.
Our Gerrit server & GitHub bots enforce CLA compliance instead.
+ Delete these instructions once you have read and applied them

Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: 0adfad4fb70ae0b3467442b9f455c952291c447d
GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/sql.go
2 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/src/database/sql/ctxutil.go b/src/database/sql/ctxutil.go
index 4dbe6af..3ba740d 100644
--- a/src/database/sql/ctxutil.go
+++ b/src/database/sql/ctxutil.go
@@ -16,11 +16,9 @@
}
si, err := ci.Prepare(query)
if err == nil {
- select {
- default:
- case <-ctx.Done():
+ if err := ctx.Err(); err != nil {
si.Close()
- return nil, ctx.Err()
+ return nil, err
}
}
return si, err
@@ -35,11 +33,10 @@
return nil, err
}

- select {
- default:
- case <-ctx.Done():
- return nil, ctx.Err()
+ if err := ctx.Err(); err != nil {
+ return nil, err
}
+
return execer.Exec(query, dargs)
}

@@ -52,11 +49,10 @@
return nil, err
}

- select {
- default:
- case <-ctx.Done():
- return nil, ctx.Err()
+ if err := ctx.Err(); err != nil {
+ return nil, err
}
+
return queryer.Query(query, dargs)
}

@@ -69,11 +65,10 @@
return nil, err
}

- select {
- default:
- case <-ctx.Done():
- return nil, ctx.Err()
+ if err := ctx.Err(); err != nil {
+ return nil, err
}
+
return si.Exec(dargs)
}

@@ -86,11 +81,10 @@
return nil, err
}

- select {
- default:
- case <-ctx.Done():
- return nil, ctx.Err()
+ if err := ctx.Err(); err != nil {
+ return nil, err
}
+
return si.Query(dargs)
}

@@ -123,14 +117,14 @@
}

txi, err := ci.Begin()
+
if err == nil {
- select {
- default:
- case <-ctx.Done():
+ if err := ctx.Err(); err != nil {
txi.Rollback()
- return nil, ctx.Err()
+ return nil, err
}
}
+
return txi, err
}

diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go
index 726aadb..158ceb4 100644
--- a/src/database/sql/sql.go
+++ b/src/database/sql/sql.go
@@ -1200,9 +1200,7 @@
return nil, errDBClosed
}
// Check if the context is expired.
- select {
- default:
- case <-ctx.Done():
+ if err := ctx.Err(); err != nil {
db.mu.Unlock()
return nil, ctx.Err()
}
@@ -2101,9 +2099,7 @@
var hookTxGrabConn func()

func (tx *Tx) grabConn(ctx context.Context) (*driverConn, releaseConn, error) {
- select {
- default:
- case <-ctx.Done():
+ if err := ctx.Err(); err != nil {
return nil, nil, ctx.Err()
}


To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Ayan George <ay...@ayan.net>
Gerrit-MessageType: newchange

Gerrit Bot (Gerrit)

unread,
Dec 14, 2020, 7:42:21 PM12/14/20
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot uploaded patch set #2 to this change.

View Change

GitHub-Last-Rev: dbde1d8753965b60dd0c72af19eb909cb6ba8bef

GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/sql.go
2 files changed, 20 insertions(+), 30 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Ayan George <ay...@ayan.net>
Gerrit-MessageType: newpatchset

Ian Lance Taylor (Gerrit)

unread,
Dec 14, 2020, 9:22:53 PM12/14/20
to Gerrit Bot, Ayan George, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Daniel Theophanes, Kevin Burke, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick.

View Change

4 comments:

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Ayan George <ay...@ayan.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Tue, 15 Dec 2020 02:22:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Dec 14, 2020, 9:30:54 PM12/14/20
to Ayan George, Daniel Theophanes, Brad Fitzpatrick, goph...@pubsubhelper.golang.org, Kevin Burke, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #3 to this change.

View Change

database/sql: replace ctx.Done() with ctx.Err()

Prior to this commit, we used a select block with a default label and a read from
ctx.Done() to test for cancellation without blocking like:
Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: dbde1d8753965b60dd0c72af19eb909cb6ba8bef
GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/sql.go
2 files changed, 20 insertions(+), 30 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 3
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Ayan George <ay...@ayan.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-MessageType: newpatchset

Gerrit Bot (Gerrit)

unread,
Feb 13, 2021, 11:03:54 AM2/13/21
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #4 to this change.

View Change

GitHub-Last-Rev: 4961ecd979a55923037bc1ad14d59ebb977e62c7

GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/fakedb_test.go
M src/database/sql/sql.go
M src/database/sql/sql_test.go
M src/net/dial.go
M src/net/fd_unix.go
M src/net/fd_windows.go
M src/net/http/serve_test.go
M src/net/http/transport.go
M src/net/lookup.go
M src/os/exec/exec.go
11 files changed, 54 insertions(+), 87 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 4

Gerrit Bot (Gerrit)

unread,
Feb 13, 2021, 11:09:05 AM2/13/21
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #5 to this change.

View Change

database/sql,net,net/http,os/exec: Replace ctx.Done() with ctx.Err()

Prior to this commit, we performed non-blocking tests for cancellation
by receiving from ctx.Done() in a select block with a default label
like:

select {

case <-ctx.Done():
// handle cancellation
    default:
}

Unless we're multiplexing over multiple channels, a better way to test
if a context is canceled is to test the result of ctx.Err()

This commit replaces all of the select{} based non-blocking cancellation
checks with a call to ctx.Err().

The src/context BenchmarkCheckCanceled benchmark demonstrates that
calling ctx.Err() is faster and, in general, it is probably clearer to
simply check ctx.Err() than using a select block.


Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: 4961ecd979a55923037bc1ad14d59ebb977e62c7
GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/fakedb_test.go
M src/database/sql/sql.go
M src/database/sql/sql_test.go
M src/net/dial.go
M src/net/fd_unix.go
M src/net/fd_windows.go
M src/net/http/serve_test.go
M src/net/http/transport.go
M src/net/lookup.go
M src/os/exec/exec.go
11 files changed, 54 insertions(+), 87 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 5

Gerrit Bot (Gerrit)

unread,
Feb 25, 2021, 9:54:58 AM2/25/21
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #6 to this change.

View Change

database/sql,net,net/http,os/exec: Replace ctx.Done() with ctx.Err()

Prior to this commit, we performed non-blocking tests for cancellation
by receiving from ctx.Done() in a select block with a default label
like:

select {
case <-ctx.Done():
// handle cancellation
default:
}

Unless we're multiplexing over multiple channels, a better way to test
if a context is canceled is to test the result of ctx.Err()

This commit replaces all of the select{} based non-blocking cancellation
checks with a call to ctx.Err().

The src/context BenchmarkCheckCanceled benchmark demonstrates that
calling ctx.Err() is faster and, in general, it is probably clearer to
simply check ctx.Err() than using a select block.

Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: ba2fe38565545e7f9ee964b5cd99fea6d59e6a7b

GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/fakedb_test.go
M src/database/sql/sql.go
M src/database/sql/sql_test.go
M src/net/dial.go
M src/net/fd_unix.go
M src/net/fd_windows.go
M src/net/http/serve_test.go
M src/net/http/transport.go
M src/net/lookup.go
M src/os/exec/exec.go
11 files changed, 59 insertions(+), 93 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 6

Ayan George (Gerrit)

unread,
Mar 25, 2021, 11:43:08 AM3/25/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Daniel Theophanes, Kevin Burke, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick, Ian Lance Taylor.

Patch set 6:Code-Review +1

View Change

1 comment:

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Ayan George <ay...@ayan.net>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Thu, 25 Mar 2021 15:43:01 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Emmanuel Odeke (Gerrit)

unread,
Mar 26, 2021, 7:43:01 PM3/26/21
to Gerrit Bot, Ayan George, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Daniel Theophanes, Kevin Burke, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick, Ian Lance Taylor.

Emmanuel Odeke removed a vote from this change.

View Change

Removed Code-Review+1 by Ayan George <ay...@ayan.net>

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Ayan George <ay...@ayan.net>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: deleteVote

Emmanuel Odeke (Gerrit)

unread,
Mar 26, 2021, 7:44:32 PM3/26/21
to Gerrit Bot, Ayan George, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Daniel Theophanes, Kevin Burke, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick, Ian Lance Taylor.

View Change

12 comments:


    • This commit replaces all of the select{} based non-blocking cancellation
      checks with a call to ctx.Err().

    • This change replaces, where necessary, the select{} based
      non-blocking waits with a call to context.Err()

    • Patch Set #6, Line 25: The src/context BenchmarkCheckCanceled benchmark

      context.BenchmarkCheckCanceled demonstrates

  • Patchset:

    • Patch Set #6:

      Thank you for this change Ayan, and great to see you contributing in 2021 too :-)

      I've added some feedback to your change. Please rebase with the latest code from
      the master branch, and please address the feedback below. When you are ready I shall
      stamp it. Also, please don't +1 your own CLs :-)

      Thank you.

  • File src/database/sql/ctxutil.go:


    • if err := ctx.Err(); err != nil {

    • 			txi.Rollback()
      return nil, err
      }
      }

      return txi, err

      Perhaps we could reverse conditional this and unnest it:

        if err != nil {
      return txi, err
      }
        err = ctx.Err()
      if err == nil {
      return txi, nil
      }
        // Otherwise rollback the transaction and discard it.
      txi.Rollback()
      return nil, err
  • File src/database/sql/sql.go:

  • File src/database/sql/sql_test.go:

    • Patch Set #6, Line 341:

      switch err := ctx.Err(); err {
      case nil:
      t.Fatalf("context err = nil; want context.Canceled")
      default:
      t.Fatalf("context err = %v; want context.Canceled", err)
      case context.Canceled:
      // expected context.Canceled
      }

      We can simplify this to:

       if err := ctx.Err(); err != context.Canceled {
      t.Fatalf("context err = %v; want context.Canceled", err)
      }
    • Patch Set #6, Line 2964: t.Fatal("timeout: failed to rollback query without closing rows:", err)

      t.Fatalf("timeout: failed to rollback query without closing rows: %v", err)

  • File src/net/http/serve_test.go:

    • Patch Set #6, Line 4797:

      		select {
      case <-ctx.Done():
      t.Error("should not be Done in ServeHTTP")

      Please revert this, intentionally a select.

    • Patch Set #6, Line 5853:

      select {
      case <-r.Context().Done():
      t.Error("context unexpectedly canceled")
      default:

      If we got this route and just replace singly, we've got to check for:
      a) context.Canceled
      b) err != nil, could be deadline exceeded and if so we need to figure out why
      c) allow err == nil

      and at that point it becomes a tedious check. Perhaps you can do this

         if err := r.Context().Err(); err != nil {
      t.Errorf("context unexpectedly expired: %v", err)
      }
    • Patch Set #6, Line 4809: if ctx.Err() != nil {

      This is an incorrect translation, it should be that if ctx.Err() == nil,
      that we have the failure. I'd say let's just revert and leave it as it was
      otherwise if you retrieve an error we need to check for its value.

  • File src/net/http/transport.go:


    • if err := ctx.Err(); err != nil {
    •       req.closeBody()
      return nil, err
      }

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Ayan George <ay...@ayan.net>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Fri, 26 Mar 2021 23:44:24 +0000

Gerrit Bot (Gerrit)

unread,
Mar 26, 2021, 8:52:03 PM3/26/21
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick, Ian Lance Taylor.

Gerrit Bot uploaded patch set #7 to this change.

View Change

database/sql,net,net/http,os/exec: Replace ctx.Done() with ctx.Err()

Prior to this commit, we performed non-blocking tests for cancellation
by receiving from ctx.Done() in a select block with a default label
like:

select {
case <-ctx.Done():
// handle cancellation
default:
}

Unless we're multiplexing over multiple channels, a better way to test
if a context is canceled is to test the result of ctx.Err()

This commit replaces all of the select{} based non-blocking cancellation
checks with a call to ctx.Err().

The src/context BenchmarkCheckCanceled benchmark demonstrates that
calling ctx.Err() is faster and, in general, it is probably clearer to
simply check ctx.Err() than using a select block.

Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: 701e35f7abaa031ef66c7b0ba6a138c85a54794f

GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/fakedb_test.go
M src/database/sql/sql.go
M src/database/sql/sql_test.go
M src/net/dial.go
M src/net/fd_unix.go
M src/net/fd_windows.go
M src/net/http/serve_test.go
M src/net/http/transport.go
M src/net/lookup.go
M src/os/exec/exec.go
11 files changed, 56 insertions(+), 91 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 7
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Ayan George <ay...@ayan.net>
Gerrit-CC: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Kevin Burke <k...@inburke.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: newpatchset

Gerrit Bot (Gerrit)

unread,
Mar 26, 2021, 9:09:29 PM3/26/21
to Ayan George, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes, Brad Fitzpatrick, Ian Lance Taylor.

Gerrit Bot uploaded patch set #8 to this change.

View Change

database/sql,net,net/http,os/exec: Replace ctx.Done() with ctx.Err()

Prior to this commit, we performed non-blocking tests for cancellation
by receiving from ctx.Done() in a select block with a default label
like:

select {
case <-ctx.Done():
// handle cancellation
default:
}

Unless we're multiplexing over multiple channels, a better way to test
if a context is canceled is to test the result of ctx.Err()

This commit replaces all of the select{} based non-blocking cancellation
checks with a call to ctx.Err().

The src/context BenchmarkCheckCanceled benchmark demonstrates that
calling ctx.Err() is faster and, in general, it is probably clearer to
simply check ctx.Err() than using a select block.

Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
GitHub-Last-Rev: d6f2b3e38ccb48e3649ece72556522b1e26715e4

GitHub-Pull-Request: golang/go#43189
---
M src/database/sql/ctxutil.go
M src/database/sql/fakedb_test.go
M src/database/sql/sql.go
M src/database/sql/sql_test.go
M src/net/dial.go
M src/net/fd_unix.go
M src/net/fd_windows.go
M src/net/http/serve_test.go
M src/net/http/transport.go
M src/net/lookup.go
M src/os/exec/exec.go
11 files changed, 56 insertions(+), 91 deletions(-)

To view, visit change 278112. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1de2fdca200afd81e3bd1c9ade8ea02e19324ae5
Gerrit-Change-Number: 278112
Gerrit-PatchSet: 8
Reply all
Reply to author
Forward
0 new messages