Skip to content

feat: add TCP keepalive for MySQL and PostgresSQL.#3559

Open
xuehaonan27 wants to merge 3 commits into
transact-rs:mainfrom
xuehaonan27:tcp_keepalive
Open

feat: add TCP keepalive for MySQL and PostgresSQL.#3559
xuehaonan27 wants to merge 3 commits into
transact-rs:mainfrom
xuehaonan27:tcp_keepalive

Conversation

@xuehaonan27

@xuehaonan27 xuehaonan27 commented Oct 12, 2024

Copy link
Copy Markdown
Contributor

BREAK CHANGE: [sqlx_core::net::socket::connect_tcp]. New parameter added.

Add TCP keepalive configuration which could be enabled by [PgConnectOptions::tcp_keep_alive] and [MySqlConnectOptions::tcp_keep_alive].

Does your PR solve an issue?

fixes #3540

BREAK CHANGE: [`sqlx_core::net::socket::connect_tcp`]. New parameter added.

Add TCP keepalive configuration which could be enabled by
[`PgConnectOptions::tcp_keep_alive`] and [`MySqlConnectOptions::tcp_keep_alive`].

@CommanderStorm CommanderStorm 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.

(not a maintainer)

I thought you might still appreciate an external review ^^

Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment on lines +186 to +191
#[derive(Debug, Clone)]
pub struct TcpKeepalive {
pub time: Duration,
pub interval: Duration,
pub retries: u32,
}

@CommanderStorm CommanderStorm Oct 12, 2024

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.

https://docs.rs/socket2/latest/socket2/struct.TcpKeepalive.html

I think we should use the builder pattern too and copy the platform support of socket2 given that this is explicitly done in their case.

Also adding documentation via docstrings is really helpful => copying their docs is likely fine.

Likely we should make this Copy too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for giving me a review!
That's reasonable, or we might have to maintain the consistency between TcpKeepalive defined in socket2 and in sqlx_core.
Should I re-export TcpKeepalive definition in socket2?

@CommanderStorm CommanderStorm Oct 13, 2024

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.

Current code is a build failour on the following platforms (assuming one tries to use the code):

any(
    target_os = "openbsd",
    target_os = "redox",
    target_os = "solaris",
    target_os = "nto",
    target_os = "espidf",
    target_os = "vita",
    target_os = "haiku",
)

Please use the same platform cfg scoping as the new dependency.

Comment thread sqlx-core/src/net/socket/mod.rs Outdated
host: &str,
port: u16,
with_socket: Ws,
keepalive: &Option<TcpKeepalive>,

@CommanderStorm CommanderStorm Oct 12, 2024

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.

Suggested change
keepalive: &Option<TcpKeepalive>,
keepalive: Option<&TcpKeepalive>,

Would this not be a better API?
https://users.rust-lang.org/t/api-design-option-t-vs-option-t/34139/2

Given that socket2::TcpKeepalive is Copy, I think copying that derive and dropping the reference might be more ergonomic. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't notice that TcpKeepalive in socket2 is Copy.
I will adjust it. Thx.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't notice the API design preference in Rust. Thank you for pointing this out for me!

@xuehaonan27

Copy link
Copy Markdown
Contributor Author

Should I re-export TcpKeepalive in socket2 or give a definition on our own?

`TcpKeepalive`.
fix: `connect_tcp` API design.
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment on lines +187 to +191
pub struct TcpKeepalive {
pub time: Option<Duration>,
pub interval: Option<Duration>,
pub retries: Option<u32>,
}

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.

If we want to encapsulate instead of using socket2::TcpKeepalive in the public api (that is something that @abonander should decide), we should use a newtype wrapper instead of just copying the other code.

Suggested change
pub struct TcpKeepalive {
pub time: Option<Duration>,
pub interval: Option<Duration>,
pub retries: Option<u32>,
}
pub struct TcpKeepalive(socket2::TcpKeepalive)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's reasonable, thx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea. But socket2::TcpKeepalive is not Copy (Sorry I made a mistake before).
So doing so we won't have Copy trait.

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.

Interesting. I don't know how I came up with the idea that it does. Must have misread a part of the code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, I have mixed feelings about using a bespoke struct here.

These builder methods could just be directly on the ConnectOptions structs.

I suppose being able to define TcpKeepalive separately in a const could be nice for reusability, but at the same time, having to import and build a separate type would also be a little annoying if you only use it once.

I think maybe because interval and retries aren't supported on all platforms, we should only expose the tcp_keepalive_time() on the ConnectOptions builders. We can always add the others later.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These fields should also be #[doc(hidden)] or just private.

@abonander

Copy link
Copy Markdown
Collaborator

After some thought, I'm not entirely sure how much value this actually has.

As I worked out in estuary/flow#1676 (comment), keepalive likely won't solve the original reporter's problem.

It could be useful for other reasons, such as keeping connections from timing out, or catching when a server disconnected abruptly without sending a FIN packet.

However, because the connection state is managed directly, we won't see a keepalive timeout until the next time we try to read from or write to the socket, at which point we're already trying to use it anyway.

I suppose that's still preferable to it hanging forever on a read that will never complete, though.

Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment thread sqlx-core/src/net/socket/mod.rs Outdated
Comment on lines +187 to +191
pub struct TcpKeepalive {
pub time: Option<Duration>,
pub interval: Option<Duration>,
pub retries: Option<u32>,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, I have mixed feelings about using a bespoke struct here.

These builder methods could just be directly on the ConnectOptions structs.

I suppose being able to define TcpKeepalive separately in a const could be nice for reusability, but at the same time, having to import and build a separate type would also be a little annoying if you only use it once.

I think maybe because interval and retries aren't supported on all platforms, we should only expose the tcp_keepalive_time() on the ConnectOptions builders. We can always add the others later.

tcp_keepalive.rs and update related code

- Moved the TCP keepalive type definition from
  `mod.rs` to new source file `tcp_keepalive.rs`
- Modified method `tcp_keep_alive` to `tcp_keepalive_time`
  in `MySqlConnectOptions` and `PgConnectOptions`
- Updated `socket2` dependency in `sqlx-core` to
  enable feature `all`.
@xuehaonan27
xuehaonan27 requested a review from abonander October 16, 2024 03:24
@svanharmelen

svanharmelen commented Aug 26, 2025

Copy link
Copy Markdown

I think this logic is required for pgnotify connections to recover after the connection is somehow broken. Could you take another look at this one @abonander to see what is needed to get this merged? Thanks!

@acheung-harmonicinc

Copy link
Copy Markdown

Catching situations where a server disconnects abruptly without sending a FIN packet is a real-world use case for us.

Our application subscribes to PostgreSQL notifications via database triggers. After an infrastructure issue, we observed that the application silently stopped receiving notifications while the process itself remained running and unaware of any connection problem.

I was able to reproduce this by using iptables rules to drop all packets from the database server. In this scenario, the PgListener continues to wait indefinitely and never detects that the connection is broken. I believe a similar situation would occur if the database host is physically disconnected from the network: no FIN or RST is sent, so the client never gets notified that the connection is actually gone.

When I applied the LD_PRELOAD-based keepalive workaround described here:

https://www.redpill-linpro.com/techblog/2024/12/17/failovers-and-keepalive.html

the PgListener was able to detect the dead connection and recover correctly.

However, relying on the LD_PRELOAD trick is hacky and has undesirable side effects: it affects all TCP connections from the process, not just the database connections.

It would be very helpful if sqlx exposed dedicated TCP keepalive configuration for its database connections, so we can:

  • Enable keepalive explicitly for DB connections only.
  • Tune keepalive parameters (e.g., idle time, interval, probe count) to match our failover and detection requirements.

This would allow PgListener (and other long-lived connections) to reliably detect broken connections in scenarios where the server disappears without a clean shutdown.

@c-solo

c-solo commented Feb 10, 2026

Copy link
Copy Markdown

We're hitting the same issue in production — long-lived PostgreSQL connections silently go stale after network changes, and without TCP keepalive there's no way to detect it. Would love to see this merged, thank you for the work on this!

adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
adriangb added a commit to adriangb/sqlx that referenced this pull request Jul 24, 2026
Adds `TcpKeepalive` and `net::connect_tcp_with_keepalive`, plus
`tcp_keepalive()` on `PgConnectOptions` and `MySqlConnectOptions` and
libpq-compatible URL parameters for Postgres (`keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_count`).

Keepalive is off by default, so nothing changes for existing users.

Motivation: without it, a connection whose server disappeared *without*
closing the socket never finds out. A failover, a killed container, or a
dropped NAT mapping leaves the client blocked reading a response that will
never arrive; there is nothing left to retransmit, so no RST is ever
provoked and the read waits forever. `TCP_NODELAY`, which is all SQLx sets
today, does not help, and a server-side `statement_timeout` cannot fire on
a server that is gone.

This is the case @abonander allowed for in
transact-rs#3559 (comment):
"I suppose that's still preferable to it hanging forever on a read that
will never complete." Worth adding on the objection raised there, that a
keepalive timeout is only noticed the next time the socket is used: that
is not true of a blocked reader. When the probes are exhausted the kernel
sets `sk_err` to `ETIMEDOUT` and wakes anyone parked on the socket, so the
pending read fails rather than waiting for someone to poke it.

We hit this in production: a maintenance loop that had a query in flight
when its Postgres instance went away stopped doing work permanently, while
other loops in the same process recovered in seconds. Reproduced by holding
an `ACCESS EXCLUSIVE` lock so the query blocks server-side, then removing
the server from the network and restarting it.

Implementation follows transact-rs#3559 by @xuehaonan27, rebased onto the current
`connect_tcp` and reduced to an additive API: `connect_tcp` keeps its
signature and delegates, so external drivers are unaffected.
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.

Add the ability to enable TCP keepalive on Postgres client connections

6 participants