From 70c223045ee39d7fd93768dde33243d99a9d9605 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 10 May 2019 10:25:54 -0700 Subject: [PATCH] fix race condition in hooks --- packer/rpc/hook.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packer/rpc/hook.go b/packer/rpc/hook.go index e4e804839..c5ac329f6 100644 --- a/packer/rpc/hook.go +++ b/packer/rpc/hook.go @@ -4,6 +4,7 @@ import ( "context" "log" "net/rpc" + "sync" "github.com/hashicorp/packer/packer" ) @@ -22,6 +23,7 @@ type HookServer struct { contextCancel func() hook packer.Hook + lock sync.Mutex mux *muxBroker } @@ -67,9 +69,11 @@ func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error { } defer client.Close() + h.lock.Lock() if h.context == nil { h.context, h.contextCancel = context.WithCancel(context.Background()) } + h.lock.Unlock() if err := h.hook.Run(h.context, args.Name, client.Ui(), client.Communicator(), args.Data); err != nil { return NewBasicError(err) } @@ -79,8 +83,10 @@ func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error { } func (h *HookServer) Cancel(args *interface{}, reply *interface{}) error { + h.lock.Lock() if h.contextCancel != nil { h.contextCancel() } + h.lock.Unlock() return nil }