#12246·temporal

History shard write path permanently wedges after a real Postgres primary failover; deadlock detector correctly names Shard(N)-io-semaphore but takes no recovery action by default

Author: alexandrebatistaCreated Sep 28, 2026Updated Sep 28, 2026
Labelspotential-bug

Expected Behavior

After a Postgres primary failover completes and the database itself is confirmed healthy again, the Temporal history shard(s) affected by the failover should resume normal write throughput within a reasonable, bounded time — either automatically, or via the server's own deadlock detector taking corrective action once it identifies the stall.

Actual Behavior

Postgres/Patroni fails over cleanly in ~30 seconds (confirmed via real StartWorkflowExecution writes succeeding again, and Patroni's reported timeline increment, plus independent psql checks showing no locks, no blocked queries, healthy replication). But every workflow owned by one specific history shard stops making any progress at all — e.g. a started timer never fires, historyLength stays frozen — with zero further log output for that shard once the ~30s DB failover window closes.

The server's own deadlock detector (common/deadlock/deadlock.go) fires repeatedly, roughly every 60s, logging "potential deadlock detected" and naming Shard(N)-io-semaphore — a genuine, repeated, non-clearing detection, not a one-off false positive.

In a multi-replica deployment, force-deleting the specific wedged history pod does not clear it — a fresh replacement pod reproduces the same stuck workflows once it acquires the shard. Force-deleting every history replica eventually produces the missing write, but recovery afterward is abnormally slow (roughly two orders of magnitude slower than normal) and trickles over several more minutes rather than draining cleanly.

Across every configuration tried (see "Mitigations tried" below), the only thing that has ever recovered the wedge is a full restart of the affected history process(es), and even then recovery has taken anywhere from several minutes to 20+ minutes to fully drain. It has never been observed to self-heal without that restart within observation windows of 7–23 minutes.

Suspected mechanism (not proven): ContextImpl.ioSemaphore in service/history/shard/context_impl.go is a per-shard counting semaphore, capacity set by history.shardIOConcurrency (default: 1), acquired before and released (via defer) after every shard-scoped persistence write (AssertOwnership, AddTasks, CreateWorkflowExecution, UpdateWorkflowExecution, ConflictResolveWorkflowExecution, SetWorkflowExecution, DeleteWorkflowExecution, updateShardInfo). Each wraps its DB call in a context bounded by history.shardIOTimeout (default 5s) — so normally even a failing call returns and releases the semaphore well within that window. A permanent hold implies the underlying SQL driver call didn't honor context cancellation at all for one caller — most plausibly a goroutine blocked in a lower-level, uninterruptible read on a TCP connection to the killed primary that never received a RST/FIN, so the context timing out doesn't unblock the read. Because shardIOConcurrency defaults to 1, this single stuck holder blocks every other write for that shard too — including the deadlock detector's own health-check probe, which is why the detector correctly identifies the problem but can never clear it on its own.

Mitigations tried, both insufficient:

  1. sql.maxConnLifetime: 15s (shortening pooled-connection lifetime) — eliminated the visible error noise (previously 6+ minutes of repeated context deadline exceeded) but not the underlying wedge; the shard still silently stopped making progress afterward.
  2. system.deadlock.AbortProcess: true (crash the process on a confirmed deadlock, relying on a restart policy to recover) tried together with history.shardIOConcurrency raised to 10 (to shrink blast radius) — the combination backfired: raising concurrency meant the deadlock detector's own probe (which only needs to acquire one free token) never blocked, since 9 other tokens stayed free even with one permanently held — so "potential deadlock detected" never fired at all, and AbortProcess never triggered. The wedge reproduced identically; only a manual restart recovered it. Worth flagging on its own: raising shardIOConcurrency as a mitigation silently defeats the deadlock detector's own detection mechanism for this class of issue, since the detector's probe and the real workload contend for the same pool and the probe only needs one token.

Ask:

  • Is this a known limitation of the SQL persistence layer's connection-recovery path?
  • Is there a supported way to bound a shard I/O semaphore acquisition's hold time independent of the underlying driver's context-cancellation behavior — e.g. a hard ceiling that force-releases the token and marks the connection bad?
  • Is system.deadlock.AbortProcess combined with a non-default shardIOConcurrency known to interact this way, and if so, is that worth a doc note on either setting?

Full logs, tctl workflow show output, and the docker-compose/Helm reproduction setup are available on request.

Possibly related, found during research (not confirmed to be the same root cause):

  • #11691 (open, v1.31.2) — same shape (a real backend disruption leaves an internal subsystem silently and permanently wedged, health still reports SERVING, only a full restart recovers it), different specific trigger (sql: database is closed not recognized as a refresh signal), same general subsystem.
  • #6514 (fixed by #6538) — a distinct, already-fixed reconnect-throttle bug in the same general area; establishes this class of defect has precedent, but doesn't explain either wedge reproduced here.
  • #11547 (open) — a brief Unavailable blip resetting a shard's retry backoff, causing a synchronized retry storm; a plausible contributor to the abnormally slow multi-minute recovery trickle observed after a restart, likely a distinct mechanism from the io-semaphore wedge itself.

Steps to Reproduce the Problem

  1. Start ~20 workflow executions (each running one activity or timer for ~75s), and wait a few seconds for them to be genuinely in flight.
  2. Force-kill (docker kill -9 or equivalent) the current Patroni-managed Postgres primary.
  3. Confirm Patroni promotes the replica and the database itself is healthy (independently, via direct psql queries and Patroni's own REST API) — then observe workflow progress and server logs for the next several minutes.

Specifications

  • Version: Reproduced on both temporalio/auto-setup:1.24.2 (single-process) and Helm chart temporal/temporal:1.7.0 (server app version 1.32.0, multi-replica: 2 frontend / 2 history / 2 matching / 1 worker, numHistoryShards: 4) — both wedge with the identical signature, on materially different SQL-reconnect code (v1.24.2 predates common/persistence/sql/sqlplugin/db_handle.go entirely; v1.32.0 includes it plus the priority-aware semaphore from #6034/#6142), which argues against "it's just old reconnect code" as a sufficient explanation.
  • Platform: Patroni-managed PostgreSQL 16 (1 primary + 1 async streaming replica) behind HAProxy (on-marked-down shutdown-sessions set on the Postgres backend), single-node etcd as Patroni's DCS, Elasticsearch 7.17.9 ×2 for visibility. maxConnLifetime: 15s applied on the SQL persistence datastore in every run.