go-gorm / gorm

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

fatal error: concurrent map writes in "parseRelation" function #7287

Open leecoolyyds opened 1 day ago

leecoolyyds commented 1 day ago

We found that two tables, both associated with the same table, were called at the same time when the program was started

err = db.Model(&bo.PeInfo{}).Order("id ASC").Find(&Info1List).Error

There's a probability the program will crash

I tested it on a virtual machine and kept rebooting the program for a minute. There is no problem when I list the association of a table or change the name of the association.

type Info1 struct { CreatedAt time.Time json:"created_at" UpdatedAt time.Time json:"updated_at" Code string

Relation []*Relation gorm:"foreignkey:Code;references:Code" }

type Info2 struct { CreatedAt time.Time json:"created_at" UpdatedAt time.Time json:"updated_at" Code string Relation []*Relation gorm:"foreignkey:Code;references:Code" }

The following is the stack information when the program exits fatal error: concurrent map writes fatal error: concurrent map writes

goroutine 44 [running]: gorm.io/gorm/schema.(Schema).parseRelation(0xc000b190e0, 0xc000b252c0) vendor/gorm.io/gorm/schema/relationship.go:100 +0x5d8 gorm.io/gorm/schema.ParseWithSpecialTableName({0x134e900, 0xc0009f25a0}, 0xc00073dc40, {0x1b0c068?, 0xc00013ed80?}, {0x0, 0x0}) vendor/gorm.io/gorm/schema/schema.go:341 +0x22c5 gorm.io/gorm.(Statement).ParseWithSpecialTableName(0xc0009f6380, {0x134e900?, 0xc0009f25a0?}, {0x0?, 0x14f6e00?}) vendor/gorm.io/gorm/statement.go:492 +0x65 gorm.io/gorm.(Statement).Parse(...) vendor/gorm.io/gorm/statement.go:488 gorm.io/gorm.(processor).Execute(0xc0007229b0, 0xc0007eca50?) vendor/gorm.io/gorm/callbacks.go:105 +0x179 gorm.io/gorm.(*DB).Find(0xc000a10e38?, {0x1367600?, 0xc00093cb40}, {0x0, 0x0, 0x0})

Error code location, is indeed a map write if relation.FieldSchema != relation.Schema && relation.Polymorphic == nil && field.OwnerSchema == nil { relation.FieldSchema.Relationships.Relations[""+relation.Schema.Name+""+relation.Name] = relation }

and my gorm version: gorm.io/driver/mysql v1.5.2 gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde

Thank you very much!

leecoolyyds commented 1 day ago

It was the same when I upgraded to the latest version

github-actions[bot] commented 1 day 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 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

jinzhu commented 1 day ago

Can you create a playground for this?

github-actions[bot] commented 1 day 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 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