Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.0.2 exists data race #195

Closed
UnderTreeTech opened this issue Aug 31, 2021 · 2 comments
Closed

V1.0.2 exists data race #195

UnderTreeTech opened this issue Aug 31, 2021 · 2 comments
Labels
question Further information is requested

Comments

@UnderTreeTech
Copy link
Contributor

UnderTreeTech commented Aug 31, 2021

MongoDB Version: 3.6.23
Qmgo Version: 1.0.2
Test Command: go test -race -coverprofile=coverage.txt -covermode=atomic ./...

Recently I upgrade qmgo to v1.0.2, and I got data race error when I run test with -race flag. Folling is the error output.

  • Error output
==================
WARNING: DATA RACE
Write at 0x00c0003af300 by goroutine 30:
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*connection).connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/connection.go:231 +0x90c
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).connectionInitFunc·dwrap·16()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:134 +0x58

Previous read at 0x00c0003af300 by goroutine 7:
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).stale()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:199 +0x1f5
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.connectionExpiredFunc()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:97 +0x1b1
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*resourcePool).Get()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/resource_pool.go:125 +0x215
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).get()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:388 +0x6b5
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*Server).Connection()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/server.go:268 +0x233
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*SelectedServer).Connection()
      <autogenerated>:1 +0x6c
  go.mongodb.org/mongo-driver/x/mongo/driver.Operation.getServerAndConnection()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/operation.go:246 +0x103
  go.mongodb.org/mongo-driver/x/mongo/driver.Operation.Execute()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/operation.go:301 +0x107
  go.mongodb.org/mongo-driver/x/mongo/driver/operation.(*Command).Execute()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/operation/command.go:104 +0x318
  go.mongodb.org/mongo-driver/mongo.(*Database).RunCommand()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/mongo/database.go:203 +0x27a
  go.mongodb.org/mongo-driver/mongo.(*Client).Ping()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/mongo/client.go:265 +0x23c
  github.com/qiniu/qmgo.client()
      /Users/go/pkg/mod/github.com/qiniu/[email protected]/client.go:165 +0x1fe
  github.com/qiniu/qmgo.NewClient()
      /Users/go/pkg/mod/github.com/qiniu/[email protected]/client.go:142 +0xaf
  mongo.New()
      /Users/Desktop/mongo/mongo.go:24 +0xf2
  mongo.TestMongo()
      /Users/Desktop/mongo/mongo_test.go:18 +0x126
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      /usr/local/go/src/testing/testing.go:1306 +0x47

Goroutine 30 (running) created at:
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).connectionInitFunc()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:134 +0xfc
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).connectionInitFunc-fm()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:128 +0x39
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*resourcePool).add()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/resource_pool.go:102 +0x57
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*resourcePool).Maintain()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/resource_pool.go:210 +0x271
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*resourcePool).initialize()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/resource_pool.go:95 +0x10e
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*pool).connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/pool.go:208 +0x84
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*Server).Connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/server.go:215 +0x2f5
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.ConnectServer()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/server.go:136 +0x9a
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*Topology).addServer()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/topology.go:686 +0x14d
  go.mongodb.org/mongo-driver/x/mongo/driver/topology.(*Topology).Connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/x/mongo/driver/topology/topology.go:213 +0xeac
  go.mongodb.org/mongo-driver/mongo.(*Client).Connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/mongo/client.go:156 +0x92
  go.mongodb.org/mongo-driver/mongo.Connect()
      /Users/go/pkg/mod/go.mongodb.org/[email protected]/mongo/client.go:106 +0x67
  github.com/qiniu/qmgo.client()
      /Users/go/pkg/mod/github.com/qiniu/[email protected]/client.go:157 +0xa4
  github.com/qiniu/qmgo.NewClient()
      /Users/go/pkg/mod/github.com/qiniu/[email protected]/client.go:142 +0xaf
  mongo.New()
      /Users/Desktop/mongo/mongo.go:24 +0xf2
  mongo.TestMongo()
      /Users/Desktop/mongo/mongo_test.go:18 +0x126
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1259 +0x22f
  testing.(*T).Run·dwrap·21()
      /usr/local/go/src/testing/testing.go:1306 +0x47

Goroutine 7 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:1306 +0x726
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1598 +0x99
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1259 +0x22f
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1596 +0x7ca
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1504 +0x9d1
  main.main()
      _testmain.go:91 +0x324
==================
  • Following is my test code

mongo.go

package mongo

import (
	"context"
	"fmt"

	"github.com/qiniu/qmgo"
)

// Config MongoDB DSN configs
type Config struct {
	DSN         string
	DBName      string
	MaxPoolSize uint64
	MinPoolSize uint64
}

type UserInfo struct {
	ID   int64  `bson:"_id"`
	Name string `bson:"name"`
}

func New(cfg *Config) (*qmgo.Client, func() error) {
	cli, err := qmgo.NewClient(context.Background(),
		&qmgo.Config{
			Uri:         cfg.DSN,
			MaxPoolSize: &cfg.MaxPoolSize,
			MinPoolSize: &cfg.MinPoolSize,
		})
	if err != nil {
		panic(fmt.Sprintf("open mongo client fail, err is %s", err.Error()))
	}

	return cli, func() error {
		return cli.Close(context.Background())
	}
}

mongo_test.go

package mongo

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMongo(t *testing.T) {
	ctx := context.Background()
	cfg := &Config{
		DSN:         "mongodb:https://root:[email protected]:27017/?connect=direct",
		DBName:      "test_mongo",
		MaxPoolSize: 100,
		MinPoolSize: 10,
	}
	cli, close := New(cfg)
	defer close()

	coll := cli.Database(cfg.DBName).Collection("user")
	// single insert
	_, err := coll.InsertOne(ctx, &UserInfo{
		ID:   int64(211023454441556),
		Name: "qmgo",
	})
	assert.Nil(t, err)
}
@UnderTreeTech UnderTreeTech changed the title V1.0.2 exist data race V1.0.2 exists data race Aug 31, 2021
@jiangz222 jiangz222 added the question Further information is requested label Aug 31, 2021
@jiangz222
Copy link
Collaborator

Need to test official driver in the same way.

UnderTreeTech added a commit to UnderTreeTech/qmgo that referenced this issue Sep 3, 2021
jiangz222 added a commit that referenced this issue Sep 3, 2021
@jiangz222
Copy link
Collaborator

fix in release v.1.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants