fix purge

This commit is contained in:
(null)
2023-07-24 02:09:54 -04:00
parent 5d4e949e76
commit ca4d77ecd2
6 changed files with 26 additions and 12 deletions

View File

@@ -63,11 +63,10 @@ impl<W: Write + Send + Sync> PtyForwarder<W> {
listener: UnixListener,
mut command: Command,
output_log: W,
) -> PtyForwarder<W> {
let pty_result = openpty(None, None).unwrap();
let maybe_child = command.pty(&pty_result).spawn();
let child = maybe_child.unwrap();
PtyForwarder {
) -> std::io::Result<PtyForwarder<W>> {
let pty_result = openpty(None, None)?;
let child = command.pty(&pty_result).spawn()?;
Ok(PtyForwarder {
child,
listener,
clients: Vec::new(),
@@ -75,7 +74,7 @@ impl<W: Write + Send + Sync> PtyForwarder<W> {
ingress: Vec::new(),
egress: Buffer::new(),
output_log,
}
})
}
pub fn pid(&self) -> u32 {

View File

@@ -935,7 +935,7 @@ fn main() -> Result<(), ActionError> {
_ = attach::run(socket);
}
let exit = n.notified_sync_take_value();
std::process::exit(exit.unwrap_or(1) as i32)
std::process::exit(exit.unwrap_or(2) as i32 - 1)
}
}
};

View File

@@ -158,7 +158,8 @@ pub(super) fn spawn_process_pty(
.map_err(|err| ExecError::CannotOpenLogFile(log_path_str.to_string(), err))?;
let listener = std::os::unix::net::UnixListener::bind(socket_path.as_ref())
.map_err(ExecError::CannotBindUnixSocket)?;
let forwarder = PtyForwarder::from_command(listener, cmd, file);
let forwarder =
PtyForwarder::from_command(listener, cmd, file).map_err(ExecError::CannotSpawn)?;
let pid = forwarder.pid();
std::thread::spawn(move || {
// XXX

View File

@@ -72,7 +72,9 @@ impl ProcessRunnerStat {
true
});
if let Some(notify) = &self.exit_notify {
notify.clone().notify_waiters_with_value(exit_code as u64);
notify
.clone()
.notify_waiters_with_value(exit_code as u64 + 1);
}
}
@@ -87,7 +89,9 @@ impl ProcessRunnerStat {
.borrow()
.exit_code
.expect("The entire tree exited but not the process itself?!");
notify.clone().notify_waiters_with_value(exit_code as u64);
notify
.clone()
.notify_waiters_with_value(exit_code as u64 + 1);
}
}
}

View File

@@ -199,6 +199,8 @@ impl ServerContext {
// XXX: Potential race condition when trying to import/commit/pull images during purge
pub(crate) async fn purge_images(&self) -> anyhow::Result<()> {
info!("begin purge");
let config = self.config();
let layers_dir = &config.layers_dir;
let im = self.image_manager.read().await;
@@ -231,6 +233,8 @@ impl ServerContext {
.and_then(|s| s.parse::<ChainId>().ok())
});
// eprintln!("chain_ids: {chain_ids:#?}");
let mut file_set: std::collections::HashSet<OciDigest> =
std::collections::HashSet::from_iter(files.into_iter());
let mut chain_id_set: std::collections::HashSet<ChainId> =
@@ -250,6 +254,7 @@ impl ServerContext {
}
if !chain_id_set.is_empty() {
if let Some(cid) = record.manifest.chain_id() {
info!("keep: {cid}, wanted by: {}:{}", record.name, record.tag);
chain_id_set.remove(&cid);
let props = zfs.get_props(format!("{}/{cid}", config.image_dataset))?;
let mut origin_chain = None;
@@ -259,6 +264,7 @@ impl ServerContext {
.split_once('@')
.and_then(|(_, c)| ChainId::from_str(c).ok())
{
info!("keep: {c}, referenced by {cid}");
chain_id_set.remove(&c);
origin_chain = Some(c);
}
@@ -271,14 +277,16 @@ impl ServerContext {
}
for garbage in file_set.iter() {
info!("removing orphaned layer: {garbage}");
std::fs::remove_file(format!("{layers_dir}/{garbage}"))?;
}
for chain_id in chain_id_set.iter() {
info!("destroying ZFS dataset: {chain_id}");
_ = zfs.destroy(
format!("{}/{chain_id}", config.image_dataset),
false,
false,
true,
false,
);
}

View File

@@ -513,7 +513,9 @@ async fn purge(
local_context: &mut ConnectionContext<Variables>,
request: (),
) -> GenericResult<()> {
context.read().await.purge_images().await.unwrap();
if let Err(error) = context.read().await.purge_images().await {
error!("purge error: {error:#?}");
}
Ok(())
}
#[derive(Serialize, Deserialize, Debug)]