Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- **`rm` and `kill` remove a workspace's compose volumes when its containers are gone.**
A workspace whose containers were already gone (`NotFound` in `dl --ls`) left its
`<uid>_bazel-output` and every other compose volume behind. `kill` now finds a compose
workspace's running containers by devpod's uid instead of by the workspace id. See
[Every other volume only the workspace used](docs/cleanup.md#every-other-volume-only-the-workspace-used)
for which volumes go.

## [0.64.2] - 2026-10-31

### Fixed
Expand Down
23 changes: 20 additions & 3 deletions docs/cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,24 @@ hold:
- **No container that is left uses it.** dl asks Docker after the workspace's own
containers are gone, so a volume another workspace still mounts stays.
- **It is the workspace's own.** Compose made it for the workspace's project under
compose's default name, `<project>_<volume>`, or its name holds the workspace id or
devpod's uid as a whole part, as `<id>-pixi` does.
compose's default name, `<project>_<volume>`, or its name holds one of devpod's uids
for the workspace as a whole part, as `dind-var-lib-docker-<uid>` does. A name that
holds the workspace id as a whole part, as `<id>-pixi` does, counts only when a
container of devpod's current uid mounts it and dl read devpod's create result for
that uid: only that result says which names the config spells literally.

A compose volume is also found by its label, not only by what mounts it. A volume compose
made for one of the workspace's projects under the default name `<project>_<volume>` is
the workspace's own even when no container is left to mount it, as when `dl --ls` shows
the workspace `NotFound`. The workspace's projects are devpod's current uid for it, every
older uid dl kept from an earlier `up` (devpod makes a record again under a new uid, and
the old project's volumes keep the old one), and the project of each leftover container
the delete removes that devpod made for this workspace. dl keeps the older uids by
workspace id alone, so when two devpod contexts hold the same id, dl reads none of them:
an older uid may be the other context's workspace. A leftover project somebody
started by hand in the clone is named after its folder, and `<folder>_<volume>` is shared
by every folder of that name, so its volumes stay. A volume of the workspace's own that stays because another container
still uses it is named on stderr, with the command that finds that container.

Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
Two kinds of volume are shared on purpose and always stay: a name the devcontainer config
spells literally, with no `${...}` in it (`team-tracker-pixi`), and a volume compose made
Expand All @@ -86,7 +102,8 @@ Keeping a volume can be undone; deleting a shared one cannot. A volume created b
compose ran, and not marked external, has no compose label: it stays only when its name
holds neither the workspace id nor devpod's uid. Each volume removed is named on stderr. A volume Docker will not
remove is a line, never a failed delete. A delete that the guard refuses, that devpod
refuses, or a `kill` whose `devpod delete` times out, removes no volume. `stop`, `up`, `recreate` and `--refresh-stale` never
refuses, or a `kill` whose `devpod delete` times out, removes no volume, except the
compose volumes of leftover containers the delete itself removed. `stop`, `up`, `recreate` and `--refresh-stale` never
run this, so the caches a recreate keeps stay.

### The volumes of a workspace devpod has already forgotten
Expand Down
6 changes: 6 additions & 0 deletions rust/devlaunch-core/public-api.api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ pub devlaunch_core::api::LifecycleNotice::VolumesNotRemoved
pub devlaunch_core::api::LifecycleNotice::VolumesNotRemoved::occasion: devlaunch_core::flows::lifecycle::SweepOccasion
pub devlaunch_core::api::LifecycleNotice::VolumesNotRemoved::refusal: devlaunch_core::flows::lifecycle::VolumeRefusal
pub devlaunch_core::api::LifecycleNotice::VolumesNotRemoved::workspace_id: alloc::string::String
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeInUse
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeInUse::name: alloc::string::String
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeInUse::workspace_id: alloc::string::String
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeNotRemoved
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeNotRemoved::name: alloc::string::String
pub devlaunch_core::api::LifecycleNotice::WorkspaceVolumeNotRemoved::reason: alloc::string::String
Expand Down Expand Up @@ -900,6 +903,9 @@ pub devlaunch_core::flows::lifecycle::LifecycleNotice::VolumesNotRemoved
pub devlaunch_core::flows::lifecycle::LifecycleNotice::VolumesNotRemoved::occasion: devlaunch_core::flows::lifecycle::SweepOccasion
pub devlaunch_core::flows::lifecycle::LifecycleNotice::VolumesNotRemoved::refusal: devlaunch_core::flows::lifecycle::VolumeRefusal
pub devlaunch_core::flows::lifecycle::LifecycleNotice::VolumesNotRemoved::workspace_id: alloc::string::String
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeInUse
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeInUse::name: alloc::string::String
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeInUse::workspace_id: alloc::string::String
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeNotRemoved
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeNotRemoved::name: alloc::string::String
pub devlaunch_core::flows::lifecycle::LifecycleNotice::WorkspaceVolumeNotRemoved::reason: alloc::string::String
Expand Down
12 changes: 11 additions & 1 deletion rust/devlaunch-core/src/clients/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ pub(crate) struct ComposeContainer {
pub(crate) working_dir: Option<PathBuf>,
/// `com.docker.compose.project.config_files`, split on its commas.
pub(crate) config_files: Vec<PathBuf>,
/// `com.docker.compose.project`: the project compose made it for, which names
/// the compose volumes the project made (`<project>_<volume>`).
pub(crate) project: Option<String>,
}

impl ComposeContainer {
Expand All @@ -229,7 +232,8 @@ pub(crate) enum Unlisted {
/// line per container.
const COMPOSE_FORMAT: &str = "{{.ID}}\t{{.Names}}\t\
{{.Label \"com.docker.compose.project.working_dir\"}}\t\
{{.Label \"com.docker.compose.project.config_files\"}}";
{{.Label \"com.docker.compose.project.config_files\"}}\t\
{{.Label \"com.docker.compose.project\"}}";

/// Every container on this machine that a compose project made, running or not.
pub(crate) fn compose_containers(runner: &dyn Runner) -> Result<Vec<ComposeContainer>, Unlisted> {
Expand Down Expand Up @@ -547,11 +551,17 @@ fn compose_container(line: &str) -> Option<ComposeContainer> {
.filter(|file| !file.is_empty())
.map(PathBuf::from)
.collect();
let project = fields
.next()
.map(str::trim)
.filter(|project| !project.is_empty())
.map(str::to_owned);
Some(ComposeContainer {
id: id.to_owned(),
name: name.to_owned(),
working_dir,
config_files,
project,
})
}

Expand Down
128 changes: 124 additions & 4 deletions rust/devlaunch-core/src/flows/kept_copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ use crate::domain::workspace_state::NonEmpty;
/// conditions, and a reader of either can see at a glance which it is holding.
const COPIES_DIR: &str = "workspace-copies";

/// The docker-in-docker feature's volume name, before the uid devpod substitutes.
const DIND_PREFIX: &str = "dind-var-lib-docker-";

/// Where devlaunch keeps its copies of what devpod substituted.
///
/// A path parameter rather than a read of the process environment, for
Expand Down Expand Up @@ -122,13 +125,24 @@ impl KeptCopies {
let Ok(bytes) = std::fs::read(&result) else {
return;
};
let recorded = parse_result(&bytes);
let mut recorded = parse_result(&bytes);
if recorded.is_empty() {
// Nothing devpod substituted and no image it named: a document this
// could not read is a document it copies nothing out of, which leaves
// the workspace exactly where it was before the copy existed.
// the workspace exactly where it was before the copy existed. Asked
// before the kept uids join it, so they cannot make it look worth
// writing over the copy that is there.
return;
}
// The uids of every earlier record go first: devpod forgets them when it
// makes a record again, and their compose volumes still carry them.
let mut uids = self.uids(workspace_id);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
for uid in recorded.uids.drain(..) {
if !uids.contains(&uid) {
uids.push(uid);
}
}
recorded.uids = uids;
let Ok(text) = serde_json::to_string(&recorded) else {
return;
};
Expand All @@ -147,6 +161,31 @@ impl KeptCopies {
NonEmpty::of(read_copy(&self.copy_path(workspace_id))?.volumes)
}

/// Every uid devpod gave this workspace, oldest first, or nothing where there
/// is no copy to read.
///
/// devpod names the compose project after the uid, so each one names the
/// compose volumes a record of this workspace made. A copy written before uids
/// were kept still names its last one, inside the dind volume name devpod
/// substituted it into — a value devpod wrote down, not a guess.
pub(crate) fn uids(&self, workspace_id: &str) -> Vec<String> {
let Some(copy) = read_copy(&self.copy_path(workspace_id)) else {
return Vec::new();
};
let mut uids = copy.uids;
for uid in copy
.volumes
.iter()
.filter_map(|name| name.strip_prefix(DIND_PREFIX))
.filter(|uid| !uid.is_empty())
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
{
if !uids.iter().any(|known| known == uid) {
uids.push(uid.to_owned());
}
}
uids
}

/// Every workspace this cache holds a copy for, sorted.
///
/// The reclaim's domain, and it is deliberately not a walk of the clone
Expand Down Expand Up @@ -213,12 +252,21 @@ struct Copy {
/// stays distinguishable from one devpod derived; a devcontainer built from a
/// Dockerfile declares none.
declared_image: Option<String>,
/// Every `SubstitutionContext.DevContainerID` a completed `up` recorded, oldest
/// first. devpod names the compose project after it, so it is what names the
/// compose volumes of a record devpod has since made again or forgotten.
/// `default` so a copy written before this field still reads.
#[serde(default)]
uids: Vec<String>,
}

impl Copy {
/// Whether this copy would say nothing at all, in which case none is written.
fn is_empty(&self) -> bool {
self.volumes.is_empty() && self.image.is_none() && self.declared_image.is_none()
self.volumes.is_empty()
&& self.image.is_none()
&& self.declared_image.is_none()
&& self.uids.is_empty()
}
}

Expand All @@ -237,8 +285,10 @@ fn parse_result(bytes: &[u8]) -> Copy {
let Ok(document) = serde_json::from_slice::<serde_json::Value>(bytes) else {
return Copy::default();
};
let substitutions = substitutions_in(&document);
Copy {
volumes: substitutions_in(&document).volume_names(),
volumes: substitutions.volume_names(),
uids: substitutions.devcontainer_id.clone().into_iter().collect(),
image: non_empty_string(&document["ContainerDetails"]["Config"]["Image"]),
declared_image: non_empty_string(&document["MergedConfig"]["image"]),
}
Expand Down Expand Up @@ -451,6 +501,76 @@ mod tests {
assert_eq!(copies.copied(), Vec::<String>::new());
}

/// devpod's uid for the workspace is kept too: it is the compose project
/// name, so it names every compose volume the workspace made.
#[test]
fn a_completed_up_keeps_the_workspaces_uid() {
let (_dir, copies) = a_cache();
let home = a_completed_up("myws", "/repos/o/r/repo-main-ab12", "default-my-aaaaa");

copies.keep("myws", Some(&home));

assert_eq!(copies.uids("myws"), ["default-my-aaaaa"]);
}

/// A record devpod made again gets a new uid, and the old project's volumes
/// still carry the old one. The copy keeps both, oldest first, so a later
/// delete can still name the old project's volumes.
#[test]
fn a_new_uid_does_not_drop_the_old_one() {
let (_dir, copies) = a_cache();
let old = a_completed_up("myws", "/repos/o/r/repo-main-ab12", "default-my-aaaaa");
copies.keep("myws", Some(&old));
let new = a_completed_up("myws", "/repos/o/r/repo-main-ab12", "default-my-bbbbb");
copies.keep("myws", Some(&new));
copies.keep("myws", Some(&new));

assert_eq!(
copies.uids("myws"),
["default-my-aaaaa", "default-my-bbbbb"]
);
assert_eq!(
names(&copies, "myws"),
[
"repo-main-ab12-pixi",
"dind-var-lib-docker-default-my-bbbbb"
]
);
}

/// A later result that names nothing devpod substituted leaves the copy as
/// it was. Before, the kept uids alone made it look like a copy worth
/// writing, and it replaced the volume names with nothing.
#[test]
fn a_result_that_names_nothing_leaves_the_copy_with_uids_alone() {
let (_dir, copies) = a_cache();
let up = a_completed_up("myws", "/repos/o/r/repo-main-ab12", "default-my-aaaaa");
copies.keep("myws", Some(&up));
let before = (copies.uids("myws"), names(&copies, "myws"));
let unreadable = a_result("myws", serde_json::json!({}));
copies.keep("myws", Some(&unreadable));

assert_eq!((copies.uids("myws"), names(&copies, "myws")), before);
assert!(!before.1.is_empty());
}

/// A copy written before uids were kept still names its uid, in the dind
/// volume name devpod substituted it into.
#[test]
fn a_copy_from_before_uids_were_kept_names_its_uid_through_the_dind_volume() {
let (dir, copies) = a_cache();
let store = dir.path().join(COPIES_DIR);
std::fs::create_dir_all(&store).expect("a store");
std::fs::write(
store.join("legacy.json"),
r#"{"volumes":["repo-pixi","dind-var-lib-docker-default-re-ccccc"],"image":null,"declared_image":null}"#,
)
.expect("a legacy copy");

assert_eq!(copies.uids("legacy"), ["default-re-ccccc"]);
assert_eq!(copies.uids("never-written"), Vec::<String>::new());
}

/// The image reference rides on the same copy, read from the same document at
/// the same moment (devlaunch#450), and the two references stay apart: what the
/// container ran, and what the devcontainer named.
Expand Down
41 changes: 38 additions & 3 deletions rust/devlaunch-core/src/flows/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::clients::devpod_home::{self as devpod_home, DevpodHome};
use crate::clients::docker;
use crate::clients::kill::{self as signals, Sent, Signal};
use crate::clients::ps;
use crate::flows::workspace_volumes;

pub use crate::clients::ps::HostProcess;
use crate::domain::workspace_state::NonEmpty;
Expand Down Expand Up @@ -759,7 +760,14 @@ pub fn workspace_kill(
Killed::Swept(Sweep {
signalled,
marker: sweep_marker(devpod_home, workspace_id, &holding),
containers: kill_containers(runner, workspace_id, &holding),
containers: kill_containers(
runner,
// devpod names the compose project after its uid, not the id. With
// no uid on record, the id is the only name left to try.
&workspace_volumes::workspace_uid(devpod_home, workspace_id)
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
.unwrap_or_else(|| workspace_id.to_owned()),
&holding,
),
holding,
})
}
Expand Down Expand Up @@ -903,11 +911,11 @@ fn sweep_holders(
///
/// The listing comes first because a project with nothing running is the common
/// case and a `docker kill` with no arguments is an error rather than a no-op.
fn kill_containers(runner: &dyn Runner, workspace_id: &str, holding: &Holding) -> Containers {
fn kill_containers(runner: &dyn Runner, project: &str, holding: &Holding) -> Containers {
if holding.any_attended() {
return Containers::LeftForALiveBuild;
}
let ids = match docker::running_for_project(runner, workspace_id) {
let ids = match docker::running_for_project(runner, project) {
docker::Running::These(ids) => ids,
docker::Running::NotInstalled => return Containers::NoDocker,
docker::Running::Refused { exit, stderr } => {
Expand Down Expand Up @@ -1809,6 +1817,33 @@ mod tests {
);
}

/// devpod names the compose project after its uid for the workspace, not
/// after the workspace id. So the containers are found by the uid: a filter
/// on the id matched no compose container devpod ever made.
#[test]
fn the_containers_are_found_by_the_compose_project_devpod_named() {
let home = home_for("my-ws");
std::fs::write(
home.record("default", "my-ws"),
serde_json::json!({ "id": "my-ws", "uid": "default-my-65da1" }).to_string(),
)
.expect("a record");
let fake = host_showing(NOTHING);
fake.script(["docker", "ps"], Response::stdout("abc123\n"));

let sweep = swept(workspace_kill(&fake, Some(&home), "my-ws", &mut |_| {}));

assert_eq!(
sweep.containers,
Containers::Killed(vec!["abc123".to_owned()])
);
let ps = &fake.args_to("docker")[0];
assert!(
ps.contains(&"label=com.docker.compose.project=default-my-65da1".to_owned()),
"{ps:?}"
);
}

/// The issue's own case: the container had exited 137 a minute before the
/// restart, so a `docker kill` would have found nothing to kill. Nothing to
/// kill is a finding, and it points at the process rather than the container.
Expand Down
12 changes: 7 additions & 5 deletions rust/devlaunch-core/src/flows/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,17 +1341,19 @@ fn sweep_leftovers(
workspace_id,
clone,
) {
OrphanSweep::Removed(names) => notices.say(LaunchNotice::LeftoverContainersRemoved {
workspace_id: workspace_id.to_owned(),
names,
}),
OrphanSweep::Removed { names, .. } => {
notices.say(LaunchNotice::LeftoverContainersRemoved {
workspace_id: workspace_id.to_owned(),
names,
})
}
OrphanSweep::NotListed { reason } => {
notices.say(LaunchNotice::LeftoverContainersNotListed {
workspace_id: workspace_id.to_owned(),
reason,
})
}
OrphanSweep::NotRemoved { names, reason } => {
OrphanSweep::NotRemoved { names, reason, .. } => {
notices.say(LaunchNotice::LeftoverContainersNotRemoved {
workspace_id: workspace_id.to_owned(),
names,
Expand Down
Loading
Loading