Skip to content

Commit

Permalink
Make HandlerBase.handlerState field atomic
Browse files Browse the repository at this point in the history
...to avoid a race condition if State is called before SetState.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Sep 11, 2024
1 parent 1f06c7b commit 8a30449
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/event/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ limitations under the License.
package event

import (
"sync/atomic"

submV1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
k8sV1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -101,23 +103,24 @@ type Handler interface {

// Base structure for event handlers that stubs out methods considered to be optional.
type HandlerBase struct {
handlerState HandlerState
handlerState atomic.Value
}

func (ev *HandlerBase) Init() error {
return nil
}

func (ev *HandlerBase) SetState(handlerState HandlerState) {
ev.handlerState = handlerState
ev.handlerState.Store(handlerState)
}

func (ev *HandlerBase) State() HandlerState {
if ev.handlerState == nil {
hs := ev.handlerState.Load()
if hs == nil {
return &DefaultHandlerState{}
}

return ev.handlerState
return hs.(HandlerState)
}

func (ev *HandlerBase) Stop() error {
Expand Down

0 comments on commit 8a30449

Please sign in to comment.