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

Nil panic (invalid memory address) in Unsafe mode with split Bulk operations #368

Open
tomalaci opened this issue Jul 11, 2019 · 0 comments

Comments

@tomalaci
Copy link

tomalaci commented Jul 11, 2019

Bulk Upsert (and maybe other Bulk operations) panic with invalid memory address or nil pointer dereference when you set session to operate in unsafe mode (session.SetSafe(nil)) AND you try to bulk-run more than 1000 operations so you trigger a split.

The source of nil panic is in writeOp, where oplerr is nil:
https://github.com/globalsign/mgo/blob/development/session.go#L5522
But the source of the actual problem is at the end of writeOpCommand, returning nil,nil when Safe mode is off:
https://github.com/globalsign/mgo/blob/development/session.go#L5728

Sample code to reproduce the issue (I am using a replica set):

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
)

const (
	mongoaddrs = "addr1:27017,addr2:27017,addr3:27017"
)

func main() {
	session, dialErr := mgo.DialWithInfo(&mgo.DialInfo{
		Addrs:    strings.Split(mongoaddrs, ","),
		Database: "test",
	})
	if dialErr != nil {
		fmt.Printf("MongoDB dial failure, err: %v", dialErr)
		os.Exit(1)
	}

	session.SetSafe(nil) // Fire and forget

	var upsertArr []interface{}
	for i := 0; i < 1500; i++ { // Make sure upserted docs are >1000 for auto-split
		queryDoc := bson.M{
			"field1": "val1",
		}
		upsertDoc := bson.M{
			"field1": "val1",
			"field2": "val2",
			"field3": "val3",
		}
		upsertArr = append(upsertArr, queryDoc, upsertDoc)
	}

	c := session.DB("").C("test_collection")
	bulk := c.Bulk()
	bulk.Upsert(upsertArr...)
	bulk.Run()
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x563833]

goroutine 1 [running]:
github.com/globalsign/mgo.(*Collection).writeOp(0xc0002ee090, 0x599900, 0xc00005aa80, 0xc00005aa01, 0x0, 0x0, 0x0)
        C:/Users/Azeroc/go/pkg/mod/github.com/globalsign/mgo@v0.0.0-20181015135952-eeefdecb41b8/session.go:5366 +0x6a3
github.com/globalsign/mgo.(*Bulk).runUpdate(0xc00033de20, 0xc00005e340, 0xc000060400, 0xc00005aa60, 0x501)
        C:/Users/Azeroc/go/pkg/mod/github.com/globalsign/mgo@v0.0.0-20181015135952-eeefdecb41b8/bulk.go:330 +0x7b
github.com/globalsign/mgo.(*Bulk).Run(0xc00033de20, 0xc0002bc000, 0xbb8, 0xe00)
        C:/Users/Azeroc/go/pkg/mod/github.com/globalsign/mgo@v0.0.0-20181015135952-eeefdecb41b8/bulk.go:297 +0x237
main.main()
        C:/Users/Azeroc/go_projects/.../main.go:44 +0x687

What version of MongoDB are you using (mongod --version)?

db version v2.6.12

What version of Go are you using (go version)?

go version go1.12.6 windows/amd64

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Azeroc\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Azeroc\go
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Azeroc\go_projects\...\mgo_dev\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Azeroc\AppData\Local\Temp\go-build162542790=/tmp/go-build -gno-record-gcc-switches

What did you do?

See the start of the post.

Can you reproduce the issue on the latest development branch?

Yes

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

No branches or pull requests

1 participant