Skip to content

Commit 27002f4

Browse files
ldanilekConvex, Inc.
authored andcommitted
avoid panic when rolled back subtransaction creates a table (#35010)
GitOrigin-RevId: 45f208df8bdd184afec49c81b953f3850dec2453
1 parent 2da951b commit 27002f4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

crates/database/src/tests/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,9 @@ async fn test_subtransaction_failure_rolls_back_table_creation(
24492449
tx.rollback_subtransaction(tokens)?;
24502450
};
24512451
assert!(!TableModel::new(&mut tx).table_exists(TableNamespace::test_user(), &table_name));
2452+
// Regression test: take_stats used to panic.
2453+
let stats = tx.take_stats();
2454+
assert!(stats.contains_key(&"_unknown".parse()?));
24522455
db.commit(tx).await?;
24532456
let mut tx = db.begin(Identity::system()).await?;
24542457
assert!(!TableModel::new(&mut tx).table_exists(TableNamespace::test_user(), &table_name));

crates/database/src/transaction.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,22 @@ impl<RT: Runtime> Transaction<RT> {
676676
.into_iter()
677677
.map(|(tablet, stats)| {
678678
(
679-
self.table_mapping()
680-
.tablet_name(tablet)
681-
.expect("tablet should exist"),
679+
match self.table_mapping().tablet_name(tablet) {
680+
Ok(name) => name,
681+
Err(_) => {
682+
// This is unusual, but possible if the tablet was created in a
683+
// subtransaction that was rolled back. Such a tablet never gets
684+
// created, but might still have usage stats.
685+
tracing::warn!("Tablet {tablet} does not exist");
686+
// It's fine to return "_unknown" here because nothing requires
687+
// these to correspond to an actual table.
688+
//
689+
// We use "_unknown" to avoid colliding with valid user table names.
690+
"_unknown"
691+
.parse()
692+
.expect("'_unknown' should be a valid table name")
693+
},
694+
},
682695
stats,
683696
)
684697
})

0 commit comments

Comments
 (0)