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

NATS reconnect #1546

Merged
merged 1 commit into from
Nov 29, 2022
Merged

NATS reconnect #1546

merged 1 commit into from
Nov 29, 2022

Conversation

sungam3r
Copy link
Collaborator

fixes #1544

// reset connection in case of stuck so the next HC call will establish it again
// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1544
if (connection.State == ConnState.DISCONNECTED || connection.State == ConnState.CLOSED)
_connection = null;
Copy link

Choose a reason for hiding this comment

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

When this branch is executed (setting connection to null) then the code afterwards may throw System.NullReferenceException because it's accessing the State property in the GetHealthCheckResultFromState method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

_connection is not connection

if (connection == null)
{
connection = CreateConnection(_options);
var exchanged = Interlocked.CompareExchange(ref _connection, connection, null);
Copy link

Choose a reason for hiding this comment

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

Not sure if I'm aware of the complete picture, but if CompareExchange is used to throw away connection objects created by other threads maybe double checked locking would be sufficient on the critical section that creates the connection or using Lazy<T>

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eventually they all do the same. I prefer non-blocking APIs if applicable.

@sungam3r sungam3r merged commit db2ef39 into master Nov 29, 2022
@sungam3r sungam3r deleted the nats branch November 29, 2022 20:34
@sungam3r sungam3r mentioned this pull request Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NATS check still failing after NATS Server is restarted (AspNetCore.HealthChecks.Nats)
2 participants