Shipkeep is an open-source platform to deploy apps on your own servers. Its control plane is one Docker Compose project: the control plane itself, PostgreSQL, Caddy and a few services around them. Upgrading it is the usual routine: set the new image, docker compose pull, docker compose up -d.
In the lab, after one such upgrade, a restore failed at its very last step, the rename of the restored database:
database "shipkeep" is being accessed by other users
Nothing should have been using it. The old control plane was gone, and the restore had stopped the new one. Yet PostgreSQL listed three sessions of the application's role, one of them the job queue's. All three were idle, and all three came from an address that no container had any more.
Left alone, they would have stayed about two hours.
A container that only holds the network
The control plane and Caddy have no network namespace of their own. Both join the namespace of a small container called net:
net:
<<: *shipkeep # the Shipkeep image, like the control plane
command: ['node', '-e', 'setInterval(() => {}, 2147483647)']
ports: ['80:80', '443:443', '443:443/udp', '7443:7443']
control-plane:
network_mode: service:net
caddy:
network_mode: service:net
net does nothing but hold the namespace and the published ports. A container that joins another one's namespace loses it when that one restarts. With a third container holding it, the control plane and Caddy can each restart, or crash, without cutting the other off.
But net runs the Shipkeep image. A new SHIPKEEP_IMAGE changes it too, so up -d recreates it.
What happens during up -d
Compose recreates net first, because the control plane depends on it. The old control plane is still running at that moment, in a namespace that has just lost the interface net brought. When it stops a moment later, its process closes its connections properly. The kernel tries to send each FIN, and has nowhere to send it.
PostgreSQL never hears anything. To the server, an idle session whose client has vanished looks exactly like an idle session that is fine.
The clean fix would be to stop the old control plane while it still has its network. I watched Docker's events during up -d: net is killed, stopped and destroyed before the old control plane is touched. Compose handles the dependency first, and I found no way to change that order from the compose file.
Only the server can notice
The client is gone, so the only side left to notice is PostgreSQL. TCP keepalives exist for exactly this, and PostgreSQL can turn them on for its own sockets. Their defaults explain the two hours: Linux waits 7200 seconds of silence before the first probe.
The fix is three settings on the postgres command:
postgres:
command:
- postgres
- -c
- tcp_keepalives_idle=60
- -c
- tcp_keepalives_interval=10
- -c
- tcp_keepalives_count=6
After 60 seconds of silence, PostgreSQL sends a probe. Six probes without an answer, 10 seconds apart, end the session. A half-open session now lasts at most 60 + 6 × 10 = 120 seconds. A healthy idle connection answers each probe and is not affected.
PostgreSQL also has idle_session_timeout, but it ends every idle session, healthy ones in the control plane's connection pool included. Keepalives only end a session whose other end no longer answers.
The lab
A small Compose project reproduces the mechanism without the rest of Shipkeep, on Docker 29 and Compose 2.40:
- PostgreSQL 17, the image the control plane pins;
- a
netcontainer; - a client in
net's namespace (network_mode: service:net), holding an idlepsqlsession.
The "upgrade" changes an environment variable on net and on the client, as a new image does, then runs up -d. pg_stat_activity tells which sessions are left.
| Keepalives | Right after up -d
|
Later |
|---|---|---|
| none (Linux default, 7200 s) | old and new session | old one still there after 120 s |
| 60 / 10 / 6 | old and new session | old one gone after 49 s, new one kept |
Why 49 seconds and not 120? In that run, the new net got the old one's address. The first probe reached a namespace that knew nothing of that connection, which answered with a reset, and the session ended at once. The session had been idle since before up -d, so that first probe came less than 60 seconds after the upgrade. When the address differs, nothing answers at all, and the session ends at the bound: 120 seconds.
Then the real compose file. docker compose config shows the command, and the postgres service started from it is healthy and reports 60, 10 and 6. Check those over TCP: on a Unix socket, PostgreSQL ignores these settings and reports 0.
The case keepalives do not cover
The fix went to review, and the reviewer, CodeRabbit, found the hole in my two minutes: Linux sends no keepalive probe while data is waiting to be acknowledged.
If the namespace disappears while PostgreSQL is in the middle of sending a result, retransmissions govern the connection, not keepalives. With the default tcp_retries2 of 15, that takes about 15 minutes.
tcp_user_timeout covers that case: the connection closes when transmitted data stays unacknowledged for that long. One more setting:
- -c
- tcp_user_timeout=120000 # milliseconds
I checked that the pinned PostgreSQL 17 image applies all four: over a TCP session, pg_settings shows 120000 ms, and the keepalives at 60, 10 and 6. I did not reproduce the in-flight case in the lab. That bound rests on the kernel's documented behavior.
What I take from it
- A connection that ends without a FIN is invisible to the server. When a client can lose its network before its process exits, as in a shared namespace, the server needs its own way to find out.
- The defaults are measured in hours. 7200 seconds is fine for a database whose clients rarely vanish. Here they vanished at every upgrade.
-
Keepalives cover the idle case only. A connection with data in flight needs
tcp_user_timeoutas well. - The restore only revealed it. The leftover sessions also held connection slots after every upgrade, which no test was looking at.
Where it stands
- Merged as #564, closing #429, and shipped in v0.1.0-beta.2. Moving to it recreates the
postgrescontainer once, since its command changed.up -drestarts the control plane at the same time anyway. - The restore no longer depends on it: it ends any session left on the database before the rename, since the control plane is stopped at that point.
- The installation guide says, under "Upgrade", why old sessions can stay open for up to two minutes.
Where you can help
- You run PostgreSQL behind Docker, Compose or Kubernetes: have you seen half-open sessions pile up after deploys, and what settings did you choose?
- You know Compose's recreate order better than I do: is there a way to stop a service before the container whose namespace it shares is recreated?
- First contribution: the good first issues each say what to change and when it is done.
And a question for the comments: what does SHOW tcp_keepalives_idle; return on your production database, over TCP?
Top comments (4)
On your Compose question, I'd remove the reason net gets recreated instead of fighting the order. net only runs setInterval, so it doesn't need the Shipkeep image. Give it a small image pinned by digest that doesn't change with a release, like registry.k8s.io/pause (Kubernetes runs that container in every pod for this exact job, holding the namespace while app containers restart) or busybox running sleep. Then a new SHIPKEEP_IMAGE leaves net's config untouched, up -d recreates only the control plane, and the old process closes its connections while the interface still exists, so PostgreSQL gets the FIN.
I'd keep the four settings anyway. They still cover a client that vanishes for some other reason, and the day someone edits net's ports, which recreates it again. The cost is one more image to pull and pin, which matters if you support offline installs.
Thanks Mike, that removes the cause instead of bounding it.
netonly gets the Shipkeep image because it shares the control plane's image anchor; all it runs is asetInterval. With a small image pinned by digest that doesn't move with a release,up -dwould leavenetalone. The old control plane would then close its connections while the interface still exists, and PostgreSQL would get the FIN.The compose file already pins every image by digest and Renovate keeps them current, so one more pinned image fits the way it works.
netwould still be recreated when that image's own digest moves, which is rare. That case, a ports change, and any other way a client vanishes are why the four settings stay, as you say.pauseis a good fit: it does exactly that job in every Kubernetes pod, with no shell and nothing else to patch. I'm going to try it.The Kubernetes version of this is a pod being deleted while its connections to an external PostgreSQL stay half open, so the same server settings matter there too. One pairing worth adding is idle_in_transaction_session_timeout, because keepalives only tell you the other end stopped answering at the TCP level, while a live but stuck client can still sit idle in transaction holding locks. I would also watch pg_stat_activity after deploys for idle sessions with an old state_change time, since that is the check that would have caught the leaked slots before the restore did. Where the driver allows it, I set keepalives on the client connection as well, so a dead server side is noticed from the app side too.
Thanks Kashif, all three are good additions, and none of them is in Shipkeep yet.
idle_in_transaction_session_timeout: agreed. Keepalives only catch a peer that stopped answering. A client that is alive but stuck in a transaction keeps its locks, and TCP never notices. That's the next setting I'll look at, with a value above our longest legitimate transaction.
pg_stat_activity after deploys: that's the check that would have caught the leaked slots before the restore did. The upgrade steps could end with that query, and old idle sessions would make a good metric.
Client-side keepalives: the control plane uses node-postgres through Prisma's adapter, which leaves keepAlive off by default. Turning it on is a small change in the pool, and then a dead server gets noticed from the app side too.
And yes, the pod case is the same mechanism: the client's network goes away before its process can close anything, so only the server can tell.