Skip to content

Commit c0023ee

Browse files
authored
fix: close iterator on error (#644)
When there's an error inside ReadWriteTransactionWithOptions it's not quite clear whether iterator will be automatically closed.
1 parent 2311ffb commit c0023ee

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

conn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,10 @@ func queryInNewRWTransaction(ctx context.Context, c *spanner.Client, statement s
16191619
var result *wrappedRowIterator
16201620
options.QueryOptions.LastStatement = true
16211621
fn := func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
1622+
if result != nil {
1623+
// in case of a retry
1624+
result.Stop()
1625+
}
16221626
it := tx.QueryWithOptions(ctx, statement, options.QueryOptions)
16231627
row, err := it.Next()
16241628
if err == iterator.Done {
@@ -1641,6 +1645,9 @@ func queryInNewRWTransaction(ctx context.Context, c *spanner.Client, statement s
16411645
}
16421646
resp, err := c.ReadWriteTransactionWithOptions(ctx, fn, options.TransactionOptions)
16431647
if err != nil {
1648+
if result != nil {
1649+
result.Stop()
1650+
}
16441651
return nil, nil, err
16451652
}
16461653
return result, &resp, nil

0 commit comments

Comments
 (0)