-
Notifications
You must be signed in to change notification settings - Fork 52
servicebus.Queue.Receive returns "context canceled" #156
Comments
I am also experiencing the same issue but I am using |
Also experiencing this on version |
FYI, for anyone who find the unexpected modification of context affecting their deployable, consider passing a child context of the original context instead to Azure service bus. Then, when the child context exits, it won't affect the parent context. If the child context needs to cancel across several go routine, cancelling the parent context will ensure that. Using the example above, parentCtx, parentCancel := context.WithCancel(context.Background())
for {
if parentCtx.Err() == context.Canceled {
break
}
func() {
childCtx, childCancel := context.WithDeadline(parentCtx, time.Millisecond)
defer childCancel()
err = q.Receive(childCtx, servicebus.HandlerFunc(func(ctx context.Context, msg *servicebus.Message) error {
log.Println("MESSAGE RECEIVED", msg.ID)
return msg.Complete(context.Background())
}))
if err != nil {
if err == context.DeadlineExceeded {
log.Println("queue timeout")
} else if err == context.Canceled {
log.Println("queue receiver returned an error. Continuing", err) // error is "context canceled"
}
}
}()
}
...
// cancel all goroutines/functions receiving from azure service bus (e.g. SIGTERM/SIGINT)
parentCancel() |
Same problem in |
A servicebus handler should never return an error. if you error out, it closes the connection, and the context, which is probably not what you want. All message handler share the same context unless you specifically pass a child context to your handler. this is what @14leeyuchieh explained above. |
Btw, there's actually 1 more case where my code did not work for. That is in the case of a network fault when the box loses internet access. I notice in that case, the servicebus client hangs indefinitely, even after internet access is restored. I didn't get a chance to figure out why exactly, but closing the client and re-instantiating a new one fixed it for me. I'll try to provide an example later if I have time, but otherwise just leaving the note here for other folks. |
There are a couple PR that got merge about restoring connections, if you manage to get a reliable test, make sure to validate it with the latest version :) |
@14leeyuchieh hey, we're having the same issue on a project at work and I have a few questions. With "the same issue" I mean these
We're still using @serbrech what's the state of those connection restoration merge requests? They've all been merged and are part of the latest version - Some factsUsing a common
I've At the same time, the After changing to a child context with a 10 minute timeout for the This In fact, they only failed the first few times (1x, 2x) and then stabilised for some reason in the latest test that I've been making yesterday & today. Here are a few stripped down and annotated logs from my example. See the timestamps. I've started three queue consumers/clients at the same time. Two of them failed only once after being started, one failed twice, and that was it. After that they kept receiving. The {"level":"info","time":"2021-04-29T19:07:52Z","message":"Consumers started"}
FIRST QUEUE:
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"Queue.Receive failed"}
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"DeadLetter.ReceiveOne failed"}
SECOND QUEUE:
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"Queue.Receive failed"}
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"DeadLetter.ReceiveOne failed"}
THIRD QUEUE:
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"Queue.Receive failed"}
{"level":"error","error":"context canceled","time":"2021-04-29T21:18:00Z","message":"DeadLetter.ReceiveOne failed"}
THIRD QUEUE, SECOND TIME:
{"level":"error","error":"context canceled","time":"2021-04-29T23:29:04Z","message":"Queue.Receive failed"}
{"level":"error","error":"context canceled","time":"2021-04-29T23:29:04Z","message":"DeadLetter.ReceiveOne failed"} |
@rokf, I am having the same problems in both versions v0.10.10 and v0.10.11
This issue is impacting my work and I am stuck on it. EDITED: My code is like this:
for {
err = queue.Receive(
context.Background(),
servicebus.HandlerFunc(func(ctx context.Context, message *servicebus.Message) error {
log.Debug("consumer received msg")
// handler logic here
return message.Complete(ctx)
}))
if err != nil {
if err == context.DeadlineExceeded {
log.Fatal("consumer: ", err)
} else {
log.Error("consumer: ", err) // error is "context canceled"
queue, err = refreshQueue(sb.ns, sb.queueName)
if err != nil {
log.Fatalf("failed to refresh queue named %s", sb.queueName)
}
}
continue
}
} |
Here's an interesting observation with If they use a common If each one of them has their own Any idea why they behave like this? |
Hi all, We've moved development of this package to the azure-sdk-for-go repo link. We've tried to really simplify how error handling works and are still working on how to make errors more actionable (Azure/azure-sdk-for-go#15610). Note that while it's not "done" that's not because we haven't started or done work in the area. If you'd like to give it a shot there's a migration guide to ease the transition: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/messaging/azservicebus/migrationguide.md I've run this particular scenario quite a bit in the new library, as adding in a regular stress testing regimen is part of the development cycle. I created a scenario that is the same as what you have above, and I'm not seeing any errors. This doesn't mean they don't exist, but it's no longer easy to replicate this kind of bug. |
Every now and then servicebus.Queue.Receive returns "context canceled" error on production. I suspect every time it happens we lose a queue message.
I can't reproduce it locally and not sure whether it's the SDK bug or an implementation issue.
Any ideas what it could be?
Package version:
github.com/Azure/azure-service-bus-go v0.9.1
Example of code below:
The text was updated successfully, but these errors were encountered: