Skip to content

fix(cleanup): make --mine mean "not someone else's", and correct two placement-group bugs (#708, #713) - #714

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/708-mine-scope-shared-infra
Oct 5, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/708-mine-scope-shared-infra

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #708. Closes #713.

Option (b) from #708, as decided. The second half is a correction to something I shipped in v0.121.0 — detail below, because one of the bugs was being hidden by the other.

#708 — --mine was a blackhole, not a filter

// before
if opts.OnlyMine && tags["spawn:iam-user"] != callerARN { continue }

spawn:iam-user is written by the instance tag builder. Shared infrastructure never carries it, and a missing tag reads as "", which never equals a caller ARN. So for those classes the filter did not narrow — it emptied:

$ spawn orphans --region us-east-1
No orphaned spawn-managed resources in us-east-1.
$ spawn orphans --region us-east-1 --all
27 resource(s)      # oldest from 2026-07-19

The rule is now "not someone else's", not "provably mine". Untagged is treated as shared rather than foreign for two reasons: it is the only choice that helps the resources already out there — stamping the tag on new ones would leave today's orphans invisible forever — and "mine" is a dubious notion for a security group deliberately shared and reused by name across runs, which is exactly what #685's MPI group is. It also matches what scanAddresses and scanPlacementGroups already do, neither of which applies the filter because neither resource carries the tag.

orphans now also prints how many resources the scope excluded, so "nothing here" can never again be the whole output when it is not the whole story.

Verified live: 0 → 25 reported by default.

#713 — and then the dry run showed me two bugs I had shipped

Running spawn cleanup --dry-run against a real account to check the above:

us-east-1  ec2  placement-group  pg-040e64301b7f056f7             -      -
us-east-1  ec2  placement-group  spawn-mpi-smoke22812-us-east-1a  empty  -
...
us-east-1  iam  instance-profile  spored-instance-profile          -      -
us-east-1  iam  instance-profile  spored-instance-role             -      -

I was wrong that RGT does not return placement groups

I claimed in #706 — in the PR, the commit, and the v0.121.0 changelog — that the Resource Groups Tagging API does not return placement groups, and built a dedicated scan on that premise. It returns them keyed by group ID:

$ aws resourcegroupstaggingapi get-resources --tag-filters Key=spawn:managed,Values=true \
    --query 'ResourceTagMappingList[?contains(ResourceARN,`placement-group`)].ResourceARN'
arn:aws:ec2:us-east-1:435415984226:placement-group/pg-040e64301b7f056f7
...

The probe I relied on returned empty because the account had zero placement groups at the time, so it could not distinguish an unsupported type from an absent one. I noted that ambiguity and asserted the strong version anyway. That is the mistake — the code was a reasonable response to a premise I should not have adopted.

Two consequences. Every group was reported twice (six rows, three groups). And the id-keyed copy cannot be deleted: DeletePlacementGroup takes a name, so pg-… fails InvalidPlacementGroup.Unknown — which the ignoreNotFound I added then treats as success. A cleanup in v0.121.0 can report removing a placement group it never touched. The tolerant error check turning a real failure into a clean bill of health is the worse half of this.

RGT's placement-group rows are now dropped in favour of the dedicated scan, which supplies both things RGT lacks: the name needed to delete, and whether the group still has members. Scoped to that one type, with a test asserting nothing else is suppressed — hiding a type with no replacement scan would be #708 all over again.

Fixing #708 unmasked an over-eager IAM classification

spored-instance-role and spored-instance-profile are created once and reused by every launch. IsLikelyOrphan's only signal for IAM is !hasRunningInstance, so an idle account made them orphans — and cleanup --yes would have deleted spawn's own identity.

Latent, not new: the broken scope had been hiding them from cleanup entirely, so one bug was concealing the other. The same change that surfaced 27 genuine orphans also made these two deletable. They are now excluded from both the orphan check and cleanup's removable set, reported with the reason instead.

Per-run spawn-instance-<hash> profiles must stay reclaimable — there are 12 in the dev account — so there is a test in that direction too, because a guard that over-reached would strand them forever.

Verified

before after
orphans reported by default 0 25
placement-group rows 6 (3 real) 3
shared IAM offered for deletion 2 0

Every gate confirmed to fail when its fix is reverted — including the direction tests (another principal's resource still excluded; per-run IAM still reclaimable), since an over-correction here is as bad as the original.

One consequence worth naming

This makes spawn cleanup meaningfully more dangerous than it was, because it now works. In an idle account a dry run offers 44 resources where it previously offered the handful that carried the instance tag. The confirmation prompt and --dry-run were nice-to-haves while the scope was broken; they are now the only thing between a user and 44 deletions. I have not changed those defaults, but it is worth a look.

…placement-group bugs (#708, #713)

--- #708

--mine filtered on spawn:iam-user, a tag pkg/aws/tags.go writes only to instances
and volumes. Shared infrastructure — security groups, IAM roles and instance
profiles, key pairs, log groups, tables — never carries it, and a missing tag reads
as "", which never equals a caller ARN. So for those classes the filter did not
narrow the result, it EMPTIED it:

  $ spawn orphans --region us-east-1
  No orphaned spawn-managed resources in us-east-1.
  $ spawn orphans --region us-east-1 --all
  27 resource(s)

"Nothing to clean" is a believed answer, and it was wrong for as long as those
resources existed — the oldest dates from 2026-07-19.

The rule is now "not someone else's" rather than "provably mine". Untagged is
treated as shared rather than foreign for two reasons: it is the only choice that
helps the resources ALREADY out there, since stamping the tag on new resources
would leave today's orphans invisible forever; and "mine" is a dubious notion for a
security group deliberately shared and reused by name across runs and principals,
which is exactly what #685's MPI group is. It also matches what the two separately
scanned classes already do — neither addresses nor placement groups apply the
filter, because neither carries the tag.

orphans now reports how many resources the scope excluded, so "nothing here" can
never again be the whole output when it is not the whole story.

--- #713, found by running cleanup --dry-run against a real account

Two bugs, both mine, and the first corrects a claim I made in #706.

I asserted there that the Resource Groups Tagging API does not return placement
groups, and built a dedicated scan on that premise. It is false — RGT returns them
keyed by group ID. The probe I relied on returned empty because the account had
zero placement groups at the time, so it could not distinguish an unsupported type
from an absent one. I noted that ambiguity and then asserted the strong version
anyway; that is the actual mistake.

So every managed group was reported twice — once by id with no state, once by name
with one. And the id-keyed copy could not be deleted: EC2's DeletePlacementGroup
takes a NAME, so pg-… fails InvalidPlacementGroup.Unknown, which the ignoreNotFound
I added then swallowed as SUCCESS. A cleanup in v0.121.0 can report removing a
placement group it never touched. A tolerant error check turned a real failure into
a clean bill of health, which is the worse half.

RGT's placement-group rows are now dropped in favour of the dedicated scan, which
supplies both things RGT lacks: the name needed to delete and whether the group
still has members. Narrowly scoped to that one type, with a test asserting nothing
else is suppressed — hiding a type with no replacement scan would be #708 again.

Second: fixing #708 unmasked an over-eager IAM classification. spored-instance-role
and spored-instance-profile are created once and reused by every launch, but the
only signal IsLikelyOrphan has for IAM is !hasRunningInstance — so an idle account
made them orphans and `cleanup --yes` would have deleted spawn's own identity. The
broken scope had been hiding them from cleanup entirely, so one bug was concealing
the other. Now excluded from both the orphan check and cleanup's removable set, and
reported with the reason; per-run spawn-instance-<hash> profiles stay reclaimable,
with a test for that direction too.

Verified on live data: placement-group rows 6 → 3, total orphans 27 → 25. Every
gate confirmed to fail when its fix is reverted.
The MPI leg creates a managed security group and a per-AZ placement group, and
teardown terminated the instances and left both behind on every run. Three of each
accumulated over one session — a smoke whose purpose is catching leaks was a source
of them.

Found by 'spawn orphans' once #708 made the default scope work, which is a pointed
way to learn it:

  ec2  placement-group  spawn-mpi-smoke22812-us-east-1a  empty
  ec2  placement-group  spawn-mpi-smoke85337-us-east-1a  empty
  ec2  placement-group  spawn-mpi-smoke87201-us-east-1a  empty

Teardown now removes both, retrying the placement-group delete for exactly the
reason spawn itself has to (#685): TerminateInstances is asynchronous and EC2
refuses to delete a group that still has members.

The post-run leak check also asked only about INSTANCES, so it printed 'no instances
left behind' over that pile every time. It now checks placement groups and security
groups too, and names 'spawn orphans' in the failure so the next step is obvious.
@scttfrdmn
scttfrdmn merged commit 624b211 into main Oct 5, 2026
6 checks passed
@scttfrdmn
scttfrdmn deleted the fix/708-mine-scope-shared-infra branch October 5, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant