cloudwego / localsession

transparently transmit context within or between goroutines
Apache License 2.0
20 stars 2 forks source link

Data Race Bug #5

Open firedtoad opened 3 months ago

firedtoad commented 3 months ago

Describe the bug image

A clear and concise description of what the bug is.

To Reproduce

package main

import (
    "context"
    "github.com/cloudwego/localsession"
    "runtime"
    "testing"
    "time"
)

func init() {
    localsession.InitDefaultManager(localsession.ManagerOptions{
        EnableImplicitlyTransmitAsync: false,
        ShardNumber:                   1,
        GCInterval:                    time.Second,
    })
}

func BenchmarkBind(b *testing.B) {

    b.ResetTimer()
    b.ReportAllocs()
    session := localsession.NewSessionCtx(context.WithValue(context.Background(), "key", 1))
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            localsession.BindSession(session)
            localsession.UnbindSession()
        }
    })
}

func BenchmarkGetSession(b *testing.B) {
    b.ResetTimer()
    b.ReportAllocs()
    session := localsession.NewSessionCtx(context.WithValue(context.Background(), "key", 1))
    localsession.BindSession(session)
    for i := 0; i < b.N; i++ {
        session1, ok := localsession.CurSession()
        if ok {
            _, ok1 := session1.Get("key").(int)
            runtime.KeepAlive(ok1)
        }
        runtime.KeepAlive(ok)
        runtime.KeepAlive(session1)
    }
    localsession.UnbindSession()
}
==================
WARNING: DATA RACE
Write at 0x00c000116418 by goroutine 7:
  github.com/cloudwego/localsession.SessionManager.GC()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:177 +0x237
  github.com/cloudwego/localsession.(*SessionManager).startGC.func1()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:192 +0x10b

Previous read at 0x00c000116418 by goroutine 125:
  github.com/cloudwego/localsession.(*shard).Load()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:93 +0x44
  github.com/cloudwego/localsession.(*SessionManager).UnbindSession()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:149 +0xb4
  github.com/cloudwego/localsession.UnbindSession()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/gls.
go:128 +0x55
  test/bench.BenchmarkBind.func1()
      C:/work/server/test/bench/session_test.go:27 +0x59
  testing.(*B).RunParallel.func1()
      C:/golang/go1.21/src/testing/benchmark.go:815 +0x1c3

Goroutine 7 (running) created at:
  github.com/cloudwego/localsession.(*SessionManager).startGC()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:190 +0x124
  github.com/cloudwego/localsession.NewSessionManager()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/mana
ger.go:77 +0x264
  github.com/cloudwego/localsession.InitDefaultManager.func1()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/gls.
go:67 +0xe4
  sync.(*Once).doSlow()
      C:/golang/go1.21/src/sync/once.go:74 +0xf0
  sync.(*Once).Do()
      C:/golang/go1.21/src/sync/once.go:65 +0x44
  github.com/cloudwego/localsession.InitDefaultManager()
      C:/golang/go1.21/app/pkg/mod/github.com/cloudwego/localsession@v0.0.2/gls.
go:60 +0x52
  test/bench.init.0()
      C:/work/server/test/bench/session_test.go:12 +0x28

Goroutine 125 (running) created at:
  testing.(*B).RunParallel()
      C:/golang/go1.21/src/testing/benchmark.go:808 +0x26a
  test/bench.BenchmarkBind()
      C:/work/server/test/bench/session_test.go:24 +0x177
  testing.(*B).runN()
      C:/golang/go1.21/src/testing/benchmark.go:193 +0x1fa
  testing.(*B).launch()
      C:/golang/go1.21/src/testing/benchmark.go:334 +0x377
  testing.(*B).doBench.func1()
      C:/golang/go1.21/src/testing/benchmark.go:284 +0x33
==================

Expected behavior No Data Race Or Fix DataRace Detector

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Kitex version:

Please provide the version of Kitex you are using.

Environment:

The output of go env.

Additional context

Add any other context about the problem here.

AsterDY commented 3 months ago

let me try to fix it

AsterDY commented 3 months ago

repeated issue to #4