Context cancellation and returned error

174 views
Skip to first unread message

Pierre Durand

unread,
Apr 3, 2026, 1:42:31 PM (7 days ago) Apr 3
to golang-nuts
My code has a select with a case that waits for context cancellation.

Currently I'm doing this:
case <-ctx.Done():
return ctx.Err()

I noticed that some libraries and the stdlib are doing this:
case <-ctx.Done():
return context.Cause(ctx)

What is the best practice ?

Axel Wagner

unread,
Apr 4, 2026, 12:17:24 AM (7 days ago) Apr 4
to Pierre Durand, golang-nuts
The latter. ctx.Err() can only return the context-specific errors, while context.Cause can also return the error passed using WithCancelCause.

--
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.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/9adc5736-37da-4823-abc8-78f202171a15n%40googlegroups.com.

Pierre Durand

unread,
Apr 4, 2026, 5:02:01 PM (6 days ago) Apr 4
to golang-nuts
Thank you for your answer.
Yes I understand the difference between the 2 functions.

Can we say it's a best practice to use return context.Cause(ctx) instead of return ctx.Err() ?
Is there a reason to use return ctx.Err() instead of return context.Cause(ctx) ?

Axel Wagner

unread,
Apr 5, 2026, 1:24:21 AM (6 days ago) Apr 5
to Pierre Durand, golang-nuts
On Sat, 4 Apr 2026 at 23:02, Pierre Durand <pierre...@gmail.com> wrote:
Thank you for your answer.
Yes I understand the difference between the 2 functions.

Can we say it's a best practice to use return context.Cause(ctx) instead of return ctx.Err() ?

That is what I said, yes.
 
Is there a reason to use return ctx.Err() instead of return context.Cause(ctx) ?

Probably. You might need to transfer an error over the wire and only have codes for "cancelled" and "timeout", for example. But in general, `context.Cause` has superseded `Context.Err`, the latter just can't be changed for compatibility reasons.
 

Le samedi 4 avril 2026 à 06:17:24 UTC+2, Axel Wagner a écrit :
The latter. ctx.Err() can only return the context-specific errors, while context.Cause can also return the error passed using WithCancelCause.

On Fri, 3 Apr 2026 at 19:43, Pierre Durand <pierre...@gmail.com> wrote:
My code has a select with a case that waits for context cancellation.

Currently I'm doing this:
case <-ctx.Done():
return ctx.Err()

I noticed that some libraries and the stdlib are doing this:
case <-ctx.Done():
return context.Cause(ctx)

What is the best practice ?

--
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.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/9adc5736-37da-4823-abc8-78f202171a15n%40googlegroups.com.

--
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.
Reply all
Reply to author
Forward
0 new messages