Node stops silently when its PostgreSQL connections hang: no timeout, no error, health stays OK

Issue Description

Stalwart 1.0.3 (8b32686), 3 nodes, PostgreSQL + Patroni through a local HAProxy, Valkey, S3 blobs. connectTimeout 15000, the default.

When the database stops answering but TCP stays up, every database wait on that node blocks. I found no limit in the code and saw none in 20 minutes. The node stops sending mail and refuses logins by hanging. It logs nothing and its health checks stay 200, so nothing shows the fault. Other nodes keep delivering, which hides it further.

Expected Behavior

A database wait that gets no reply ends in a logged error within a bounded time, the node’s queue carries on with other work, and /healthz/ready reports the store isn’t answering.

Actual Behavior

Result, 1 run, 20 minutes frozen:

Check, frozen node Result
Process Same PID throughout, no restart
/healthz/live, /healthz/ready 200 at every check
Log warnings and errors, first 20 min 0 of 29 lines; 0 on the other nodes too
Mail sent by this node 0 (it delivered before the freeze)
Submission Connects, then hangs at login; my client gave up after 45 s, 4 of 4
Database sockets ESTABLISHED, last receive growing past 1,100 s
Message held with HOLDFOR, ingested here, due during the freeze Delivered by another node, at +289 s
Controls, the other two nodes 42 of 42 delivered, 0 duplicates

After the sockets were killed, still frozen: 9 store.postgresql-error at once, so a closed socket is reported. Then 3 new connections, ESTABLISHED with nothing received for 180 s and no further errors, though connectTimeout is 15 s. Submission still timed out.

After the thaw it recovered by itself, same PID: submission in 0.7 s, then 24 of 24 further messages delivered, 20 sent by this node, 0 duplicates.

Reproduction Steps

  1. Run a node’s database connections through a local proxy. Check every node delivers.
  2. Freeze the proxy on one node: pkill -STOP -x haproxy. Sockets stay ESTABLISHED, the kernel still ACKs, no reply comes.
  3. Keep submitting on the other nodes (control). Try a submission on the frozen node, and watch its log, /healthz/live, /healthz/ready and sockets.
  4. Kill its database sockets while still frozen: ss -K -tn 'dst 127.0.0.1:5433'. Wait 180 s.
  5. Thaw: pkill -CONT -x haproxy.

Relevant Log Output

Log, after killing the sockets, trimmed:

2026-11-05T05:33:53Z ERROR PostgreSQL error (store.postgresql-error) reason = "connection closed", causedBy = "crates/store/src/dispatch/store.rs:152", causedBy = "crates/smtp/src/queue/walk.rs:157", ...
2026-11-05T05:33:53Z ERROR PostgreSQL error (store.postgresql-error) reason = "connection closed", causedBy = "crates/store/src/dispatch/store.rs:152", causedBy = "crates/services/src/task_manager/manager.rs:396", ...
2026-11-05T05:33:53Z ERROR PostgreSQL error (store.postgresql-error) listenerId = "submission", localPort = 587, reason = "connection closed", causedBy = "crates/store/src/dispatch/store.rs:44", ...

Stalwart Version

v1.x

Installation Method

Binary (Linux)

Database Backend

PostgreSQL

Blob Storage

S3-compatible

Search Engine

PostgreSQL

Directory Backend

Internal

Additional Context

Limits of this test: it freezes all database access on one node, a stand-in for a hung proxy or peer. It doesn’t show that one hung socket among several stops the queue.

Seen on 1.0.2, not reproduced on 1.0.3: after a database primary was powered off, one node stopped processing its queue for about 9 hours with a single socket stuck. Same PID, no errors, health OK. Killing that socket brought the queue back.

Source as I read it:

  • store/src/backend/postgres/main.rs:29-50 sets connect_timeout but no deadpool wait, create or recycle timeout, and no statement or read timeout. deadpool’s defaults are no timeouts. tokio-postgres applies connect_timeout to the socket connect only, not the startup handshake, which would fit the 180 s above.
  • smtp/src/queue/manager.rs:168 awaits process inline, so one stuck read holds up that node’s whole queue manager.
  • TCP keepalive or tcp_user_timeout wouldn’t catch this case, since the peer’s kernel still ACKs. It takes a timeout at the application level.

docs/cluster/configuration/load-balancing.md says /healthz/ready is 200 when the node “has its data store open”. http/src/request.rs:707 only checks that a store is configured, so a store that has stopped answering still counts.

We bound this outside Stalwart: TCP timeouts on HAProxy, and a watchdog that restarts Stalwart when a socket to the database has had no reply for 5 minutes.

Possibly related, no shared cause shown: Stalwart 0.16.23: mail delivery stops while messages remain queued, delivery stops while messages remain queued. A stuck database socket would be worth checking next time.

Question: would you consider bounding these waits, with the error logged, and having /healthz/ready turn 503 when the store stops answering? Waiting forever on a database read is a fault in itself, even with the network to blame.

Tested and written with AI help.

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Your reading of the source matches what I found. I can’t speak for the maintainers on whether they’ll bound these waits, so a human will need to answer that part.

  • Pool timeouts: In crates/store/src/backend/postgres/main.rs, PostgresStore::open only sets cfg.connect_timeout from the connectTimeout setting. The ManagerConfig only sets the recycling method. The PoolConfig only sets the max pool size. I found no wait, create, recycle, statement or read timeout there.
  • Readiness check: In crates/http/src/request.rs, /healthz/live always returns 200. /healthz/ready returns 200 whenever self.core.storage.data is not none, so it only checks that a data store is configured. It never queries the store, which is why a store that has stopped answering still reports ready.

I didn’t check the claims about deadpool’s defaults, tokio-postgres’ handshake behaviour, or the queue manager awaiting process inline. Those parts of your analysis are plausible but unverified here.

Your HAProxy TCP timeouts plus a watchdog on idle database sockets is a reasonable workaround for now. The docs don’t describe any application-level query timeout for PostgreSQL.

Docs: Load balancing | Stalwart

This isn’t covered in the current docs. A community member or staff member will follow up.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Nothing in the PostgreSQL backend put a limit on waiting for the server. The pool only had connectTimeout, tokio-postgres applies that to the TCP connect alone (which is why your new connections sat in the handshake for 180 s), and no request had a deadline. With the proxy frozen and the kernel still ACKing, every query just waited, and /healthz/ready only checked that a data store was configured, so it stayed at 200.

The fix bounds this at the connection level, so it covers every query, the login handshake and pool recycling:

  • A new responseTimeout setting on the PostgreSQL data store (default 2 minutes) limits how long a request waits for the server to start answering. When it passes, the connection is closed with a logged store.postgresql-error (“PostgreSQL server did not respond in time”), every request waiting on it fails with an error, and the pool replaces it with a fresh connection. Idle pooled connections are never timed out.
  • connectTimeout now covers the whole connect, handshake and authentication included.
  • /healthz/ready returns 503 while the store isn’t answering: the node is marked unresponsive on a response or connect timeout and becomes ready again with the next reply from the server.

Since every wait now ends, a stuck read no longer holds the queue manager forever either; it errors out after responseTimeout and the queue carries on. The default leaves plenty of room for slow full-text searches on large installs, and you can lower it if you’d rather detect a hang sooner; the slowest statements we measured under heavy load were a few seconds. The fix ships in the next weekly release. Your HAProxy server timeouts and the watchdog are a good setup until you upgrade, and the HAProxy timeouts are worth keeping afterwards too.

On t/1762: if that stall was a stuck socket, it would now end within responseTimeout and leave a store.postgresql-error in the log, so it won’t be silent next time.