Skip to content

Commit

Permalink
accounts: work around non-immediate closing of sql connections
Browse files Browse the repository at this point in the history
When removing an account, try 5 times with 1 second sleep in between
in case removal of database files fails. This happens on Windows
platform sometimes due to a known bug in r2d2 [1].

[1] sfackler/r2d2#99
  • Loading branch information
link2xt committed Apr 17, 2022
1 parent e9fe8ce commit 18425b8
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,27 @@ impl Accounts {
drop(ctx);

if let Some(cfg) = self.config.get_account(id).await {
fs::remove_dir_all(async_std::path::PathBuf::from(&cfg.dir))
.await
.context("failed to remove account data")?;
let mut counter = 0;
loop {
counter += 1;

if let Err(err) = fs::remove_dir_all(async_std::path::PathBuf::from(&cfg.dir))
.await
.context("failed to remove account data")
{
// Spend up to 1 minute trying to remove the files.
// Files may remain locked up to 30 seconds due to r2d2 bug:
// https://github.com/sfackler/r2d2/issues/99
if counter > 60 {
return Err(err);
}

// Wait 1 second and try again.
async_std::task::sleep(std::time::Duration::from_millis(1000)).await;
} else {
break;
}
}
}
self.config.remove_account(id).await?;

Expand Down

0 comments on commit 18425b8

Please sign in to comment.