Skip to content

Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook#997

Merged
mtmk merged 5 commits into
nats-io:mainfrom
tormoder:connect-socket-fix-uri-assignment-on-override
Feb 3, 2026
Merged

Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook#997
mtmk merged 5 commits into
nats-io:mainfrom
tormoder:connect-socket-fix-uri-assignment-on-override

Conversation

@tormoder

Copy link
Copy Markdown
Contributor

Fixes #996.

@mtmk

mtmk commented Nov 10, 2025

Copy link
Copy Markdown
Member

Thanks @tormoder good catch! this is fixing some of the logging but exceptions are still coming through with the original URI since the fix is done inside ConnectSocketAsync(uri) call and try/catch blocks wrapping it are still using the old uri. Maybe it can return the uri? edit: that won't work if it's throwing

@mtmk mtmk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks @tormoder, I saw a couple of places exceptions were still reporting the original uri

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this one is still reporting the original uris

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is still reporting the original

}

var connectionFactory = Opts.SocketConnectionFactory ?? (uri.IsWebSocket ? WebSocketFactory.Default : TcpFactory.Default);
_logger.LogInformation(NatsLogEvents.Connection, "Connect to NATS using {FactoryType} {Uri}", connectionFactory.GetType().Name, uri);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is fine

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe this could return the uri (actually that won't work with exception!)

Comment on lines 448 to 458
var target = (uri.Host, uri.Port);
if (OnConnectingAsync != null)
{
_logger.LogInformation(NatsLogEvents.Connection, "Invoke OnConnectingAsync before connecting to NATS {Uri}", uri);
target = await OnConnectingAsync(target).ConfigureAwait(false);
if (target.Host != uri.Host || target.Port != uri.Port)
{
uri = uri with { Uri = new UriBuilder(uri.Uri) { Host = target.Host, Port = target.Port, }.Uri };
var modifiedUri = new UriBuilder(uri.Uri) { Host = target.Host, Port = target.Port }.Uri;
uri = new NatsUri(modifiedUri.ToString(), uri.IsSeed, uri.Uri.Scheme);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe this can be extracted into a method?

@tormoder

Copy link
Copy Markdown
Contributor Author

Thanks, I will take another look.

@tormoder

Copy link
Copy Markdown
Contributor Author

A bit fiddly, but did another attempt.

@mtmk

mtmk commented Nov 19, 2025

Copy link
Copy Markdown
Member

A bit fiddly, but did another attempt.

thanks @tormoder yes it looks a bit fiddly :) it seems to work though. I will have a closer look once the other PR is merged that fixes the tests as well.

@mtmk mtmk self-assigned this Nov 19, 2025
mtmk added 2 commits January 30, 2026 09:03
No need to over optimize redacted value it's never on hot path.
Correcness first.
@mtmk

mtmk commented Jan 30, 2026

Copy link
Copy Markdown
Member

hey @tormoder it looks like the problem was the NatsUri redaction caching. it's never on a hot path so just removed the caching of that string. That seems to be fixing the issue, let me know it works for you too.

@mtmk
mtmk requested a review from scottf January 30, 2026 10:29
@tormoder

Copy link
Copy Markdown
Contributor Author

hey @tormoder it looks like the problem was the NatsUri redaction caching. it's never on a hot path so just removed the caching of that string. That seems to be fixing the issue, let me know it works for you too.

Yes, thank you.

@scottf scottf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mtmk
mtmk merged commit 0bd296a into nats-io:main Feb 3, 2026
21 of 22 checks passed
mtmk added a commit that referenced this pull request Feb 10, 2026
Changes in this release:
* Fix StreamSourceInfo.Active handle -1 correctly (#1058)
* Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook (#997)

Chanfes from preview.2
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)

Changes from preview.1
* Fix and improve build for docs ci error (#1040)
* resolves: 1035 - Use ConnectTimeout when awaiting INFO signal (#1039)
* Fix slow consumer blocking (#1041)
@mtmk mtmk mentioned this pull request Feb 10, 2026
mtmk added a commit that referenced this pull request Feb 10, 2026
Changes in this release:
* Fix StreamSourceInfo.Active handle -1 correctly (#1058)
* Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook (#997)

Chanfes from preview.2
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)

Changes from preview.1
* Fix and improve build for docs ci error (#1040)
* resolves: 1035 - Use ConnectTimeout when awaiting INFO signal (#1039)
* Fix slow consumer blocking (#1041)
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.

Wrong NATS URI used for logging (and exceptions) when using address from OnConnectingAsync hook

3 participants