Skip to content

Commit 61752bb

Browse files
authoredSep 2, 2022
fix: deep copy input to avoid race and modifying caller's input (#2451)
1 parent f7298f7 commit 61752bb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed
 

‎pkg/cloudprovider/aws/createfleetbatcher.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
package aws
1616

1717
import (
18+
"bytes"
1819
"context"
20+
"encoding/json"
1921
"fmt"
2022
"sync"
2123
"time"
@@ -132,8 +134,16 @@ func (b *CreateFleetBatcher) runCalls() {
132134
// we know that these create fleet calls are identical so we can just run the first one and increase the number
133135
// of instances that we request
134136
call := requestBatch[0]
135-
call.input.TargetCapacitySpecification.SetTotalTargetCapacity(int64(len(requestBatch)))
136-
outputs, err := b.ec2api.CreateFleetWithContext(call.ctx, call.input)
137+
// deep copy the input we are about to modify so that we don't modify any caller's input parameter
138+
input, err := deepCopy(call.input)
139+
if err != nil {
140+
// shouldn't occur, but if it does we log an error and just modify the caller's input so we
141+
// can continue to launch instances
142+
logging.FromContext(context.Background()).Infof("error copying input, %s", err)
143+
input = call.input
144+
}
145+
input.TargetCapacitySpecification.SetTotalTargetCapacity(int64(len(requestBatch)))
146+
outputs, err := b.ec2api.CreateFleetWithContext(call.ctx, input)
137147

138148
// error occurred at the CreateFleet call level, so notify all requestors of the same error
139149
if err != nil {
@@ -195,3 +205,17 @@ func (b *CreateFleetBatcher) runCalls() {
195205
}
196206
}
197207
}
208+
209+
func deepCopy(v *ec2.CreateFleetInput) (*ec2.CreateFleetInput, error) {
210+
var buf bytes.Buffer
211+
enc := json.NewEncoder(&buf)
212+
if err := enc.Encode(v); err != nil {
213+
return nil, err
214+
}
215+
dec := json.NewDecoder(&buf)
216+
var cp ec2.CreateFleetInput
217+
if err := dec.Decode(&cp); err != nil {
218+
return nil, err
219+
}
220+
return &cp, nil
221+
}

0 commit comments

Comments
 (0)
Please sign in to comment.