Handling Context Cancelled errors
Asked Answered
B

1

20

I am new to Go and still learning it every signle day. I see a lot of context cancelled error in our project which is kind of annoying me. So i was thinking to dig a little deeper to figure out whats going on.

If I am not wrong, in "simple" terms, context cancelled means that the given request (via grpc using gokit) has been time out. Correct? If so what should be best way to fix this issue?

  1. Should I look into why these requests are being timed out (probably underlying db queries are taking long or something) and fix those?

  2. Is it something Go internal which may be hinting about something related to Go?

  3. What will be the best approach to start handling this error? Right now, all I see "Context cancelled" and have no idea why so.

Benavides answered 15/6, 2020 at 3:51 Comment(1)
"context cancelled means that the given request has timed out, correct?" Nope. Timeouts produce context.DeadlineExceeded. Cancelled means one of the CancelFuncs has been called, nothing more nothing less.Amathiste
C
22

A context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.

Incoming requests to a server should create a Context, and outgoing calls to servers should accept a Context.

A context.Canceled error does not necessarily mean a timeout error.

Scenario 1:

If you are using go routines, if the parent go routine finishes but child routine still runs in the background, and the child go routine had a context which is common to the parent go routine this can end up in a context cancelled, if the parent go routine cancels the context before exiting.

So if the child go routine does not have dependency on the parent's context, it is always a good practice to create a new context for background go routines. A new context can be created using the context.Background()

Scenario 2:

If your application is microservice based (or have several components which call each other using contexts), when microservice 1 calls microservice 2, and microservice 2 explicitly cancels the context, even in that case you can get this error.

A context can be canceled by calling the cancel() function as shown below:

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

Cancelling this context releases resources associated with it. So if the callee has explicitly cancelled the context as shown above, this can result in a context.Canceled error at caller.

These context cancelled errors can be handled checking the context.Canceled error from a grpc.Dial() call (if you are using grpc).

Contrapuntist answered 15/6, 2020 at 5:6 Comment(3)
This explanation isn't very clear.Smog
How to check if it was cancelled? ctx.Err() == context.Canceled does not produce true when the context was cancelled.Kershner
if error.Code() == request.CanceledErrorCode should be the way to check for context cancelling checkMyosin

© 2022 - 2025 — McMap. All rights reserved.