Skip to content

Commit b6879cd

Browse files
committed
ci(cleanup): count the nested retain's deferred roots apart from the serial sweep's
The Amplify step now deletes one sandbox at a time (#1255) and counts the sandboxes it defers to the next run in DEFERRED. This PR counted the roots it defers after starting a nested retain in a variable of the same name, so both summary lines printed the sum. It now has its own counter, NESTED_DEFERRED. The nested retain stays behind the one-at-a-time gate, so it never starts while an earlier sandbox's delete may still be running, and its nested delete is not awaited: the model stack holds no API Gateway resource, so it doesn't compete for the REST API delete quota the wait protects. A comment in the step says so. Tests: a stuck and a normal sandbox in one run (the stuck root is deferred on its own line, the normal one deleted and awaited), and no nested delete once the wait budget is spent or while an earlier delete is still running. The step tests use the step's poll override, so they take seconds, not minutes.
1 parent e3f3e90 commit b6879cd

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎.github/workflows/cleanup-stacks.yml‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ jobs:
399399
# at 40 min and defer the remainder to the next scheduled run.
400400
DEADLINE=$((SECONDS + 2400)) # 40 minutes
401401
# Of that, at most 15 minutes waiting on deletes (see the delete loop).
402-
# The wait settings are overridable for scripts/ci/amplify-sweep.test.mjs.
402+
# The wait settings are overridable for the step tests
403+
# (scripts/ci/amplify-sweep.test.mjs, amplify-stuck-delete.test.mjs).
403404
WAIT_DEADLINE=$((SECONDS + ${AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS:-900}))
404405
PER_STACK_WAIT=${AMPLIFY_SWEEP_STACK_WAIT_SECONDS:-360}
405406
POLL=${AMPLIFY_SWEEP_POLL_SECONDS:-15}
@@ -412,7 +413,7 @@ jobs:
412413
DEFERRED=0
413414
BLOCKED=""
414415
TRIGGER_FAILED=0
415-
DEFERRED=0
416+
NESTED_DEFERRED=0
416417
SCRIPT_FAILED=0
417418
RETAINED_FILE=$(mktemp)
418419
@@ -547,6 +548,14 @@ jobs:
547548
# for deletion by hand, on this run and every later one (--leftovers
548549
# below). The script runs from the job's checkout, which includes
549550
# scripts/ci on every leg.
551+
#
552+
# It runs behind the one-at-a-time gate above, so it never starts while
553+
# an earlier root delete may still be running. Its own nested delete is
554+
# not awaited (so it can overlap a later root delete, or another nested
555+
# one): the model stack holds the AppSync resolvers, IAM roles and the
556+
# table, but no API Gateway resource, so it doesn't compete for the REST
557+
# API delete quota the wait protects. The root, whose `blocks` nested
558+
# stack holds the REST API, is deleted (and awaited) on the next run.
550559
echo "🧹 Emptying nested buckets for $STACK"
551560
_VISITED_STACKS=""
552561
empty_stack_buckets "$STACK"
@@ -557,7 +566,7 @@ jobs:
557566
if [ "$RC" -eq 3 ]; then
558567
# Not deleting the root now: its delete would race the nested one.
559568
echo "Nested stack delete started for $STACK; root delete deferred to the next run"
560-
DEFERRED=$((DEFERRED + 1))
569+
NESTED_DEFERRED=$((NESTED_DEFERRED + 1))
561570
continue
562571
elif [ "$RC" -ne 0 ]; then
563572
echo "::warning::amplify-stuck-delete.sh exited $RC for $STACK; plain retry only"
@@ -606,7 +615,7 @@ jobs:
606615
echo "- Deleted (confirmed): $DELETED" | tee -a "$GITHUB_STEP_SUMMARY"
607616
echo "- Deferred to the next run: $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
608617
echo "- Delete-stack call failed: $TRIGGER_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
609-
echo "- Root delete deferred (nested stack delete started): $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
618+
echo "- Root delete deferred (nested stack delete started): $NESTED_DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
610619
echo "- Nested retain script failed: $SCRIPT_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
611620
echo "- Still DELETE_FAILED: $STILL_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
612621
# What amplify-stuck-delete.sh retained, this run or earlier (with the
@@ -627,7 +636,7 @@ jobs:
627636
echo "" | tee -a "$GITHUB_STEP_SUMMARY"
628637
echo "_Each delete is awaited (bounded) before the next; stacks that don't finish (commonly a throttled REST API delete, or the Amplify managed DynamoDB table) are retried on the next scheduled run._" | tee -a "$GITHUB_STEP_SUMMARY"
629638
if [ "$STILL_FAILED" -gt 0 ]; then
630-
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
639+
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($NESTED_DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
631640
fi
632641
633642
- name: Cleanup orphaned API Gateway REST APIs

‎scripts/ci/amplify-stuck-delete.test.mjs‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,17 @@ function stepScript() {
243243
* Runs the step against `files`, with the root listed, tagged and `status`.
244244
* `cwd` replaces the repo root the step runs scripts/ci/ from.
245245
*/
246-
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd } = {}) {
246+
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd, env = {} } = {}) {
247247
const dir = fixture({
248248
'stacks.txt': `${ROOT}\n`,
249249
[`info-${key(ROOT)}.txt`]: `sandbox\t${status}\t${created}\n`,
250250
...files,
251251
});
252252
const file = join(dir, 'step.sh');
253253
writeFileSync(file, stepScript());
254-
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '' }, cwd);
254+
// The step waits for each plain delete (polling describe-stacks); no real
255+
// sleeps in tests.
256+
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '', AMPLIFY_SWEEP_POLL_SECONDS: '0', ...env }, cwd);
255257
}
256258

257259
const NO_ACCOUNT = /123456789012/;
@@ -807,6 +809,52 @@ describe(`cleanup-stacks.yml "${STEP}" step`, { skip: !GNU_DATE && 'needs GNU da
807809
assert.doesNotMatch(r.summary, NO_ACCOUNT);
808810
});
809811

812+
// The step deletes one sandbox at a time and defers the rest once its wait
813+
// budget is spent or a delete is still running. The nested retain runs behind
814+
// that gate, and its deferred roots are counted on their own line.
815+
const NORMAL = 'amplify-amplifygen2-amp716639251-sandbox-1111111111';
816+
const two = (over = {}) =>
817+
sandbox({
818+
'stacks.txt': `${ROOT}\n${NORMAL}\n`,
819+
[`info-${key(NORMAL)}.txt`]: 'sandbox\tROLLBACK_COMPLETE\t2026-10-19T14:48:22.894Z\n',
820+
...over,
821+
});
822+
823+
it('defers a stuck root on its own count, then deletes a normal sandbox and waits for it', () => {
824+
const r = runStep(two());
825+
assert.equal(r.status, 0, r.stdout);
826+
assert.deepEqual(r.deletes, [RETAIN_TODO, `cloudformation delete-stack --stack-name ${NORMAL}`]);
827+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 1$/m);
828+
assert.match(r.summary, /^- Deferred to the next run: 0$/m);
829+
assert.match(r.summary, /^- Deleted \(confirmed\): 1$/m);
830+
});
831+
832+
it('starts no nested delete once the wait budget is spent', () => {
833+
const r = runStep(two(), { env: { AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS: '0' } });
834+
assert.equal(r.status, 0, r.stdout);
835+
assert.deepEqual(r.deletes, []);
836+
assert.match(r.summary, /^- Deferred to the next run: 2$/m);
837+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
838+
});
839+
840+
it('starts no nested delete while an earlier sandbox is still deleting', () => {
841+
// The normal sandbox comes first, and every poll in its 1-second wait
842+
// reads DELETE_IN_PROGRESS.
843+
const r = runStep(
844+
two({
845+
'stacks.txt': `${NORMAL}\n${ROOT}\n`,
846+
[`status-${key(NORMAL)}.txt`]: 'DELETE_IN_PROGRESS\n',
847+
}),
848+
{ env: { AMPLIFY_SWEEP_STACK_WAIT_SECONDS: '1', AMPLIFY_SWEEP_POLL_SECONDS: '0.2' } },
849+
);
850+
assert.equal(r.status, 0, r.stdout);
851+
assert.match(r.calls, new RegExp(`describe-stacks --stack-name ${NORMAL} --query Stacks\\[0\\]\\.StackStatus`));
852+
assert.match(r.stdout, new RegExp(`Still deleting: ${NORMAL}`));
853+
assert.deepEqual(r.deletes, [`cloudformation delete-stack --stack-name ${NORMAL}`]);
854+
assert.match(r.summary, /^- Deferred to the next run: 1$/m);
855+
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
856+
});
857+
810858
it('only retries plainly a ROLLBACK_FAILED sandbox (the retain needs DELETE_FAILED)', () => {
811859
const r = runStep(sandbox(), { status: 'ROLLBACK_FAILED' });
812860
assert.equal(r.status, 0, r.stdout);

0 commit comments

Comments
 (0)