Skip to content

Commit 37e4fa3

Browse files
committed
fix(spanner): prevent leaked emulator transactions
1 parent 5a45fb5 commit 37e4fa3

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Spanner/src/Database.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,17 @@ public function runTransaction(callable $operation, array $options = []): mixed
964964
$this->isRunningTransaction = true;
965965
try {
966966
$res = call_user_func($operation, $transaction);
967+
} catch (\Throwable $e) {
968+
$active = $transaction->state() === Transaction::STATE_ACTIVE;
969+
$singleUse = $transaction->type() === Transaction::TYPE_SINGLE_USE;
970+
if ($active && !$singleUse) {
971+
try {
972+
$transaction->rollback($options);
973+
} catch (\Throwable $rollbackException) {
974+
// ignore rollback failure and bubble up the original exception
975+
}
976+
}
977+
throw $e;
967978
} finally {
968979
$this->isRunningTransaction = false;
969980
}

Spanner/tests/System/ReadTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ public function testLockHintReadWriteTransaction()
262262
$rows = iterator_to_array($res->rows());
263263
$this->assertNotEmpty($rows);
264264
$this->assertEquals($limit, count($rows));
265+
266+
$res->transaction()->rollback();
265267
}
266268

267269
public function testLockHintOnReadOnlyThrowsAnError()

0 commit comments

Comments
 (0)