Conversation
c48b98e to
c55401b
Compare
c55401b to
3af368e
Compare
When a struct field's Scan method panics, database drivers may deadlock if rows.Close() or tx.Rollback() is called afterwards. This is because the driver holds internal locks during Scan that are not properly released when a panic occurs. This fix: - Wraps rows.Scan() to catch panics and convert them to errors - Adds scanPanicError type to wrap panic values as errors - Adds IsScanPanicError() function to detect scan panic errors - Modifies generated code to skip tx.Rollback() on scan panic errors - Updates Row.Scan() and scanAny() to handle panic gracefully - Avoids calling rows.Close() after a panic to prevent deadlock 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
3af368e to
52326be
Compare
x5iu
added a commit
that referenced
this pull request
Dec 16, 2025
When a struct field's Scan method panics, database drivers may deadlock if rows.Close() or tx.Rollback() is called afterwards. This is because the driver holds internal locks during Scan that are not properly released when a panic occurs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
修复了在
Scan过程中发生 panic 时导致死锁的问题。问题描述
当结构体字段的
Scan方法发生 panic 时,某些数据库驱动会在 Scan 过程中持有内部锁。如果在 panic 后执行defer rows.Close()或defer tx.Rollback(),会导致死锁。解决方案
Row.Scan: 移除
defer r.rows.Close(),改为在每个成功/错误路径上显式调用Close()。这样当rows.Scan()panic 时,不会尝试关闭 rows,panic 会正常传播。生成的代码: 移除
defer tx.Rollback(),改为在每个错误返回点显式调用tx.Rollback()。这样当 Scan 中 panic 后,不会尝试 rollback,panic 会正常传播到调用者。设计原则
Test plan
./test.sh验证所有测试通过🤖 Generated with Claude Code