From e17984face88430dfa9978e4894f6cf4f6cebf02 Mon Sep 17 00:00:00 2001 From: Patrick Lucas Date: Fri, 11 Oct 2013 14:05:24 -0700 Subject: [PATCH] Fix race condition after launching EC2 instance It is possible for an instance to not immediately exist after it is launched. Previously, InstanceStateRefreshFunc would crash if this race condition were realized. This change takes the exact same approach of the function above, AMIStateRefreshFunc, treating 'InvalidInstanceID.NotFound' as if there were an empty result. --- builder/amazon/common/state.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/builder/amazon/common/state.go b/builder/amazon/common/state.go index 1e9dea36b..eaa29e92d 100644 --- a/builder/amazon/common/state.go +++ b/builder/amazon/common/state.go @@ -62,11 +62,16 @@ func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) StateRefreshFunc { return func() (interface{}, string, error) { resp, err := conn.Instances([]string{i.InstanceId}, ec2.NewFilter()) if err != nil { - log.Printf("Error on InstanceStateRefresh: %s", err) - return nil, "", err + if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" { + // Set this to nil as if we didn't find anything. + resp = nil + } else { + log.Printf("Error on InstanceStateRefresh: %s", err) + return nil, "", err + } } - if len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 { + if resp == nil || len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 { // Sometimes AWS just has consistency issues and doesn't see // our instance yet. Return an empty state. return nil, "", nil