Raw() and Scan() api when using in Concurrent coroutine may cause various sql errors unexpectd and sporadically
current version:
gorm.io/driver/postgres v1.5.4 gorm.io/gorm v1.25.5
using method
var connParam = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
"xx", "xx", "xx", "xx", 5432)
pgClient, _ := gorm.Open(postgres.Open(connParam), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
client := pgClient
alarmTable := client.Table("alarm_send_record")
for i := 0; i < 2; i++ {
go func() {
systemAlarms := make([]model.AlarmEntity, 0)
//sql := fmt.Sprintf("select * from alarm_send_record where alarm_type = 1 and status=2 and contact_address= '%s' limit %v", innerItem, count)
//sql := fmt.Sprintf("select * from alarm_send_record where alarm_type = 1 and status=2 limit 1")
sql := "select * from alarm_send_record where id='1739241918322896899'"
fmt.Println(sql)
tx := alarmTable.Raw(sql)
tx.Scan(&systemAlarms)
}()
}
result
- execute above code many times, some result success, but occasionally occur erros,and error is different, like following exam
select * from alarm_send_record where id='1739241918322896899'
select * from alarm_send_record where id='1739241918322896899'
2023/12/28 10:19:19 D:/goprojects/fofund-alarm-dispatcher/test/postgres_test.go:554 ERROR: syntax error at or near "select" (SQLSTATE 42601)
[6.872ms] [rows:-]
select * from alarm_send_record where id='1739241918322896899'
select * from alarm_send_record where id='1739241918322896899'
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x168f467]
goroutine 42 [running]:
database/sql.(*Rows).Next(0x0)
D:/Go20/src/database/sql/sql.go:2951 +0x27
gorm.io/gorm.(*DB).Scan(0xc000160d20, {0x1b53d20, 0xc0000080d8})
C:/Users/qiankai/go/pkg/mod/gorm.io/gorm@v1.25.5/finisher_api.go:530 +0x225
fofund-alarm-dispatcher/test.TestAnalyseScanErrorV2.func2()
D:/goprojects/fofund-alarm-dispatcher/test/postgres_test.go:565 +0xdb
created by fofund-alarm-dispatcher/test.TestAnalyseScanErrorV2
D:/goprojects/fofund-alarm-dispatcher/test/postgres_test.go:556 +0x26b
Comment From: github-actions[bot]
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.io ✨ Search Before Asking ✨