Skip to content

pgconn: preserve full error chain in normalizeTimeoutError#2556

Open
c-tonneslan wants to merge 2 commits into
jackc:masterfrom
c-tonneslan:fix/normalize-timeout-error-preserve-chain
Open

pgconn: preserve full error chain in normalizeTimeoutError#2556
c-tonneslan wants to merge 2 commits into
jackc:masterfrom
c-tonneslan:fix/normalize-timeout-error-preserve-chain

Conversation

@c-tonneslan
Copy link
Copy Markdown

Fixes #2383.

When a network timeout isn't caused by a context cancellation or deadline exceeded, normalizeTimeoutError was wrapping only the net.Error found via errors.As, discarding any outer context. For example, a DialError wrapping a net.OpError wrapping poll.DeadlineExceededError would have the DialError (including its address info) stripped off.

The fix is to wrap err (the full original error) rather than netErr (the inner net.Error extracted by errors.As). This keeps the entire chain intact so callers can still inspect it. The net.Error is still reachable via errors.As since it's embedded in the chain.

Added a test that verifies a wrappedDialError{addr} wrapping a timeoutNetError is still unwrappable after going through normalizeTimeoutError.

When ConnectTimeout is set, connectPreferred creates a per-host timeout
context derived from octx for each host. After the loop, ctx holds
whatever context was used for the last attempt, which may have already
expired. The fallback connection (for prefer-standby/prefer-primary) was
using that stale ctx, so it would fail immediately with a deadline
exceeded error even though the original context still had time left.

Fix this by creating a fresh timeout context from octx for the fallback,
same as we do for each host in the loop.

Fixes jackc#2172
When a network timeout isn't caused by a context cancellation or deadline,
normalizeTimeoutError was wrapping only the net.Error found via errors.As,
discarding any outer error context (like DialError's address info).

Wrap the original error instead so callers can still inspect the full chain.

Fixes jackc#2383
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.

normalizeTimeoutError is overwriting dial error

1 participant