Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Turso 5 min read How-to mediumAdept

How to get your database access code deadlocked in Golang

In Go's database/sql, failing to close a Rows result can deadlock the program when a loop over query results returns early on error. Rows holds a reader lock on the underlying sql.Conn until it is fully iterated or explicitly closed, so a deferred conn.Close() waits forever for the writer lock. The same leak hits plain db.QueryContext through the connection pool, which is easy to reproduce with SetMaxOpenConns(1). The happy path works fine, so the bug hides in under-tested error paths; the fix is to always defer rows.Close().

Assumes: Go basicsGo database/sql packagereader-writer locks

Related posts

  • A linter I really recommend you to use if you're doing SQL in Golang

    Turso mediumAdept

    The sqlclosecheck linter catches unclosed sql.Rows and sql.Stmt objects in Go code, which otherwise can cause deadlocks or inefficient disk use in databases like SQLite. A short example shows it flagging a missing rows.Close() and passing once defer rows.Close() is added. It can be added to CI as a GitHub Actions step via go vet -vettool, or enabled in golangci-lint through .golangci.yaml, and is recommended as a standard CI check.

  • Something you probably want to know about if you're using SQLite in Golang

    Turso mediumAdept

    In SQLite's WAL mode, an unclosed Go database/sql rows object keeps a read transaction open, which stops checkpoints from moving changes from the WAL file into the main database file. The WAL then grows without bound until the process restarts. In the example, leaking rows left a 42 MB WAL and a 4.1 kB main file, versus about 406 kB and 4.1 MB normally, and a manual PRAGMA wal_checkpoint did not fix it. Always close the results of Query and QueryContext; the same leak also blocks VACUUM.

  • Battling database performance

    incident.io mediumAdept

    Intermittent API timeouts at incident.io over two weeks traced back to requests blocking on Go's database/sql client-side connection pool, with no single slow query or pool to blame. The team shipped 24 changes along the way. These included materialised views, new indexes, query rewrites, a one-second lock timeout on transactions, and moving Slack webhook handling onto PubSub. They also built per-operation tracking of connection-hold time using an ngrok/sqlmw middleware and context counters that avoid double-counting inside transactions. The root cause turned out to be an unnecessary transaction wrapped around every Slack modal submission. Many short transactions added together had exhausted the pools, and removing it ended the timeouts.

  • parking_lot: ffffffffffffffff...

    Fly.io hardMaster

    Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading. The cause was traced to parking_lot RwLock words being set to 0xFFFFFFFFFFFFFFFF. The hunt ran from a known if-let lock-lifetime footgun through closure-scoped lock refactors with timeouts and telemetry, parking_lot's deadlock detector, gdb core-dump inspection, read_recursive experiments and miri. Core dumps showed threads waiting on a lock nobody held, and logging the raw lock word revealed every bit set, including the unused UPGRADEABLE flag. The post also covers Corrosion, Fly.io's CRDT-structured SQLite replicated over SWIM gossip, and the watchdog that bounces stuck proxies to keep deadlocks survivable.

  • We’re All Just Looking for Connection

    Slack Engineering mediumAdept

    Spikes of EOFError on SQL queries in Quip's Python backend (which also powers Slack canvas) traced back to connection-state checks that only tracked closes initiated by the application. AWS's database proxy began closing connections after 24 hours, and because asyncio's StreamWriter.is_closing() doesn't reflect a peer-initiated FIN (a socket in CLOSE_WAIT can still be read), the code never reconnected before issuing queries. Instrumenting the drift between the connected flag and the actual closed state surfaced six more bugs, including websocket false negatives, stale HTTP pool clients and missed Redis cleanup. Fixing them nearly eliminated the errors and unblocked a stalled migration from a custom runtime to asyncio, where an overloaded RPC peer closing pooled connections had caused hanging PDF exports.

The quest for software engineering knowledge.

Look:
Layout: