Skip to content

feat(cleanup,reaper): delete undrained pools' SQS queues and staged specs (#1351) - #1361

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1351-pool-queue-deleter
Oct 27, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1351-pool-queue-deleter

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1351

Before this, only spawn pool drain deleted a pool's SQS queue, and nothing deleted the specs spawn pool submit stages. This PR gives both a deleter. It follows the #1300 (pkg/instancerole) and #1301 (pkg/bucketsweep) pattern: one shared package, pkg/poolqueue, holds the rule, and both spawn cleanup --pool-queues and the TTL reaper (REAPER_POOL_QUEUES) use it.

Tagging facts

  • Before this PR, pkg/taskpool.CreateQueue created spawn-pool-<run id> with NO tags. Only the pool's role, profile and workers carried spawn:pool-run-id.
  • Queues are now created with Tags: {spawn:managed=true, spawn:pool-run-id=<run id>}. Tagging on create needs sqs:TagQueue. On AccessDenied, CreateQueue retries without tags, Queue.TagErr() reports why, and pool create warns that only pool drain will delete that queue.
  • Legacy untagged queues are not recognised at all. An untagged spawn-pool-* queue cannot be told apart from someone else's queue with that name, so it is reported and kept. spawn pool drain --run-id <id> deletes one. Its specs are kept too, because its queue still exists.
  • Pool create stages nothing. pool submit stages staging/pool-specs/<run id>/<task>.json in spawn-results-<acct>-<region>, which has no lifecycle rule, and pool drain never removed those objects. SpecStore now builds keys with poolqueue.SpecKey, so the prefix written and the prefix swept come from one definition.

The rule (poolqueue.Decide)

A queue is deleted only when all of these hold:

  • it is named spawn-pool-<id>, is tagged spawn:managed=true, and its spawn:pool-run-id tag equals <id>;
  • no instance (pending through stopped) in ANY enabled region holds the pool's worker profile (spawn-instance-pool-<hash>, or the pre-v0.147 spawn-pool-worker-<hash>) or carries its spawn:pool-run-id tag;
  • ApproximateNumberOfMessages and ApproximateNumberOfMessagesNotVisible are both 0. An unreadable count counts as not zero. ...Delayed is also counted when SQS returns it;
  • its CreatedTimestamp is at least 24 hours old.

A run's specs are deleted when its queue is gone (already, or in this sweep), no instance belongs to the pool, and the newest spec is at least 24 hours old. Every S3 call names the account as ExpectedBucketOwner. In report mode, a queue that reap would delete counts as gone, so report and reap give the same answer. If any region cannot be read, nothing is deleted.

Reaper mode switch

REAPER_POOL_QUEUES / template PoolQueues / make deploy POOL_QUEUES= / spawn reaper deploy --pool-queues take off|report|reap. The default is report: it logs and changes nothing, as REAPER_INSTANCE_ROLES and REAPER_EMPTY_BUCKETS do, so an upgrade deletes nothing on its own. A deploy that does not name the setting keeps the deployed value (TestPoolQueuesSurvivesAMakeDeploy, TestEveryReaperSettingSurvivesARedeploy). The setting is wired into the Makefile PARAM_MAP and awk echo list, the TestPlainDeployEmitsNoModeSettingParameter key list, reaperconfig Validate/Settable, and the reaperdeploy settings test. The sweep runs once per account after the regional scans. Its summary counters are pool_queues_*, pool_specs_* and pool_queues_accounts_denied.

IAM

The same grants are in all three copies (pkg/reaperiam ScanSelfStatements, the SAM ScanSelf branch, and the member role):

  • sqs:ListQueues on * (it has no resource ARN);
  • sqs:GetQueueAttributes, sqs:ListQueueTags, sqs:DeleteQueue on arn:aws:sqs:*:<acct>:spawn-pool-*;
  • s3:DeleteObject on arn:aws:s3:::spawn-results-*/staging/pool-specs/*. Listing uses the existing results-bucket s3:ListBucket.

DeleteQueue is scoped by name and not by tag. That follows the fsx:DeleteFileSystem precedent: nobody has proven against a live account that SQS populates aws:ResourceTag for DeleteQueue, and an unproven condition fails closed without any error. The code's tag check is the guard.

The CLI inventory is regenerated (spawn cleanup gains sqs:ListQueues, sqs:ListQueueTags, sqs:GetQueueAttributes, sqs:DeleteQueue and s3:DeleteObject). Its "implied permissions" note now names sqs:TagQueue for pool create.

Gates (each watched failing against a revert that compiles)

Revert Failed
drop the in-use check TestQueuesThatMustBeKept, cmd TestCleanupPoolQueues, reaper TestPoolQueueSweepAgreesWithTheCLI + Modes
drop the non-empty check TestQueuesThatMustBeKept, TestSpecsFollowTheirQueue
drop the 24h grace TestQueuesThatMustBeKept, reaper agreement + Modes
drop the managed-tag check / the run-id-match check TestQueuesThatMustBeKept
specs ignore a live queue / ignore the spec grace TestSpecsFollowTheirQueue
WorkersInUse ignores the run-id tag / skips an unreadable region TestWorkersInUse
Sweep ignores Delete=false TestAbandonedQueueIsDeleted
reaper run never calls the sweep all four reaper PoolQueue tests
reaper checks only REAPER_REGIONS for workers reaper agreement (deletes busy), FailsClosed, Modes
reaper ignores dry-run / ignores off TestPoolQueueSweepModes
reaper proceeds after an unreadable region TestPoolQueueSweepFailsClosedOnAnUnreadableRegion
AccessDenied counted as an error TestPoolQueueSweepDeniedIsCountedApart
unset parses to reap / template default reap TestPoolQueuesDefaultIsReport, TestReaperParsersAgreeWithReaperconfig, TestPoolQueuesSurvivesAMakeDeploy
PoolQueues:POOL_QUEUES removed from PARAM_MAP TestPoolQueuesSurvivesAMakeDeploy
CreateQueue without Tags TestCreateQueueTagsTheQueue, TestCreatedQueueIsSweptAgainstSubstrate, TestCreateQueueWithoutTagQueueStillCreatesThePool
no untagged fallback TestCreateQueueWithoutTagQueueStillCreatesThePool
SpecStore key layout changed TestStagedSpecsAreWhereTheSweepLooks
sqs:ListQueueTags dropped from reaperiam TestPolicyCoversEveryReaperAPICall, TestSQSGrantsAreScopedToPoolQueues, both template-drift gates
SQS resource widened to * TestSQSGrantsAreScopedToPoolQueues, both template-drift gates
spec delete widened to spawn-results-*/* TestS3GrantsAreScopedToSpawnsTwoBucketFamilies, both template-drift gates
sqs:ListQueueTags dropped from the member role TestCrossAccountRoleCoversEveryReaperAPICall, TestCrossAccountRolePolicyMatchesReaperIAM, iamcoverage TestEveryPrincipalCallIsGranted
CLI inventory not regenerated TestCLIActionInventoryIsCurrent

CLI and reaper agreement: pkg/poolqueue/poolqueuetest is one account scenario. It holds an abandoned queue, a queue whose only worker is in an enabled region outside the scanned ones, one holding a message, a young one, an untagged one, one tagged for another run, and the specs of an abandoned run and of a drained run. cmd.TestCleanupPoolQueues and the reaper's TestPoolQueueSweepAgreesWithTheCLI both require it to produce exactly WantDeletedQueues / WantDeletedSpecRuns.

Substrate: TestCreatedQueueIsSweptAgainstSubstrate checks that the tags CreateQueue writes come back through ListQueueTags, and exercises ListQueues and DeleteQueue against a real implementation. Substrate does not model the ApproximateNumberOfMessages* counts at all (filed as scttfrdmn/substrate#1441). The test therefore asserts that, against Substrate as-is, nothing tagged is deleted because the counts are unread. It then supplies zero counts and asserts that the abandoned queue goes and the untagged one stays. The case "a queue holding messages is kept" is covered by the fake tests only.

Checks

make check passes steps 1–6 (gofmt, vet, every GOOS, staticcheck, golangci-lint, hermetic short tests, nested modules, tidy). Step 7, check-build-tags, fails from /tmp, which is the known path issue on this machine, and passes when run from /private/tmp/spawn-1351. make lint: 0 issues in all 10 modules. semgrep scan --config=auto --error . is clean. make gen-docs and make check-docs are clean. make api-snapshot adds one line, func (*Queue) TagErr() error. Nothing is removed.

Live deploy steps (not done; no AWS calls were made)

  1. Member accounts: redeploy deployment/cloudformation/ttl-reaper-cross-account-role.yaml in every account the reaper assumes into (spore-host-dev 435415984226 and any other entry in REAPER_ROLE_ARNS). Snapshot the stack parameters before the deploy and diff them after, expecting no drift. Until a role is redeployed, the account is counted in pool_queues_accounts_denied.
  2. Reaper: redeploy spawn-ttl-reaper-production with make deploy from lambda/ttl-reaper, without naming POOL_QUEUES, so the stack picks up the template default report. Snapshot the parameters first and confirm afterwards that every other parameter is unchanged. A self-hosted reaper picks this up with spawn reaper deploy, and the scan-self policy comes from reaperiam.
  3. Before the first real run, simulate the new grants with iam simulate-principal-policy, both allow and deny: sqs:DeleteQueue on spawn-pool-x allowed and on other-queue denied; s3:DeleteObject on spawn-results-<acct>-<region>/staging/pool-specs/r/t.json allowed and on .../tasks/x denied.
  4. Read the WOULD delete pool queue lines from a report run. Arm with make deploy POOL_QUEUES=reap only after that.
  5. Not verified live: that SQS ignores tags (and does not fail) when CreateQueue names an existing queue, i.e. a reused run id, and the AccessDenied code SQS returns for a missing sqs:TagQueue.

@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 59.69499% with 185 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/poolqueue/poolqueuetest/poolqueuetest.go 0.00% 97 Missing ⚠️
pkg/poolqueue/poolqueue.go 76.82% 54 Missing ⚠️
cmd/cleanup.go 69.56% 28 Missing ⚠️
cmd/pool.go 28.57% 5 Missing ⚠️
pkg/taskpool/queue.go 93.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…pecs (#1351)

pkg/poolqueue holds the one rule, shared by `spawn cleanup --pool-queues`
and the TTL reaper (REAPER_POOL_QUEUES / PoolQueues / POOL_QUEUES /
spawn reaper deploy --pool-queues; off|report|reap, default report). A
spawn-pool-<run id> queue goes only when it is tagged spawn:managed=true
with a matching spawn:pool-run-id, no live instance in any enabled region
holds the pool's worker profile or tag, both message counts are 0, and it
is at least 24h old. A run's specs under staging/pool-specs/<run id>/ in
the results bucket go once its queue is gone, no instance is the pool's,
and the newest is 24h old. An unreadable region means nothing is deleted.

pkg/taskpool.CreateQueue now creates the queue tagged; without
sqs:TagQueue it falls back to an untagged queue and pool create warns.
Untagged (pre-v0.147.0) queues are kept. IAM in pkg/reaperiam, the SAM
ScanSelf branch and the member role: sqs:ListQueues on *, the reads and
DeleteQueue on spawn-pool-*, s3:DeleteObject on
spawn-results-*/staging/pool-specs/* only.

Fixes #1351
@scttfrdmn
scttfrdmn force-pushed the fix/1351-pool-queue-deleter branch from 7184eec to 0ab5d85 Compare October 27, 2026 04:47
@scttfrdmn
scttfrdmn merged commit c3f1fc8 into main Oct 27, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1351-pool-queue-deleter branch October 27, 2026 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leave no trace: an undrained pool's SQS queue has no deleter (#1319 follow-up)

1 participant