Skip to content

retryIAM doesn't retry ConcurrentModification, so concurrent task run launches fail on CreateInstanceProfile #648

Closed

Description

@scttfrdmn

Symptom

Two spawn task run invocations issued ~60 ms apart (a Nextflow executor fanning out two independent processes) — one succeeded, the other died at launch:

spawn task run failed (exit 1) for task 'MAFFT (1)':
create task instance profile: failed to create instance profile: operation error IAM:
CreateInstanceProfile, https response error StatusCode: 409,
RequestID: da1c3449-9c37-4f46-b762-6562074fc92c, ConcurrentModification:
The previous tagging operation is still ongoing. Please wait for a while and
perform the next tagging operation until it finishes.

spawn 0.115.0, us-west-2, via nf-spawn 0.10.1.

The mechanism is already in the right place; its predicate is one code short

cmd/task.go:519 → CreateOrGetInstanceProfile, which does wrap the call in retryIAM
(pkg/aws/iam.go:477, comment: "idempotent under concurrency — #64"). So the hardening from #64
is on this path. The gap is in what retryIAM considers retryable:

// pkg/aws/iam.go:74
func retryIAM(fn func() error) error {
	for attempt := 0; attempt < 5; attempt++ {
		err = fn()
		if err == nil || isAlreadyExists(err) { return nil }
		if !isThrottle(err) { return err }        // <-- exits here
		time.Sleep(...)
	}
}

// pkg/aws/iam.go:58
func isThrottle(err error) bool {
	code := iamErrorCode(err)
	if strings.HasPrefix(code, "Throttling") || strings.HasPrefix(code, "RequestLimitExceeded") {
		return true
	}
	...
}

ConcurrentModification is neither EntityAlreadyExists/LimitExceeded (so isAlreadyExists is
false) nor throttling (so isThrottle is false), so retryIAM returns on the first attempt
without ever sleeping. ConcurrentModification does not appear anywhere in the Go sources:

$ grep -rn "ConcurrentModification" --include="*.go" .
(no matches)

That matters because ConcurrentModification is the error IAM actually returns for racing
tagged resource creation — and CreateInstanceProfile here passes Tags, which is what makes
the implicit tagging operation serialise. Throttling is the rarer case; this is the common one.

Why it bites task run specifically

It needs two concurrent launches, which is unusual when a human runs spawn task run and is the
normal case for any workflow executor — so the Shape-F path (nf-spawn, snakemake, CWL, the
pipeline verb) hits it and the one-task-at-a-time path doesn't. It is also first-run-only: once the
profile exists, instanceProfileExists short-circuits and the window closes. So it reproduces on a
fresh profile name and then stops, which makes it easy to dismiss as a blip.

In our case one of two concurrent tasks died and Nextflow aborted the whole DAG.

Suggested fix

Add the code to the retry predicate — one line, and it keeps the existing backoff:

func isConcurrentModification(err error) bool {
	if err == nil { return false }
	return strings.HasPrefix(iamErrorCode(err), "ConcurrentModification") ||
		strings.Contains(err.Error(), "ConcurrentModification")
}

and in retryIAM: if !isThrottle(err) && !isConcurrentModification(err) { return err }.

IAM documents this as a wait-and-retry condition, and the existing 500 ms × attempt backoff over 5
attempts is the right shape for it. Happy to send the PR.

Two adjacent spots worth the same treatment

  • AddRoleToInstanceProfile at pkg/aws/iam.go:491 is inside retryIAM too, so it inherits the
    same gap on the same call path.
  • SetupSporedIAMRole (pkg/aws/iam.go:1077) does CreateRole / CreateInstanceProfile /
    AddRoleToInstanceProfile without retryIAM at all (:1101, :1134, :1145), tolerating
    only EntityAlreadyExists / LimitExceeded by string match. It is a different path from
    task run, so it is not what bit us, but it is the same concern implemented a second time — the
    duplication shape discussed in spawn service --image: run a supervised headless container on a long-running instance #620, where the hardening lands on one generator and not the
    other. Routing it through retryIAM would make it inherit the fix rather than need its own.

Secondary: a misleading error on the way out

When the launch fails, the executor's cleanup reports:

spawn terminate failed (exit 1) for instance 'nf-03461ad6fc36' in us-west-2 —
the instance may still be running and billing until its TTL. Output:
Looking up instance nf-03461ad6fc36...
no instance found with name: nf-03461ad6fc36

Nothing was leaked — the instance was never created, so there was nothing to terminate — but the
message says the opposite of that, and "may still be running and billing" is the one sentence that
makes you stop and go check. spawn terminate on a name that does not exist is arguably a success
for a caller whose goal is "ensure this is not running"; at minimum no instance found deserves to
be distinguished from a real termination failure. (Filing here since the text is spawn's; happy to
move it to nf-spawn if the wrapping message is theirs.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions