jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.83k stars 845 forks source link

lru_cache concurrent map read and write #2173

Closed bobgromozeka closed 22 hours ago

bobgromozeka commented 23 hours ago

Describe the bug We are using sqlc and pgx as it's driver (pgxpool). This is fatal error that we get sometimes

| fatal error: concurrent map read and map write

| goroutine 16295435 [running]:
| github.com/jackc/pgx/v5/internal/stmtcache.(*LRUCache).Invalidate(0xc0015beed0, {0x1649030, 0x1e2})
|   /go/pkg/mod/github.com/jackc/pgx/v5@v5.7.1/internal/stmtcache/lru_cache.go:65 +0x3a
| github.com/jackc/pgx/v5.(*baseRows).Close(0xc000c20000)
|   /go/pkg/mod/github.com/jackc/pgx/v5@v5.7.1/rows.go:177 +0xf9
| github.com/jackc/pgx/v5.(*baseRows).Next(0xc000c20000)
|   /go/pkg/mod/github.com/jackc/pgx/v5@v5.7.1/rows.go:221 +0x85

To Reproduce

rows, err := q.db.Query(ctx, "select * from ......", arg.FarmID, arg.Search)
    if err != nil {
        return nil, err
    }
    defer rows.Close()
    items := []*FindClientsAndUsersRow{}
    for rows.Next() {
        var i FindClientsAndUsersRow
        if err := rows.Scan(
            &i.ID,
            &i.Name,
            &i.Email,
            &i.Type,
        ); err != nil {
            return nil, err
        }
        items = append(items, &i)
    }

Expected behavior No fatal error.

Actual behavior Concurrent map read and write

Version

bobgromozeka commented 22 hours ago

Looks like the problem was caused by using one transaction in different goroutines. Our bad, closing this issue.