Skip to content

Commit e47c67e

Browse files
committed
Simplify transaction rollback logic
If the attempt to rollback the transaction fails, with Diesel 2.0 it is now okay to drop the connection as it will be closed instead of being returned to the pool.
1 parent 1f55fac commit e47c67e

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/swirl/runner.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,17 @@ impl Runner {
172172
// TODO: Replace with flatten() once that stabilizes
173173
.and_then(std::convert::identity);
174174

175+
// If the job panics it could leave the connection inside an inner transaction(s).
176+
// Attempt to roll those back so we can mark the job as failed, but if the rollback
177+
// fails then there isn't much we can do at this point so return early. `r2d2` will
178+
// detect the bad state and drop it from the pool.
175179
loop {
176-
let depth = get_transaction_depth(conn);
177-
if depth == Ok(initial_depth) {
180+
let depth = get_transaction_depth(conn)?;
181+
if depth == initial_depth {
178182
break;
179183
}
180184
warn!("Rolling back a transaction due to a panic in a background task");
181-
match <AnsiTransactionManager as TransactionManager<PgConnection>>::rollback_transaction(conn)
182-
{
183-
Ok(_) => (),
184-
Err(e) => {
185-
error!("Leaking a thread and database connection because of an error while rolling back transaction: {e}");
186-
loop {
187-
std::thread::sleep(Duration::from_secs(24 * 60 * 60));
188-
error!("How am I still alive?");
189-
}
190-
}
191-
}
185+
AnsiTransactionManager::rollback_transaction(conn)?;
192186
}
193187

194188
match result {
@@ -246,7 +240,7 @@ impl Runner {
246240
}
247241

248242
fn get_transaction_depth(conn: &mut PgConnection) -> QueryResult<u32> {
249-
let transaction_manager = <AnsiTransactionManager as TransactionManager<PgConnection>>::transaction_manager_status_mut(conn);
243+
let transaction_manager = AnsiTransactionManager::transaction_manager_status_mut(conn);
250244
Ok(transaction_manager
251245
.transaction_depth()?
252246
.map(u32::from)

0 commit comments

Comments
 (0)