From 7460a5e359d2d210cfb6edbec2a3f0d8f1578eb5 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 22 Mar 2024 15:22:56 +1100 Subject: [PATCH] Torrent.AddPieceLayers: Fix data race --- client.go | 2 +- torrent.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 5e635f27..66abeb84 100644 --- a/client.go +++ b/client.go @@ -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) { diff --git a/torrent.go b/torrent.go index d1f4a646..39bffd77 100644 --- a/torrent.go +++ b/torrent.go @@ -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 } @@ -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 { -- 2.44.0