How to check if a request was cancelled

839 views
Skip to first unread message

Glen Huang

unread,
Nov 8, 2017, 7:02:13 AM11/8/17
to golang-nuts
I have this simple code in which I try to check if the request was cancelled. But surprisingly, it prints false instead of true in go 1.9.

I wonder what's the correct way to check that?

package main


import (
   
"context"
   
"log"
   
"net/http"
)


func main
() {
    r
, _ := http.NewRequest("GET", "http://example.com", nil)
    ctx
:= context.Background()
    ctx
, cancel := context.WithCancel(ctx)
    r
= r.WithContext(ctx)
    ch
:= make(chan bool)
    go func
() {
        _
, err := http.DefaultClient.Do(r)
        log
.Println(err == context.Canceled)
        ch
<- true
   
}()
    cancel
()
   
<-ch
}

Ain

unread,
Nov 8, 2017, 7:07:51 AM11/8/17
to golang-nuts

I noticed that OP has also posted this question to SO:


ain

Glen Huang

unread,
Nov 8, 2017, 7:11:54 AM11/8/17
to golang-nuts
Yes. Thanks for point it out, and maybe I should've mentioned that.

After posing that on SO, I have a gut feeling that it should be the intuitive way to check that. I was hoping a go author could see the question here and maybe offer some rationale why that's not the case.

Sorry if it's considered spamming.

Jakob Borg

unread,
Nov 8, 2017, 7:17:59 AM11/8/17
to Glen Huang, golang-nuts
Errors from net/http are wrapped in an url.Error to contain information about the request that failed. You can unwrap it with an assertion or type switch;

_, err := http.DefaultClient.Do(r)
if urlErr, ok := err.(*url.Error); ok {
log.Println(urlErr.Err == context.Canceled) // “true", in your example
}

//jb


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Mercl

unread,
Nov 8, 2017, 7:20:25 AM11/8/17
to Glen Huang, golang-nuts
On Wed, Nov 8, 2017 at 1:12 PM Glen Huang <hey...@gmail.com> wrote:


--

-j

Glen Huang

unread,
Nov 8, 2017, 7:20:50 AM11/8/17
to golang-nuts
Really neat! Didn't know it was wrapped. Thank you.

Glen Huang

unread,
Nov 8, 2017, 7:28:01 AM11/8/17
to golang-nuts
Yep, that's a good way too. Thanks. Ain also posted it on SO.

Jakob Borg

unread,
Nov 8, 2017, 7:40:39 AM11/8/17
to Jan Mercl, Glen Huang, golang-nuts
(expanding the code from the link)

log.Println(err, ctx.Err() == context.Canceled)

Note that, to be pedantic, this only tells you that the context has been cancelled - not that that was the error returned by the HTTP request. The HTTP request may have succeeded, or failed for another reason, before the context was cancelled. Whether this matters is of course up to the application.

//jb


Glen Huang

unread,
Nov 8, 2017, 7:44:00 AM11/8/17
to Jakob Borg, Jan Mercl, Glen Huang, golang-nuts
In my case the client is the sole consumer of the context, so it should be fine.

But I agree, checking error directly is more bullet-proof. I should probably form a habit adopting it.
Reply all
Reply to author
Forward
0 new messages