jackc / jackc/pgx

Raw() does not return if connection is closed

Open
#2,346 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

**Describe the bug**
I am accessing a connection via `Raw()` using the stdlib compatibility layer in order to close a connection that is in a bad state. I discovered that if I am in a transaction, `Raw()` does not return, and instead hangs. If I rollback the transaction before calling `Raw()`, this does not happen.

I'm not sure if this is a bug in pgx or maybe `database/sql`.

**To Reproduce**
I put a reproducer here: https://gist.github.com/horgh/e3ba60ecc20f8cf2bf7e4d795c336350

The reproducer includes a `Rollback()` which avoids the hang. If you comment out the `Rollback()` on line 69 you will see it hang. There are a few other comments in there that might be of interest.

Please run your example with the race detector enabled. For example, `go run -race main.go` or `go test -race`.

**Expected behavior**
`Raw()` should return.

**Actual behavior**
`Raw()` does not return

**Version**
- Go: `$ go version` -> go version go1.24.4 linux/amd64
- PostgreSQL: `$ psql --no-psqlrc --tuples-only -c 'select version()'` -> PostgreSQL 16.9 (Ubuntu 16.9-1.pgdg24.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit
- pgx: `$ grep 'github.com/jackc/pgx/v[0-9]' go.mod` -> v5.7.5

**Additional context**
I am closing the connection to try to work around #2223

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproducer in main.go, especially the Raw() call and the Rollback() on line 69, then run it with the race detector as requested. Trace the stdlib compatibility layer's transaction behavior and verify that Raw() returns without requiring Rollback() first and without hanging.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.