go-gorm / gorm

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

data race with cycle relation #7110

Open zwell opened 4 months ago

zwell commented 4 months ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/752

Description

In my project, hava some cycle orm relation, I use -race option to test, There is a certain probability that "DATA RACE"

Here are the test results for playgroud

==================
WARNING: DATA RACE
Read at 0x00c0005f2150 by goroutine 26:
  gorm.io/gorm/schema.(*Schema).guessRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:553 +0x1e44
  gorm.io/gorm/schema.(*Schema).parseRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:95 +0x744
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:350 +0x4164
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func2()
      E:/work/code/source/playground/main_test.go:23 +0xa4

Previous write at 0x00c0005f2150 by goroutine 25:
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:365 +0x46b3
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func1()
      E:/work/code/source/playground/main_test.go:16 +0xa4

Goroutine 26 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:21 +0x4e
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44

Goroutine 25 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:14 +0x2c
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44
==================
==================
WARNING: DATA RACE
Read at 0x00c0005f2168 by goroutine 26:
  gorm.io/gorm/schema.(*Schema).guessRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:553 +0x1e44
  gorm.io/gorm/schema.(*Schema).parseRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:95 +0x744
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:350 +0x4164
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func2()
      E:/work/code/source/playground/main_test.go:23 +0xa4

Previous write at 0x00c0005f2168 by goroutine 25:
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:369 +0x489b
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func1()
      E:/work/code/source/playground/main_test.go:16 +0xa4

Goroutine 26 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:21 +0x4e
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44

Goroutine 25 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:14 +0x2c
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44
==================
==================
WARNING: DATA RACE
Read at 0x00c0005f2180 by goroutine 26:
  gorm.io/gorm/schema.(*Schema).guessRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:553 +0x1e44
  gorm.io/gorm/schema.(*Schema).parseRelation()
      E:/work/code/source/playground/gorm/schema/relationship.go:95 +0x744
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:350 +0x4164
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func2()
      E:/work/code/source/playground/main_test.go:23 +0xa4

Previous write at 0x00c0005f2180 by goroutine 25:
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/schema/schema.go:373 +0x4a7b
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      E:/work/code/source/playground/gorm/statement.go:493 +0x146
  gorm.io/gorm.(*Statement).Parse()
      E:/work/code/source/playground/gorm/statement.go:489 +0x3ab
  gorm.io/gorm.(*processor).Execute()
      E:/work/code/source/playground/gorm/callbacks.go:105 +0x386
  gorm.io/gorm.(*DB).First()
      E:/work/code/source/playground/gorm/finisher_api.go:129 +0x2f8
  gorm.io/playground.TestGORM.func1()
      E:/work/code/source/playground/main_test.go:16 +0xa4

Goroutine 26 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:21 +0x4e
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44

Goroutine 25 (running) created at:
  gorm.io/playground.TestGORM()
      E:/work/code/source/playground/main_test.go:14 +0x2c
  testing.tRunner()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      D:/Golang/sdk/go1.22.3/src/testing/testing.go:1742 +0x44
==================
edwh commented 1 month ago

I see this problem too - usually in testing on CircleCI. Because it's a race, it's not 100% reproducible for me.

WARNING: DATA RACE
Write at 0x00c000706450 by goroutine 486:
  gorm.io/gorm/schema.(*Schema).guessRelation()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/schema/relationship.go:590 +0x121c
  gorm.io/gorm/schema.(*Schema).parseRelation()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/schema/relationship.go:93 +0x88b
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/schema/schema.go:342 +0x3ac5
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/statement.go:493 +0x144
  gorm.io/gorm.(*Statement).Parse()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/statement.go:489 +0x3cd
  gorm.io/gorm.(*processor).Execute()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/callbacks.go:105 +0x3a8
  gorm.io/gorm.(*DB).First()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/finisher_api.go:129 +0x2e4
  github.com/freegle/iznik-server-go/message.GetMessagesByIds.func1.1()
      /home/circleci/project/message/message.go:120 +0x184

Previous read at 0x00c000706450 by goroutine 489:
  gorm.io/gorm/schema.ParseWithSpecialTableName()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/schema/schema.go:341 +0x3a08
  gorm.io/gorm.(*Statement).ParseWithSpecialTableName()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/statement.go:493 +0x144
  gorm.io/gorm.(*Statement).Parse()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/statement.go:489 +0xa6
  gorm.io/gorm.(*DB).ScanRows()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/finisher_api.go:577 +0x6d
  gorm.io/gorm.(*DB).Scan()
      /home/circleci/go/pkg/mod/gorm.io/gorm@v1.25.12/finisher_api.go:535 +0x3da
  github.com/freegle/iznik-server-go/message.GetMessagesByIds.func1.4()
      /home/circleci/project/message/message.go:167 +0x1cf