Skip to content

Commit

Permalink
Fix active phase context lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Dec 4, 2023
1 parent 723c824 commit e5b0a76
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions pkg/tbtc/coordination.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,22 @@ func (ce *coordinationExecutor) coordinate(

execLogger.Info("actions checklist is: [%v]", actionsChecklist)

// Set up a context that is cancelled when the active phase of the
// coordination window ends.
// Set up a context that is automatically cancelled when the active phase
// of the coordination window ends.
//
// The coordination leader keeps that context active for the lifetime of the
// active phase to provide retransmissions of the coordination message thus
// maximize the chance that all followers receive it on time. The only case
// when the leader cancels the context prematurely is when the leader's
// routine fails.
//
// The coordination follower cancels the context as soon as it receives
// the coordination message.
ctx, cancelCtx := withCancelOnBlock(
context.Background(),
window.activePhaseEndBlock(),
ce.waitForBlockFn,
)
defer cancelCtx()

var proposal CoordinationProposal
var faults []*coordinationFault
Expand All @@ -396,6 +404,10 @@ func (ce *coordinationExecutor) coordinate(
actionsChecklist,
)
if err != nil {
// Cancel the context upon leader's routine failure. There is
// no point to keep the context active as retransmissions do not
// occur anyway.
cancelCtx()
return nil, fmt.Errorf(
"failed to execute leader's routine: [%v]",
err,
Expand All @@ -406,6 +418,9 @@ func (ce *coordinationExecutor) coordinate(
} else {
execLogger.Info("executing follower's routine")

// Cancel the context upon follower's routine completion.
defer cancelCtx()

proposal, faults, err = ce.executeFollowerRoutine(
ctx,
leader,
Expand Down
2 changes: 1 addition & 1 deletion pkg/tbtc/coordination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ func TestCoordinationExecutor_ExecuteFollowerRoutine_WithIdleLeader(t *testing.T

provider := netlocal.Connect()

broadcastChannel, err := provider.BroadcastChannelFor("test")
broadcastChannel, err := provider.BroadcastChannelFor("test-idle")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit e5b0a76

Please sign in to comment.