Skip to content

Commit

Permalink
Merge pull request #34 from ngrok-oss/euan/close-race
Browse files Browse the repository at this point in the history
Correctly lock fds when closing old listeners
  • Loading branch information
euank authored Sep 27, 2024
2 parents 9f958fe + 7e329fb commit 7f6ed0d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions fds.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var (
// This state is terminal.
// This error will be returned if an atttempt is made to mutate the file
// descriptor store after stopping the upgrader.
ErrUpgraderStopped = errors.New("the upgrader has been marked as stopped")
ErrUpgraderStopped = errors.New("the upgrader has been marked as stopped")
ErrClosingListeners = errors.New("the upgrade completed, and listeners are being cleaned up")
)

// Listener can be shared between processes.
Expand Down Expand Up @@ -131,7 +132,7 @@ type Fds struct {
l log15.Logger
}

func (f *Fds) String() string { // XXX here?
func (f *Fds) String() string {
fds := f.copy()
res := make([]string, 0, len(fds))
for _, fi := range fds {
Expand Down Expand Up @@ -463,6 +464,9 @@ func (f *Fds) copy() map[string]*fd {
// closeUnused closes unused FDs. It should be called
// while Fds is locked.
func (f *Fds) closeUnused() error {
f.mu.Lock()
defer f.mu.Unlock()

var errs []error
for name, fd := range f.fds {
if !fd.used {
Expand Down
6 changes: 3 additions & 3 deletions upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func (u *Upgrader) Ready() error {
}

// Now cleanup all old FDs while holding the lock
//u.Fds.lockMutations(errors.New("closing old listeners"))
// defer u.Fds.unlockMutations()
//_ = u.Fds.closeUnused()
u.Fds.lockMutations(ErrClosingListeners)
defer u.Fds.unlockMutations()
_ = u.Fds.closeUnused()

return nil
}
Expand Down

0 comments on commit 7f6ed0d

Please sign in to comment.