Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context.Done() may never reach if waiting on r.incoming <- msgErr #936

Merged
merged 1 commit into from
Sep 13, 2023

Conversation

nbajaj90
Copy link
Contributor

@nbajaj90 nbajaj90 commented Sep 13, 2023

While analysing an issue, we observed that even after context is cancelled, the code was blocked in ConsumeClaim. And the reason is if the code if blocked at r.incoming <- msgErr, it may fail to honour or may skip the outside session.Context().Done().
Reference - https://stackoverflow.com/questions/67608032/context-ctx-done-not-being-executed-even-though-context-was-passed-to-the-functi

Solution is to add session.Context().Done() with r.incoming <- msgErr in select clause.

Signed-off-by: nbajaj90 <nbajaj90@gmail.com>
@nbajaj90 nbajaj90 force-pushed the unblock_context_done branch from 99a3dd0 to 6aa2742 Compare September 13, 2023 12:25
select {
case r.incoming <- msgErrObj:
// do nothing
case <-session.Context().Done():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spot on! usually we'd return session.Context.Err() to let the caller know why the function returned. But this would break the existing behavior (returning nil). A user could always use context.Cause(ctx), but that might not be intuitive from the nil behavior.

Anyways, IMHO with this change you can remove the code in line 95?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code on line 95 is still required. If code on line no. 64 case msg, ok := <-claim.Messages(): doesn't get hit, that code on line no. 95 would exit the flow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, was under the assumption that ok would be false due to propagated channel somewhere into Sarama...but seems that's not the case.

@embano1 embano1 enabled auto-merge September 13, 2023 17:33
Copy link
Member

@embano1 embano1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx a ton!

@embano1 embano1 merged commit 238c545 into cloudevents:main Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants