Skip to content

Commit f31912d

Browse files
Merge pull request #662 from tjouni/fix/shouldretry-unnamed-parameter
Fix ShouldRetry callbacks to name error parameter for correct retry behavior
2 parents edd3438 + 6e564a2 commit f31912d

9 files changed

Lines changed: 18 additions & 8 deletions

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Latest Release
22

33
Please refer to [releases](https://github.com/hashicorp/packer-plugin-amazon/releases) for the latest CHANGELOG information.
4+
---
5+
## x.x.x (Unreleased)
6+
7+
## 🐛 Bug Fixes
8+
- **Fix ShouldRetry callbacks not receiving error from retry framework** – by @tjouni
9+
Several `ShouldRetry` callbacks had unnamed function parameters, causing them to
10+
reference the outer scope's error variable instead of the error passed by the retry
11+
framework. This prevented retries from working for transient AWS API errors such as
12+
`InvalidAMIID.NotFound`, `InvalidSnapshot.NotFound`, and `InvalidInstanceID.NotFound`.
13+
414
---
515
## 1.8.0 (November 19, 2025)
616

builder/common/step_create_tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult
102102
snapshotTags.Report(ui)
103103

104104
// Retry creating tags for about 2.5 minutes
105-
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
105+
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
106106
if awserrors.Matches(err, "InvalidAMIID.NotFound", "") || awserrors.Matches(err, "InvalidSnapshot.NotFound", "") {
107107
return true
108108
}

builder/common/step_run_source_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
364364
if s.IsRestricted {
365365
ec2Tags.Report(ui)
366366
// Retry creating tags for about 2.5 minutes
367-
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
367+
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
368368
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
369369
return true
370370
}

builder/common/step_run_spot_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
510510
// Apply tags to the spot request.
511511
err = retry.Config{
512512
Tries: 11,
513-
ShouldRetry: func(error) bool { return false },
513+
ShouldRetry: func(err error) bool { return false },
514514
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
515515
}.Run(ctx, func(ctx context.Context) error {
516516
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{

builder/common/step_stop_ebs_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.Sta
4545
// does not exist.
4646

4747
// Work around this by retrying a few times, up to about 5 minutes.
48-
err := retry.Config{Tries: 6, ShouldRetry: func(error) bool {
48+
err := retry.Config{Tries: 6, ShouldRetry: func(err error) bool {
4949
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
5050
return true
5151
}

common/step_create_tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult
106106
snapshotTags.Report(ui)
107107

108108
// Retry creating tags for about 2.5 minutes
109-
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
109+
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
110110
if awserrors.Matches(err, "InvalidAMIID.NotFound", "") || awserrors.Matches(err, "InvalidSnapshot.NotFound", "") {
111111
return true
112112
}

common/step_run_source_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
368368
if s.IsRestricted {
369369
ec2Tags.Report(ui)
370370
// Retry creating tags for about 2.5 minutes
371-
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
371+
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
372372
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
373373
return true
374374
}

common/step_run_spot_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
515515
// Apply tags to the spot request.
516516
err = retry.Config{
517517
Tries: 11,
518-
ShouldRetry: func(error) bool { return false },
518+
ShouldRetry: func(err error) bool { return false },
519519
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
520520
}.Run(ctx, func(ctx context.Context) error {
521521
_, err := ec2Client.CreateTags(ctx, &ec2.CreateTagsInput{

common/step_stop_ebs_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.Sta
4848
// does not exist.
4949

5050
// Work around this by retrying a few times, up to about 5 minutes.
51-
err := retry.Config{Tries: 6, ShouldRetry: func(error) bool {
51+
err := retry.Config{Tries: 6, ShouldRetry: func(err error) bool {
5252
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
5353
return true
5454
}

0 commit comments

Comments
 (0)