Skip to content

fix: Mark a host offline when the connection attempt times out - #6740

Open
mt89vein wants to merge 2 commits into
npgsql:mainfrom
mt89vein:fix-multihost-host-slow-response
Open

mt89vein wants to merge 2 commits into
npgsql:mainfrom
mt89vein:fix-multihost-host-slow-response

Conversation

@mt89vein

@mt89vein mt89vein commented Nov 3, 2026 •

Copy link
Copy Markdown

A host that accepts TCP but never answers the startup handshake stayed in the Unknown state. The read timeout breaks the connector while it is still Connecting, which intentionally skips marking the host offline, and the following Break from Open returns early because the connector is already Broken.

Mark the host offline when the final Open attempt fails too, so subsequent connections skip it until Host Recheck Seconds expires, like a host that refuses connections.

with fix, we can see this in output from issue for both cases (tested on real multihost pg)

   open #1:   2067 ms
   open #2:      4 ms
   open #3:      4 ms
   open #4:      3 ms
   open #5:      3 ms

Fixes #6733

@mt89vein

Copy link
Copy Markdown
Author

@roji It would be great if you could take a look at this pull request

Comment thread src/Npgsql/Internal/NpgsqlConnector.cs Outdated

bool IsHostDown(Exception reason)
=> reason is NpgsqlException { IsTransient: true } ne &&
(ne.InnerException is not TimeoutException || Settings.CancellationTimeout != -1) ||

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.

I'm pretty sure this whole logic is for the case when we hit an error during a query.
As for when hit an error during physical open, I 'think' there's no point in trying to differentiate between exceptions. Every single physical open is the exact same (unlike queries), so there's no reason to assume that the next physical open will succeed unlike the previous one. Which is why I propose to just always mark it as offline if we fail.
@roji I'll need your opinion here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Partly agree. You're right that the CancellationTimeout check is query-specific: during a physical open there's no command that could have timed out, so a timeout always means the host isn't responding, and I've made that unconditional for open.

I wouldn't go as far as marking the host offline on any open failure though. Not every open failure says anything about the host: authentication errors (28xxx), 3D000 for a missing database, "No username could be found", SSL/GSS negotiation failures. If we mark the host offline for those, a perfectly healthy host gets skipped for Host Recheck Seconds; if every host "fails" like that, the user ends up with "No suitable host was found." instead of the real error, and DataSource.Clear() drops the pool on every bad-password attempt. Unknown_state_on_connection_authentication_failure already asserts that the state stays Unknown in that case, so I kept the IsHostDown filtering and only relaxed it for timeouts during open via duringOpen.

@roji would still be good to hear your take.

Comment thread src/Npgsql/Internal/NpgsqlConnector.cs Outdated
// A nested break (e.g. a timeout while reading the startup response) skips
// marking the host offline because we're still connecting. If the outer Open
// ultimately failed, we still want the host to be marked offline.
if (markHostAsOfflineOnConnecting)

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.

Since we're using markHostAsOfflineOnConnecting in a single place, what if we just get rid of it and move MarkHostAsOffline to NpgsqlConnector.Open? Something like:

try
{
    await OpenCore(...);
}
catch (Exception ex)
{
    MarkHostAsOffline(ex);
    throw;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, thanks. Dropped the parameter and moved the marking into NpgsqlConnector.Open. Break no longer has any open-specific logic

@mt89vein
mt89vein force-pushed the fix-multihost-host-slow-response branch from 17a6971 to 6e0882f Compare November 18, 2026 17:45
A host that accepts TCP but never answers the startup handshake stayed in the Unknown state. The read timeout breaks the connector while it is still Connecting, which intentionally skips marking the host offline, and the following Break from Open returns early because the connector is already Broken.

Mark the host offline when the final Open attempt fails too, so subsequent connections skip it until Host Recheck Seconds expires, like a host that refuses connections.

Fixes npgsql#6733
Remove the markHostAsOfflineOnConnecting parameter from Break and instead
mark the host offline from NpgsqlConnector.Open, once the physical open has
definitively failed.

Also don't apply the query-only CancellationTimeout check when a physical
open fails: a timed-out open always means the host is unresponsive. Other
non-host failures during open (e.g. authentication errors) still leave the
host state untouched.

Simplify the test to use PgPostmasterMock instead of a hand-rolled
TcpListener.
@mt89vein
mt89vein force-pushed the fix-multihost-host-slow-response branch from 6e0882f to 12cd2f7 Compare November 23, 2026 17:59

This branch has not been deployed

No deployments
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.

Multi-host: a host that accepts TCP but never responds is never marked offline, so every new connection waits for the full Timeout

2 participants