Fix race running and closing webtorrent tracker clients

This commit is contained in:
Matt Joiner 2021-10-25 16:15:42 +11:00
parent e884b8079b
commit e178f99f35
2 changed files with 12 additions and 4 deletions

View File

@ -106,9 +106,18 @@ func (tc *TrackerClient) doWebsocket() error {
return err
}
func (tc *TrackerClient) Run() error {
// Finishes initialization and spawns the run routine, calling onStop when it completes with the
// result. We don't let the caller just spawn the runner directly, since then we can race against
// .Close to finish initialization.
func (tc *TrackerClient) Start(onStop func(error)) {
tc.pingTicker = time.NewTicker(60 * time.Second)
tc.cond.L = &tc.mu
go func() {
onStop(tc.run())
}()
}
func (tc *TrackerClient) run() error {
tc.mu.Lock()
for !tc.closed {
tc.mu.Unlock()

View File

@ -55,12 +55,11 @@ func (me *websocketTrackers) Get(url string) (*webtorrent.TrackerClient, func())
}),
},
}
go func() {
err := value.TrackerClient.Run()
value.TrackerClient.Start(func(err error) {
if err != nil {
me.Logger.Printf("error running tracker client for %q: %v", url, err)
}
}()
})
if me.clients == nil {
me.clients = make(map[string]*refCountedWebtorrentTrackerClient)
}