Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Silent self-deadlock with SetMaxOpenConns(1) when insert hooks use the pool

未关闭
#1,411 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
go, sqlite
领域
backend, database

调研方向

Start by running the provided Go reproduction with MaxOpenConns(1), then compare the client.Insert and InsertTx paths with hook and middleware execution. Review the SetMaxOpenConns guidance and relevant hook documentation before choosing a scoped direction. Done should include coverage for the deadlock scenario and a clear user-facing explanation or failure mode.

由索引模型根据 Issue 内容生成。

描述

Note: this issue was written by Claude (an AI assistant) from an investigation it ran, and @bgentry reviewed it and okayed filing it. @brandur, could you take a look?

The riversqlite package docs recommend dbPool.SetMaxOpenConns(1) to avoid SQLITE_BUSY errors from River's parallel internal operations. With that setting, two fairly natural patterns hang silently until the context deadline, or forever if the context has none. database/sql has no way to detect the self-deadlock, so the user sees no error, only a stuck insert.

1. A hook or middleware that uses the pool during a non-transactional insert. client.Insert opens its own transaction, which takes the pool's only connection, and runs insert hooks and middleware inside it. Hooks and middleware don't receive that transaction, so a hook that needs the database has to go through the pool. It then waits for the connection its own insert is holding.

2. client.Insert (instead of InsertTx) while the app holds an open transaction on the same pool. River's BEGIN waits for the connection that the app's transaction holds.

Reproduction against master (9573203e) with riversqlite and modernc.org/sqlite, a file database in WAL mode:

type poolHook struct {
	river.HookDefaults
	db *sql.DB
}

func (h *poolHook) InsertBegin(ctx context.Context, params *rivertype.JobInsertParams) error {
	_, err := h.db.ExecContext(ctx, "SELECT 1")
	return err
}

// db.SetMaxOpenConns(n); client configured with Hooks: []rivertype.Hook{&poolHook{db: db}}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
_, err := client.Insert(ctx, someArgs{}, nil)
MaxOpenConns(1) hook uses pool during client.Insert: err=context deadline exceeded after 2.001s
MaxOpenConns(4) hook uses pool during client.Insert: err=<nil> after 1ms
MaxOpenConns(1) client.Insert while app holds a tx: err=error beginning transaction: context deadline exceeded after 2s

This isn't strictly SQLite-specific. Any database/sql or pgx pool capped at one connection behaves the same way. It mostly bites SQLite users in practice, because that's where the docs recommend a single connection.

Some possible directions, not mutually exclusive:

  1. Document it. Next to the SetMaxOpenConns(1) advice, and in the hooks/middleware docs, explain that hook and middleware code runs while River holds a connection, and that app code inside its own transaction should use InsertTx.
  2. Fail fast on re-entry. River could mark the context while it holds its own insert transaction, so a re-entrant River call on the same client returns a clear error instead of hanging. This wouldn't catch a hook calling db.Exec directly on the raw pool, but it would cover River-level re-entry.
  3. Let hooks and middleware use River's transaction. Expose the insert's transaction to them, for example through the context, so they can do database work without a second connection. This is a bigger API change.

My suggestion is (1) plus (2), with (3) as a possible follow-up.

主要语言
Go
星标
5.7k
派生
187
平均合并
22 小时 36 分钟
30 天内合并 PR
89

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

riverqueue/river 的其他 Issue

查看 riverqueue/river 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。