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

async: ensure connection close error is set #180

Merged
merged 1 commit into from
Oct 17, 2024
Merged

Conversation

lgfa29
Copy link
Contributor

@lgfa29 lgfa29 commented Sep 23, 2024

The previous implementation had a couple of issues that could cause closeWithError() to not propagate the passed error properly.

  1. c.error.Store() is only called after c.close(), which could cause the channel returned by CloseChannel() to be closed before the error is set. Callers that that block on the channel may attempt to read the error before it is set.
  2. If c.close() fails, closeError is returned, but not set to c.error.Store(), causing callers to miss it. Additionally, the original error is ignored and not made available to callers at all.

To fix these issues, closeWithError() is updated to set c.error before calling c.close() and all errors are returned and set to c.error by using errors.Join().

In order to support different kinds of concrete error types, the type of the error field had to be changed from atomic.Value to error.

The previous implementation had a couple of issues that could cause
closeWithError() to not propagate the passed error properly.

1. c.error.Store() is only called after c.close(), which could cause the
   channel returned by CloseChannel() to be closed before the error is
   set. Callers that that block on the channel may attempt to read the
   error before it is set.
2. If c.close() fails, closeError is returned, but not set to
   c.error.Store(), causing callers to miss it. Additionally, the
   original error is ignored and not made available to callers at all.

To fix these issues, closeWithError() is updated to set c.error before
calling c.close() and all errors are returned and set to c.error by
using errors.Join().

In order to support different kinds of concrete error types, the type of
the error field had to be changed from atomic.Value to error.

Signed-off-by: Luiz Aoqui <luizaoqui@loopholelabs.io>
Copy link
Contributor

@SuperManifolds SuperManifolds left a comment

Choose a reason for hiding this comment

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

LGTM

@lgfa29 lgfa29 merged commit ef0be7e into main Oct 17, 2024
5 checks passed
@lgfa29 lgfa29 deleted the fix-async-close-err branch October 17, 2024 17:51
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