go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.77k stars 3.93k forks source link

Getting Data race gorm.RegisterDialect/gorm.newDialect() #5084

Closed anil1996 closed 2 years ago

anil1996 commented 2 years ago

Hello,

Encountered an issue while running test code, getting data race as following

[2022-02-11T13:11:38.067Z] WARNING: DATA RACE [2022-02-11T13:11:38.067Z] Read at 0x00c00038d6e0 by goroutine 75: [2022-02-11T13:11:38.067Z] runtime.mapaccess2_faststr() [2022-02-11T13:11:38.067Z] /usr/local/go/src/runtime/map_faststr.go:107 +0x0 [2022-02-11T13:11:38.067Z] github.com/jinzhu/gorm.newDialect() [2022-02-11T13:11:38.067Z] /home/jenkins/workspace//vendor/github.com/jinzhu/gorm/dialect.go:63 +0x87 [2022-02-11T13:11:38.067Z] github.com/jinzhu/gorm.(DB).clone() [2022-02-11T13:11:38.067Z] /home/jenkins/workspace//vendor/github.com/jinzhu/gorm/main.go:853 +0xba [2022-02-11T13:11:38.067Z] github.com/jinzhu/gorm.(DB).NewScope() [2022-02-11T13:11:38.067Z] /home/jenkins/workspace//vendor/github.com/jinzhu/gorm/main.go:204 +0x44 [2022-02-11T13:11:38.067Z] github.com/jinzhu/gorm.(DB).Find() [2022-02-11T13:11:38.068Z] /home/jenkins/workspace//vendor/github.com/jinzhu/gorm/main.go:354 +0x84 [2022-02-11T13:11:38.068Z] /store.(DBClient).Find()

Previous write at 0x00c00038d6e0 by goroutine 289: [2022-02-11T13:11:38.068Z] runtime.mapassign_faststr() [2022-02-11T13:11:38.068Z] /usr/local/go/src/runtime/map_faststr.go:202 +0x0 [2022-02-11T13:11:38.068Z] github.com/jinzhu/gorm.RegisterDialect() [2022-02-11T13:11:38.068Z] /home/jenkins/workspace//vendor/github.com/jinzhu/gorm/dialect.go:77 +0x4ee

The register dialect is happening as below code snippet

d, ok := gorm.GetDialect("mysql")
if !ok {
    return nil, gerror.New(GormGetDialectError, "Failed to get Gorm dialect \"mysql\"")
}

gorm.RegisterDialect(driverName, d)

Please let me know, if any more input is needed.

github-actions[bot] commented 2 years ago

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 2 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

jinzhu commented 2 years ago

this should only be called once, doesn't expect goroutine safe.