Skip to content

Commit

Permalink
fix(console): prevent deadlock in author new --switch (n0-computer#…
Browse files Browse the repository at this point in the history
…2032)

## Description

Fixes n0-computer#2024

This was caused by a read attempt over a lock nested inside a write
lock. Thw write lock being `let mut inner = self.0.write()` and the read
lock in `self.iroh_data_dir()`

## Notes & open questions

I checked the rest of the code in n0-computer#2013 and I think `author new
--switch` is the only command affected

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.
  • Loading branch information
divagant-martian authored and fubuloubu committed Feb 21, 2024
1 parent 9f0526c commit 1f4f087
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions iroh/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,13 @@ impl ConsoleEnv {
/// Will error if not running in the Iroh console.
/// Will persist to a file in the Iroh data dir otherwise.
pub fn set_author(&self, author: AuthorId) -> anyhow::Result<()> {
let author_path = ConsolePaths::DefaultAuthor.with_root(self.iroh_data_dir());
let mut inner = self.0.write();
if !inner.is_console {
bail!("Switching the author is only supported within the Iroh console, not on the command line");
}
inner.author = Some(author);
std::fs::write(
ConsolePaths::DefaultAuthor.with_root(self.iroh_data_dir()),
author.to_string().as_bytes(),
)?;
std::fs::write(author_path, author.to_string().as_bytes())?;
Ok(())
}

Expand Down

0 comments on commit 1f4f087

Please sign in to comment.