Skip to content

Commit ba9711f

Browse files
olavloiterahul2393
andauthored
fix(spanner): prevent possible panic for Session not found errors (#10386)
The Spanner client library could panic if specifically the following situation would occur: 1. Create a StmtBasedReadWriteTransaction 2. Only execute mutations, so that an explicit BeginTransaction RPC is required. 3. The BeginTransaction RPC returns "Session not found" 4. The client library tries to get a new session from the pool. Step 4 above would panic, because the client library did not set the readOnlyTransaction.sp field for StmtBasedReadWriteTransactions. Fixes #10385 Co-authored-by: rahul2393 <[email protected]>
1 parent 1e2af81 commit ba9711f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

spanner/client_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -3474,6 +3474,21 @@ func TestReadWriteTransaction_WrapSessionNotFoundError(t *testing.T) {
34743474
}
34753475
}
34763476

3477+
func TestStmtBasedReadWriteTransaction_SessionNotFoundError_shouldNotPanic(t *testing.T) {
3478+
t.Parallel()
3479+
server, client, teardown := setupMockedTestServer(t)
3480+
defer teardown()
3481+
server.TestSpanner.PutExecutionTime(MethodBeginTransaction,
3482+
SimulatedExecutionTime{
3483+
Errors: []error{newSessionNotFoundError("projects/p/instances/i/databases/d/sessions/s")},
3484+
})
3485+
ctx := context.Background()
3486+
tx, _ := NewReadWriteStmtBasedTransaction(ctx, client)
3487+
_ = tx.BufferWrite([]*Mutation{Update("my_table", []string{"key", "value"}, []interface{}{int64(1), "my-value"})})
3488+
// This would panic, as it could not refresh the session.
3489+
_, _ = tx.Commit(ctx)
3490+
}
3491+
34773492
func TestClient_WriteStructWithPointers(t *testing.T) {
34783493
t.Parallel()
34793494
server, client, teardown := setupMockedTestServer(t)

spanner/transaction.go

+1
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,7 @@ func NewReadWriteStmtBasedTransactionWithOptions(ctx context.Context, c *Client,
17561756
txReadyOrClosed: make(chan struct{}),
17571757
},
17581758
}
1759+
t.txReadOnly.sp = c.idleSessions
17591760
t.txReadOnly.sh = sh
17601761
t.txReadOnly.txReadEnv = t
17611762
t.txReadOnly.qo = c.qo

0 commit comments

Comments
 (0)