Skip to content

Commit 2813bdf

Browse files
committed
GODRIVER-2867 Unpin connections when ending a session. (#1330)
1 parent ad89f0c commit 2813bdf

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

mongo/integration/load_balancer_prose_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
"testing"
1414
"time"
1515

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

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)