Skip to content

Commit

Permalink
Torrent.AddPieceLayers: Fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Mar 22, 2024
1 parent ce807f1 commit 51ad9aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
t.maybeNewConns()
t.dataDownloadDisallowed.SetBool(spec.DisallowDataDownload)
t.dataUploadDisallowed = spec.DisallowDataUpload
return errors.Join(t.AddPieceLayers(spec.PieceLayers)...)
return errors.Join(t.addPieceLayersLocked(spec.PieceLayers)...)
}

func (cl *Client) dropTorrent(t *Torrent, wg *sync.WaitGroup) (err error) {
Expand Down
8 changes: 7 additions & 1 deletion torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (t *Torrent) makePieces() {
}
}

func (t *Torrent) AddPieceLayers(layers map[string]string) (errs []error) {
func (t *Torrent) addPieceLayersLocked(layers map[string]string) (errs []error) {
if layers == nil {
return
}
Expand Down Expand Up @@ -476,6 +476,12 @@ files:
return
}

func (t *Torrent) AddPieceLayers(layers map[string]string) (errs []error) {
t.cl.lock()
defer t.cl.unlock()
return t.addPieceLayersLocked(layers)
}

// Returns the index of the first file containing the piece. files must be
// ordered by offset.
func pieceFirstFileIndex(pieceOffset int64, files []*File) int {
Expand Down

0 comments on commit 51ad9aa

Please sign in to comment.