Skip to content

Commit 4d90cb5

Browse files
authored
Merge pull request #5363 from cloudflare/jmp/fix-clang-tidy-miserror
Refactor SqliteKv’s multi-put to silence an errant clang-tidy lint
2 parents c01de04 + 738c069 commit 4d90cb5

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/workerd/util/sqlite-kv.c++

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,15 @@ void SqliteKv::beforeSqliteReset() {
171171
tableCreated = false;
172172
}
173173

174+
void SqliteKv::rollbackMultiPut(Initialized& stmts, WriteOptions options) {
175+
KJ_IF_SOME(e, kj::runCatchingExceptions([&]() {
176+
// This should be rare, so we don't prepare a statement for it.
177+
stmts.db.run({.regulator = stmts.regulator, .allowUnconfirmed = options.allowUnconfirmed},
178+
kj::str("ROLLBACK TO _cf_put_multiple_savepoint"));
179+
stmts.stmtMultiPutRelease.run({.allowUnconfirmed = options.allowUnconfirmed});
180+
})) {
181+
KJ_LOG(WARNING, "silencing exception encountered while rolling back multi-put", e);
182+
}
183+
}
184+
174185
} // namespace workerd

src/workerd/util/sqlite-kv.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ class SqliteKv: private SqliteDatabase::ResetListener {
177177
// first write.
178178

179179
void beforeSqliteReset() override;
180+
181+
// Helper function that rolls back a multi-put statement and swallows any exceptions that may
182+
// occur during the rollback.
183+
void rollbackMultiPut(Initialized& stmts, WriteOptions options);
180184
};
181185

182186
// Iterator over list results.
@@ -272,20 +276,13 @@ void SqliteKv::put(ArrayOfKeyValuePair& pairs, WriteOptions options) {
272276
// general structure can be shared somehow?
273277
auto& stmts = ensureInitialized(options.allowUnconfirmed);
274278
stmts.stmtMultiPutSavepoint.run({.allowUnconfirmed = options.allowUnconfirmed});
275-
for (const auto& pair: pairs) {
276-
try {
279+
280+
{
281+
// If any of the puts throw an exception, rollback the transaction and re-throw the exception
282+
// from the put that failed.
283+
KJ_ON_SCOPE_FAILURE(rollbackMultiPut(stmts, options));
284+
for (const auto& pair: pairs) {
277285
put(pair.key, pair.value, {.allowUnconfirmed = options.allowUnconfirmed});
278-
} catch (...) {
279-
try {
280-
// This should be rare, so we don't prepare a statement for it.
281-
stmts.db.run({.regulator = stmts.regulator, .allowUnconfirmed = options.allowUnconfirmed},
282-
kj::str("ROLLBACK TO _cf_put_multiple_savepoint"));
283-
stmts.stmtMultiPutRelease.run({.allowUnconfirmed = options.allowUnconfirmed});
284-
} catch (...) {
285-
auto e = kj::getCaughtExceptionAsKj();
286-
KJ_LOG(WARNING, "silencing exception encountered while rolling back multi-put", e);
287-
}
288-
throw;
289286
}
290287
}
291288
stmts.stmtMultiPutRelease.run({.allowUnconfirmed = options.allowUnconfirmed});

0 commit comments

Comments
 (0)