Skip to content

Commit dba04d5

Browse files
raulbhariso
andauthored
adds a mutex to ctx cancel (#1909)
Co-authored-by: Haris Osmanagić <[email protected]>
1 parent 3df0511 commit dba04d5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Diff for: pkg/lifecycle/stream/destination_acker.go

+8
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ type DestinationAckerNode struct {
3232

3333
// queue is used to store messages
3434
queue deque.Deque[*Message]
35+
3536
// m guards access to queue
3637
m sync.Mutex
3738

39+
// mctx guards access to the contextCtxCancel function
40+
mctx sync.Mutex
41+
3842
base subNodeBase
3943
logger log.CtxLogger
4044

@@ -49,7 +53,9 @@ func (n *DestinationAckerNode) Run(ctx context.Context) (err error) {
4953
// start a fresh connector context to make sure the connector is running
5054
// until this method returns
5155
var connectorCtx context.Context
56+
n.mctx.Lock()
5257
connectorCtx, n.connectorCtxCancel = context.WithCancel(context.Background())
58+
n.mctx.Unlock()
5359
defer n.connectorCtxCancel()
5460

5561
// signalChan is buffered to ensure signals don't get lost if worker is busy
@@ -226,5 +232,7 @@ func (n *DestinationAckerNode) SetLogger(logger log.CtxLogger) {
226232

227233
func (n *DestinationAckerNode) ForceStop(ctx context.Context) {
228234
n.logger.Warn(ctx).Msg("force stopping destination acker node")
235+
n.mctx.Lock()
229236
n.connectorCtxCancel()
237+
n.mctx.Unlock()
230238
}

Diff for: pkg/lifecycle/stream/source.go

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type SourceNode struct {
4747
state csync.ValueWatcher[nodeState]
4848
connectorCtxCancel context.CancelFunc
4949

50+
// mctx guards access to the connector context
51+
mctx sync.Mutex
52+
5053
stop struct {
5154
sync.Mutex
5255
position opencdc.Position
@@ -102,7 +105,10 @@ func (n *SourceNode) Run(ctx context.Context) (err error) {
102105
// start a fresh connector context to make sure the connector is running
103106
// until this method returns
104107
var connectorCtx context.Context
108+
109+
n.mctx.Lock()
105110
connectorCtx, n.connectorCtxCancel = context.WithCancel(context.Background())
111+
n.mctx.Unlock()
106112
defer n.connectorCtxCancel()
107113

108114
// openMsgTracker tracks open messages until they are acked or nacked
@@ -241,7 +247,9 @@ func (n *SourceNode) stopGraceful(ctx context.Context, reason error) (err error)
241247

242248
func (n *SourceNode) ForceStop(ctx context.Context) {
243249
n.logger.Warn(ctx).Msg("force stopping source connector")
250+
n.mctx.Lock()
244251
n.connectorCtxCancel()
252+
n.mctx.Unlock()
245253
}
246254

247255
func (n *SourceNode) Pub() <-chan *Message {

0 commit comments

Comments
 (0)