globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 232 forks source link

Bug in copySession method #365

Open XEuRoMAN opened 5 years ago

XEuRoMAN commented 5 years ago

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

4

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

1.12

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

ubuntu, linux, amd

What did you do?

Trying to copy the session and have different socket timeout value. Sadly, if I set SetSocketTimeout on one session, value is changed on all sessions that were copied using copySession https://github.com/globalsign/mgo/blob/master/session.go#L844

This is because method creates new object of Session but reuses pointer values in object properties, e.g.: Session struct has property dialInfo *DialInfo. The new Session object takes pointer value from Session that should be copied :

scopy := Session{
    ...
    dialInfo: session.dialInfo,
}

SetSocketTimeout method sets values and changes are applied to all the sessions that were copied.

s.dialInfo.Timeout = d
s.dialInfo.ReadTimeout = d
s.dialInfo.WriteTimeout = d

Can you reproduce the issue on the latest development branch?

Probably, yes. Didn't see any related issues or PRs.