-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: SendAsync break FlushAsync when RentConnection throw exception #1762
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
base: master
Are you sure you want to change the base?
Conversation
When `_connectionPool.RentConnection() ` throw exception The `publisher.PublishAsync` will throw exception,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the exception handling in the SendAsync method by introducing nested try-catch blocks. The connection acquisition is moved inside the outer try block, and a new outer catch handler is added to handle exceptions during connection rental.
Key changes:
- Connection rental moved from outside try-catch into outer try block
- Added nested try-catch-finally structure with inner block handling message sending and outer block handling connection rental failures
- Outer catch block uses different exception variable name (
evsex)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try | ||
| { | ||
| var msg = new Msg(message.GetName(), message.Body.ToArray()); | ||
| foreach (var header in message.Headers) | ||
| var connection = _connectionPool.RentConnection(); | ||
| try | ||
| { |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outer try-catch structure introduces a bug. If RentConnection() at line 35 throws an exception, the connection variable will not be initialized, but the finally block at line 67 will still attempt to call _connectionPool.Return(connection) with an uninitialized variable. Consider removing the outer try-catch and handling RentConnection() exceptions separately, or ensure the finally block checks if the connection was successfully initialized before returning it.
| } | ||
| catch (Exception ex) | ||
| { | ||
| var warpEx = new PublisherSentFailedException(ex.Message, ex); |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable name 'warpEx' appears to be a misspelling. Consider renaming to 'wrappedEx' or 'wrapEx' for clarity.
| { | ||
| var warpEx = new PublisherSentFailedException(ex.Message, ex); | ||
|
|
||
| var warpEx = new PublisherSentFailedException(e.Message, e); |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable name 'warpEx' appears to be a misspelling. Consider renaming to 'wrappedEx' or 'wrapEx' for clarity.
| catch (Exception e) | ||
| { | ||
| var warpEx = new PublisherSentFailedException(ex.Message, ex); | ||
|
|
||
| var warpEx = new PublisherSentFailedException(e.Message, e); | ||
| return OperateResult.Failed(warpEx); | ||
| } |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic catch clause.
| catch (Exception ex) | ||
| { | ||
| var warpEx = new PublisherSentFailedException(ex.Message, ex); | ||
|
|
||
| throw new PublisherSentFailedException("NATS message send failed, no consumer reply!"); | ||
| return OperateResult.Failed(warpEx); | ||
| } |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic catch clause.
Description:
When
_connectionPool.RentConnection()throw exceptionThe
publisher.PublishAsyncwill throw exception.Issue(s) addressed:
Changes:
Affected components:
How to test:
Provide a step-by-step guide on how to test your changes. Include any relevant information like environment setup, dependencies, etc.
Additional notes (optional):
Provide any additional notes or context that may be relevant to the changes.
Checklist:
Reviewers: