]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix race in Torrent.String
authorMatt Joiner <anacrolix@gmail.com>
Tue, 12 Mar 2019 00:22:25 +0000 (11:22 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 12 Mar 2019 00:22:25 +0000 (11:22 +1100)
t.go
torrent.go

diff --git a/t.go b/t.go
index 452a717a5c4ce78d8d97dcb754dbd6aed383fc20..c76d92737a4a7310ed768717bfcf117207534634 100644 (file)
--- a/t.go
+++ b/t.go
@@ -115,8 +115,6 @@ func (t *Torrent) SetDisplayName(dn string) {
 // The current working name for the torrent. Either the name in the info dict,
 // or a display name given such as by the dn value in a magnet link, or "".
 func (t *Torrent) Name() string {
-       t.cl.lock()
-       defer t.cl.unlock()
        return t.name()
 }
 
index 92089e94f06c8e94d303101b8094d5deda21a95f..6f609206d3ebaf2dbd7f82a83a33b6e7d10caa67 100644 (file)
@@ -104,6 +104,7 @@ type Torrent struct {
 
        // Name used if the info name isn't available. Should be cleared when the
        // Info does become available.
+       nameMu      sync.RWMutex
        displayName string
 
        // The bencoded bytes of the info dict. This is actively manipulated if
@@ -260,7 +261,9 @@ func (t *Torrent) invalidateMetadata() {
        for i := range t.metadataCompletedChunks {
                t.metadataCompletedChunks[i] = false
        }
+       t.nameMu.Lock()
        t.info = nil
+       t.nameMu.Unlock()
 }
 
 func (t *Torrent) saveMetadataPiece(index int, data []byte) {
@@ -355,7 +358,9 @@ func (t *Torrent) setInfo(info *metainfo.Info) error {
                        return fmt.Errorf("error opening torrent storage: %s", err)
                }
        }
+       t.nameMu.Lock()
        t.info = info
+       t.nameMu.Unlock()
        t.displayName = "" // Save a few bytes lol.
        t.initFiles()
        t.cacheLength()
@@ -442,6 +447,8 @@ func (t *Torrent) setMetadataSize(bytes int) (err error) {
 // The current working name for the torrent. Either the name in the info dict,
 // or a display name given such as by the dn value in a magnet link, or "".
 func (t *Torrent) name() string {
+       t.nameMu.RLock()
+       defer t.nameMu.RUnlock()
        if t.haveInfo() {
                return t.info.Name
        }