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

Connection timeout wrongly applied to non-preferred hosts #2172

Open
jwreschnig-utiq opened this issue Nov 21, 2024 · 0 comments
Open

Connection timeout wrongly applied to non-preferred hosts #2172

jwreschnig-utiq opened this issue Nov 21, 2024 · 0 comments
Labels

Comments

@jwreschnig-utiq
Copy link

jwreschnig-utiq commented Nov 21, 2024

Describe the bug
(Related to the testing I mentioned #2171 but this appears to be a straightforward bug.)

When connecting with a target_session_attrs preference that cannot be satisfied and the final host times out, the connection to the non-preferred host is not attempted and falsely times out.

To Reproduce

  1. Create a connection string with two hosts and a preference for the second; e.g. a primary and standby with target_session_attrs=prefer-standby
  2. Block network traffic from the client to the standby.
  3. Configure a connection timeout for e.g. 1 second.
  4. Execute a query.

Expected behavior
After 1 second the connection to the standby times out, the primary is chosen instead, and the query executes.

Actual behavior
After 1 second the query returns a timeout.

Version

  • go version go1.23.2 linux/amd64
  • PostgreSQL: PostgreSQL 14.13 (Ubuntu 14.13-1.pgdg22.04+1) on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
  • pgx: v5.7.1

Additional context
From my understanding of the code, connectPreferred resets the timeout for each new host as it iterates the possible targets. However, once it's decided to fall back to fallbackConnectOneConfig, it reuses whatever the last ctx it had from the initial pass was. If this had timed out, the new connection will start in a timed-out state. It should instead probably iterate the entire list again with fresh contexts based on octx. Or, as an optimization, the first non-preferred connection could be returned rather than the config and returned directly if no other preferred one is found / closed if a preferred one is found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant