Skip to content

Fix context management in channelLink to prevent goroutine leaks. #9753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ type channelLink struct {
// allows contexts that either block or cancel on those depending on
// the use case.
cg *fn.ContextGuard

// cancel is the cancellation function for the context passed to
// htlcManager. It should be called when the link is stopped to clean up
// resources.
cancel context.CancelFunc
}

// hookMap is a data structure that is used to track the hooks that need to be
Expand Down Expand Up @@ -597,8 +602,13 @@ func (l *channelLink) Start() error {

l.updateFeeTimer = time.NewTimer(l.randomFeeUpdateTimeout())

// Create a background context that can be canceled when the link is
// stopped
ctx, cancel := context.WithCancel(context.Background())
l.cancel = cancel

l.cg.WgAdd(1)
go l.htlcManager(context.TODO())
go l.htlcManager(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do then?

case <-l.cg.Done():


return nil
}
Expand All @@ -623,6 +633,11 @@ func (l *channelLink) Stop() {
l.cfg.ChainEvents.Cancel()
}

// Cancel the context used by the htlcManager to clean up resources
if l.cancel != nil {
l.cancel()
}

// Ensure the channel for the timer is drained.
if l.updateFeeTimer != nil {
if !l.updateFeeTimer.Stop() {
Expand Down