Skip to content
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

Wait for cleanup on certexchange server shutdown #430

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Changes from 1 commit
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
40 changes: 37 additions & 3 deletions certexchange/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"errors"
"fmt"
"runtime/debug"
"sync"
"time"

"github.com/filecoin-project/go-f3/certstore"
Expand All @@ -28,7 +29,8 @@
Host host.Host
Store *certstore.Store

cancel context.CancelFunc
runningLk sync.RWMutex
stopFunc context.CancelFunc
}

func (s *Server) withDeadline(ctx context.Context) (context.Context, context.CancelFunc) {
Expand Down Expand Up @@ -111,9 +113,25 @@

// Start the server.
func (s *Server) Start() error {
s.runningLk.Lock()
defer s.runningLk.Unlock()
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
if s.stopFunc != nil {
return fmt.Errorf("certificate exchange already running")

Check warning on line 119 in certexchange/server.go

View check run for this annotation

Codecov / codecov/patch

certexchange/server.go#L119

Added line #L119 was not covered by tests
}

ctx, cancel := context.WithCancel(context.Background())
s.cancel = cancel
s.stopFunc = cancel
s.Host.SetStreamHandler(FetchProtocolName(s.NetworkName), func(stream network.Stream) {
s.runningLk.RLock()
defer s.runningLk.RUnlock()
if s.stopFunc == nil {
_ = stream.Reset()
return

Check warning on line 129 in certexchange/server.go

View check run for this annotation

Codecov / codecov/patch

certexchange/server.go#L128-L129

Added lines #L128 - L129 were not covered by tests
}

// Kill the stream if/when we shutdown the server.
defer context.AfterFunc(ctx, func() { _ = stream.Reset() })()

ctx, cancel := s.withDeadline(ctx)
defer cancel()

Expand All @@ -129,7 +147,23 @@

// Stop the server.
func (s *Server) Stop() error {
// Ask the handlers to cancel/stop.
s.runningLk.RLock()
cancel := s.stopFunc
s.runningLk.RUnlock()
if cancel == nil {
return nil
}
cancel()

// Wait and finish shutdown.
s.runningLk.Lock()
defer s.runningLk.Unlock()
if s.stopFunc == nil {
return nil

Check warning on line 163 in certexchange/server.go

View check run for this annotation

Codecov / codecov/patch

certexchange/server.go#L163

Added line #L163 was not covered by tests
}
s.stopFunc = nil
s.Host.RemoveStreamHandler(FetchProtocolName(s.NetworkName))
s.cancel()

return nil
}
Loading