Skip to content

fix(pool): one worker IAM role per pool; drain removes it (#1147) - #1198

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1147-pool-role
Oct 20, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1147-pool-role

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Each pool now gets its own worker IAM role and instance profile, and pool drain removes them once no worker needs them.

Fixes #1147

Problem

Every pool shared the role spawn-pool-worker, and its one inline policy is scoped to a single run's queue. CreateOrGetInstanceProfile overwrites that policy, so a second pool create cut the first pool's workers off from their queue.

Approach

I used one role per pool rather than accumulating per-run statements on a shared role. Accumulating would need a read-modify-write of a shared document (which races under concurrent creates), would grow toward the 10 KB inline-policy limit, and needs per-run removal. A per-pool role keeps each grant exactly as narrow as before, and removing it is one delete.

  • Name: spawn-pool-worker-<16 hex of sha256(run id)>. A run id is uncapped and IAM role names stop at 64 characters, so the id is hashed. The role is tagged spawn:pool-run-id.
  • Cleanup (Leave no trace: nothing reaps the control plane — the lambdas, their roles/rules/alarms, or the buckets spawn auto-creates #653): pool drain deletes the role and profile only when no instance tagged with the pool's run id is pending, running, stopping or stopped. Deleting a live worker's role strips its credentials, and spored then cannot terminate it. That is worse than a leftover role. Workers terminate soon after their queue is deleted (fix(pool): workers terminate when drained; idempotent drain; --cost-limit (#1146, #1183) #1192), so drain re-checks every 10 s for up to --wait (new flag, default 2m). This matters because nf-spawn's SpawnPoolObserver drains only once, at run end, while the workers are still terminating; without the wait every nf-spawn run would leave its role behind. If workers remain after the wait, or the check itself fails, drain keeps the role and prints how to remove it (re-run spawn pool drain). spawn orphans is the backstop.
  • New Go API (additions only, snapshot regenerated): Client.DeleteInstanceProfileAndRole and Client.LiveInstancesWithTag. The second returns the describe error, where ListInstances swallows per-region errors, because an empty result here leads to a deletion.

Gates (each watched failing against a revert that compiles)

Gate Revert Result
TestConcurrentPoolCreatesKeepTheirOwnGrants: two goroutines run the real CreateOrGetInstanceProfile against a stateful, recording IAM+STS Query fake (httptest), then check each pool's profile grants only its own queue RoleName: "spawn-pool-worker" FAIL ("pool run-b's profile spawn-pool-worker grants SQS on [...run-a]")
TestDrainRemovesThePoolRoleOnlyOnceNoWorkerNeedsIt/workers_still_live len(live) > 0 && false FAIL
.../check_failed err != nil && false FAIL
TestLiveInstancesWithTagReturnsTheDescribeError swallow the error FAIL
TestDrainWaitsForWorkersBeforeRemovingTheRole: live counts 3,1,0 delete on the 3rd check after 2 sleeps; never-zero exhausts exactly --wait/10s polls; checks counted through a sleep seam waited >= 0 (no wait) / waited += 0 (unbounded; the capped fake sleep turns the hang into a failure) FAIL / FAIL

Merge note

Rebased onto main after #1192 and #1200 merged. Drain now runs: idempotent queue delete (#1192), then the role cleanup with --wait. The cleanup also runs on an "already drained" re-run, because a re-run is how a role kept for live workers gets removed. #1200's TestIdleTerminateClaimsAreTrue passes with the new --wait help.

Checks

  • make check: all steps passed except check-build-tags, which fails on the /private/tmp worktree path (known on main).
  • make check-docs: clean. semgrep --config=auto --error: clean. -race on the new tests: pass.

Not verified

Not run on AWS. Neither the deletion against real IAM nor the caller permissions drain now needs (iam:DeleteRole, iam:DeleteInstanceProfile, iam:DeleteRolePolicy, iam:DetachRolePolicy, iam:RemoveRoleFromInstanceProfile, iam:List*) has been exercised live. Accounts that already have the old shared spawn-pool-worker role keep it; it shows up in spawn orphans.

All pools shared spawn-pool-worker, whose single inline policy is scoped to
one run's queue, so a second pool create cut the first pool's workers off.
Each pool now gets spawn-pool-worker-<hash of run id>, tagged with its run
id. pool drain deletes the role and profile once no worker is pending,
running, stopping or stopped, and keeps it (saying how to remove it) while
workers remain, since deleting a live worker's role strands it.

Fixes #1147
…ng the role

nf-spawn drains once at run end, right after which the workers are still
terminating; without a bounded wait every nf-spawn run would leave its role
behind.
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.82353% with 21 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/pool.go 58.33% 13 Missing and 2 partials ⚠️
pkg/aws/cleanup.go 60.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit be14514 into main Oct 20, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1147-pool-role branch October 20, 2026 01:02
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.

spawn pool: shared spawn-pool-worker role is clobbered by a concurrent pool create

1 participant