Skip to content

Commit

Permalink
accounts: retry remove_account multiple times on failure
Browse files Browse the repository at this point in the history
When removing an account, try 60 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 which may result in 30
seconds delay until all connections are closed [1].

[1] sfackler/r2d2#99
  • Loading branch information
link2xt committed Apr 17, 2022
1 parent e9fe8ce commit 494043d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Take `delete_device_after` into account when calculating ephemeral loop timeout #3211
- Fix a bug where a blocked contact could send a contact request #3218
- Try removing account folder multiple times in case of failure #3229

### Changes

Expand Down
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")?;
// 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
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")
{
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 494043d

Please sign in to comment.