Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count在表中没有数据的时候偶现panic #6805

Closed
allen7fu opened this issue Jan 25, 2024 · 1 comment
Closed

count在表中没有数据的时候偶现panic #6805

allen7fu opened this issue Jan 25, 2024 · 1 comment
Assignees
Labels

Comments

@allen7fu
Copy link

Description

        tx := db.Begin()
	defer tx.Commit()
	var total int32
	if err := tx.Table(constant.Tbl).Where(query, where.Args...).Count(&total).Error; err != nil {
		return 0, nil, err
	}

	if total == 0 {
		return total, result, nil
	}

	if err := tx.Table(constant.Tbl).Where(query, where.Args...).Order("priority desc").Limit(limit).Offset(offset).Find(&result).Error; err != nil {
		return 0, nil, err
	}

	return total, result, nil

本意是,保证count和分页数据的一致性,在select下起了一个事务,会偶现panic,堆栈如下

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x10087d3e2]

goroutine 155 [running]:
testing.tRunner.func1.2({0x100ff0260, 0x101e9f360})
	/usr/local/go/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1529 +0x39f
panic({0x100ff0260, 0x101e9f360})
	/usr/local/go/src/runtime/panic.go:884 +0x213
database/sql.(*Tx).rollback(0x10010db01?, 0x0?)
	/usr/local/go/src/database/sql/sql.go:2282 +0x22
database/sql.(*Tx).Rollback(0x1013b77c8?)
	/usr/local/go/src/database/sql/sql.go:2314 +0x1b
github.com/jinzhu/gorm.(*Scope).callCallbacks.func1()
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/scope.go:863 +0x7d
panic({0x100ff0260, 0x101e9f360})
	/usr/local/go/src/runtime/panic.go:884 +0x213
database/sql.(*Tx).grabConn(0x0, {0x1013cf2f8, 0xc00004a140})
	/usr/local/go/src/database/sql/sql.go:2209 +0x52
database/sql.(*Tx).QueryContext(0x0, {0x1013cf2f8, 0xc00004a140}, {0xc000da6e60, 0x50}, {0xc0007aabf0, 0x1, 0x1})
	/usr/local/go/src/database/sql/sql.go:2485 +0x57
database/sql.(*Tx).QueryRowContext(...)
	/usr/local/go/src/database/sql/sql.go:2508
database/sql.(*Tx).QueryRow(0xc000ce5900?, {0xc000da6e60?, 0x10?}, {0xc0007aabf0?, 0xc0007aab60?, 0x100f042e0?})
	/usr/local/go/src/database/sql/sql.go:2522 +0x4a
github.com/jinzhu/gorm.rowQueryCallback(0xc000ce5900)
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/callback_row_query.go:36 +0x24a
github.com/jinzhu/gorm.(*Scope).callCallbacks(0xc000ce5900, {0xc0002a2d80, 0x1, 0x100f042e0?})
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/scope.go:869 +0x99
github.com/jinzhu/gorm.(*Scope).row(0xc000ce5900)
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/scope.go:945 +0xf6
github.com/jinzhu/gorm.(*Scope).count(0xc000ce5900, {0x100f14d60, 0xc0000efee4})
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/scope.go:1034 +0x510
github.com/jinzhu/gorm.(*DB).Count(0x1011e0f61?, {0x100f14d60, 0xc0000efee4})
	/Users/xxx/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:401 +0x45

gorm版本:v1.9.16
复现条件:
1)表中没有数据
2)循环调用10000次,大概3000次的时候panic

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jan 25, 2024
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants