Skip to content

Commit e9e975d

Browse files
committed
GODRIVER-2867 Unpin connections when ending a session. (#1330)
1 parent 76f284d commit e9e975d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

mongo/integration/load_balancer_prose_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"go.mongodb.org/mongo-driver/bson"
1717
"go.mongodb.org/mongo-driver/internal/assert"
18+
"go.mongodb.org/mongo-driver/internal/require"
1819
"go.mongodb.org/mongo-driver/mongo"
1920
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2021
"go.mongodb.org/mongo-driver/mongo/options"
@@ -99,5 +100,33 @@ func TestLoadBalancerSupport(t *testing.T) {
99100
_, err := mt.Coll.InsertOne(ctx, bson.M{"x": 1})
100101
assertErrorHasInfo(mt, err, 0, 1, 0)
101102
})
103+
104+
// GODRIVER-2867: Test that connections are unpinned from transactions
105+
// when the transaction session is ended. Create a Client with
106+
// maxPoolSize=1 and expect that it can start and commit 5 transactions
107+
// with that 1 connection.
108+
mt.RunOpts("transaction connections are unpinned", maxPoolSizeMtOpts, func(mt *mtest.T) {
109+
{
110+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
111+
defer cancel()
112+
113+
for i := 0; i < 5; i++ {
114+
sess, err := mt.Client.StartSession()
115+
require.NoError(mt, err, "StartSession error")
116+
117+
err = sess.StartTransaction()
118+
require.NoError(mt, err, "StartTransaction error")
119+
120+
ctx := mongo.NewSessionContext(ctx, sess)
121+
_, err = mt.Coll.InsertOne(ctx, bson.M{"x": 1})
122+
assert.NoError(mt, err, "InsertOne error")
123+
124+
err = sess.CommitTransaction(ctx)
125+
assert.NoError(mt, err, "CommitTransaction error")
126+
127+
sess.EndSession(ctx)
128+
}
129+
}
130+
})
102131
})
103132
}

x/mongo/driver/session/client_session.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,10 @@ func (c *Client) ClearPinnedResources() error {
331331
return nil
332332
}
333333

334-
// UnpinConnection gracefully unpins the connection associated with the session if there is one. This is done via
335-
// the pinned connection's UnpinFromTransaction function.
336-
func (c *Client) UnpinConnection() error {
334+
// unpinConnection gracefully unpins the connection associated with the session
335+
// if there is one. This is done via the pinned connection's
336+
// UnpinFromTransaction function.
337+
func (c *Client) unpinConnection() error {
337338
if c == nil || c.PinnedConnection == nil {
338339
return nil
339340
}
@@ -353,6 +354,12 @@ func (c *Client) EndSession() {
353354
return
354355
}
355356
c.Terminated = true
357+
358+
// Ignore the error when unpinning the connection because we can't do
359+
// anything about it if it doesn't work. Typically the only errors that can
360+
// happen here indicate that something went wrong with the connection state,
361+
// like it wasn't marked as pinned or attempted to return to the wrong pool.
362+
_ = c.unpinConnection()
356363
c.pool.ReturnSession(c.Server)
357364
}
358365

0 commit comments

Comments
 (0)