1
1
mirror of https://github.com/wez/wezterm.git synced 2024-09-11 22:37:11 +03:00

clean up blob storage on shutdown

The static OnceCell didn't run destructors, so shift to something
where we can explicitly trigger them to run and clean up the
temporary directory area.
This commit is contained in:
Wez Furlong 2023-03-18 22:19:46 -07:00
parent 344a41f944
commit 4466997b88
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
2 changed files with 18 additions and 11 deletions

View File

@ -1,8 +1,8 @@
use crate::{ContentId, Error, LeaseId};
use once_cell::sync::OnceCell;
use std::io::{BufRead, Seek};
use std::sync::{Arc, Mutex};
static STORAGE: OnceCell<Box<dyn BlobStorage + Send + Sync + 'static>> = OnceCell::new();
static STORAGE: Mutex<Option<Arc<dyn BlobStorage + Send + Sync + 'static>>> = Mutex::new(None);
pub trait BufSeekRead: BufRead + Seek {}
pub type BoxedReader = Box<dyn BufSeekRead + Send + Sync>;
@ -51,16 +51,21 @@ pub trait BlobStorage {
}
pub fn register_storage(
storage: Box<dyn BlobStorage + Send + Sync + 'static>,
storage: Arc<dyn BlobStorage + Send + Sync + 'static>,
) -> Result<(), Error> {
STORAGE
.set(storage)
.map_err(|_| Error::AlreadyInitializedStorage)
STORAGE.lock().unwrap().replace(storage);
Ok(())
}
pub fn get_storage() -> Result<&'static (dyn BlobStorage + Send + Sync + 'static), Error> {
pub fn get_storage() -> Result<Arc<dyn BlobStorage + Send + Sync + 'static>, Error> {
STORAGE
.get()
.map(|s| s.as_ref())
.lock()
.unwrap()
.as_ref()
.map(|s| s.clone())
.ok_or_else(|| Error::StorageNotInit)
}
pub fn clear_storage() {
STORAGE.lock().unwrap().take();
}

View File

@ -695,7 +695,7 @@ fn run_terminal_gui(opts: StartCommand, default_domain_name: Option<String>) ->
if let Some(pos) = opts.position.as_ref() {
set_window_position(pos.clone());
}
wezterm_blob_leases::register_storage(Box::new(
wezterm_blob_leases::register_storage(Arc::new(
wezterm_blob_leases::simple_tempdir::SimpleTempDir::new()?,
))?;
@ -1204,7 +1204,9 @@ fn run() -> anyhow::Result<()> {
match sub {
SubCommand::Start(start) => {
log::trace!("Using configuration: {:#?}\nopts: {:#?}", config, opts);
run_terminal_gui(start, None)
let res = run_terminal_gui(start, None);
wezterm_blob_leases::clear_storage();
res
}
SubCommand::Ssh(ssh) => run_ssh(ssh),
SubCommand::Serial(serial) => run_serial(config, &serial),