Skip to content

Commit

Permalink
Remove relevant webtorrent offers when closing Torrent
Browse files Browse the repository at this point in the history
(cherry picked from commit 73a0b5e4d2fe679aac31d87171537c70f91f46ee)
  • Loading branch information
anacrolix committed Jul 12, 2022
1 parent f120b93 commit aedf258
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
13 changes: 8 additions & 5 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Torrent struct {
userOnWriteChunkErr func(error)

closed chansync.SetOnce
onClose []func()
infoHash metainfo.Hash
pieces []Piece

Expand Down Expand Up @@ -865,6 +866,9 @@ func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
err = errors.New("already closed")
return
}
for _, f := range t.onClose {
f()
}
if t.storage != nil {
wg.Add(1)
go func() {
Expand Down Expand Up @@ -1614,11 +1618,10 @@ func (t *Torrent) runHandshookConnLoggingErr(pc *PeerConn) {
}

func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
wtc, release := t.cl.websocketTrackers.Get(u.String())
go func() {
<-t.closed.Done()
release()
}()
wtc, release := t.cl.websocketTrackers.Get(u.String(), t.infoHash)
// This needs to run before the Torrent is dropped from the Client, to prevent a new webtorrent.TrackerClient for
// the same info hash before the old one is cleaned up.
t.onClose = append(t.onClose, release)
wst := websocketTrackerStatus{u, wtc}
go func() {
err := wtc.Announce(tracker.Started, t.infoHash)
Expand Down
11 changes: 11 additions & 0 deletions webtorrent/tracker-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ func (tc *TrackerClient) closeUnusedOffers() {
tc.outboundOffers = nil
}

func (tc *TrackerClient) CloseOffersForInfohash(infoHash [20]byte) {
tc.mu.Lock()
defer tc.mu.Unlock()
for key, offer := range tc.outboundOffers {
if offer.infoHash == infoHash {
offer.peerConnection.Close()
delete(tc.outboundOffers, key)
}
}
}

func (tc *TrackerClient) Announce(event tracker.AnnounceEvent, infoHash [20]byte) error {
metrics.Add("outbound announces", 1)
var randOfferId [20]byte
Expand Down
3 changes: 2 additions & 1 deletion wstracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type websocketTrackers struct {
Proxy http.ProxyFunc
}

func (me *websocketTrackers) Get(url string) (*webtorrent.TrackerClient, func()) {
func (me *websocketTrackers) Get(url string, infoHash [20]byte) (*webtorrent.TrackerClient, func()) {
me.mu.Lock()
defer me.mu.Unlock()
value, ok := me.clients[url]
Expand Down Expand Up @@ -74,6 +74,7 @@ func (me *websocketTrackers) Get(url string) (*webtorrent.TrackerClient, func())
return &value.TrackerClient, func() {
me.mu.Lock()
defer me.mu.Unlock()
value.TrackerClient.CloseOffersForInfohash(infoHash)
value.refCount--
if value.refCount == 0 {
value.TrackerClient.Close()
Expand Down

0 comments on commit aedf258

Please sign in to comment.