---
title: "How to get your database access code deadlocked in Golang"
description: "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."
url: "https://swe.quest/articles/how-to-get-your-database-access-code-deadlocked-in-golang-f01f82ad269b4372"
canonical: "https://turso.tech/blog/how-to-get-your-database-access-code-deadlocked-in-golang-f31ec98c3833"
source: "Turso"
published: "2023-07-27"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["go","concurrency","debugging","databases"]
---

# How to get your database access code deadlocked in Golang

A post on [Turso](https://swe.quest/sources/turso.md), published 2023-07-27, 5 min read, How-to. Original: [https://turso.tech/blog/how-to-get-your-database-access-code-deadlocked-in-golang-f31ec98c3833](https://turso.tech/blog/how-to-get-your-database-access-code-deadlocked-in-golang-f31ec98c3833)

## Summary

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().

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Go](https://swe.quest/tags/go.md) (Languages)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- Go basics ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- Go database/sql package ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- reader-writer locks

## Keywords

database/sql, sql.Conn, rows.Close, Turso

## Related posts

- [A linter I really recommend you to use if you're doing SQL in Golang](https://swe.quest/articles/a-linter-i-really-recommend-you-to-use-if-you-re-doing-sql-in-golang-01ce20ff247e64a5.md): Turso · 2023-08-10 · Medium · 3 min read · Go, Static analysis, CI/CD, SQL
  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.
- [Something you probably want to know about if you're using SQLite in Golang](https://swe.quest/articles/something-you-probably-want-to-know-about-if-you-re-using-sqlite-in-golang-998bba3901e282c7.md): Turso · 2023-08-03 · Medium · 5 min read · SQLite, Go, Storage engines, Debugging
  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.
- [Battling database performance](https://swe.quest/articles/battling-database-performance-5c5eb431f69f9020.md): incident.io · 2023-04-20 · Medium · 10 min read · PostgreSQL, Performance engineering, Observability, Go, Incidents & postmortems
  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.
- [parking\_lot: ffffffffffffffff...](https://swe.quest/articles/parking-lot-ffffffffffffffff-e7cd36eac4ddf5cc.md): Fly.io · 2025-05-28 · Hard · 16 min read · Concurrency, Rust, Debugging, Incidents & postmortems, Load balancing
  Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading.
- [We’re All Just Looking for Connection](https://swe.quest/articles/we-re-all-just-looking-for-connection-b10700c8e0417e48.md): Slack Engineering · 2024-10-10 · Medium · 7 min read · Python, Networking, Debugging, Concurrency, Incidents & postmortems
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
