From 499b2d8725f44506a0172a0952e6313769833efd Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Wed, 20 Mar 2019 11:01:56 +1100
Subject: [PATCH] Fix race condition in Torrent.SetDisplayName

---
 t.go       | 9 ++++++---
 torrent.go | 7 -------
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/t.go b/t.go
index c76d9273..cd8bc10c 100644
--- a/t.go
+++ b/t.go
@@ -107,9 +107,12 @@ func (t *Torrent) Seeding() bool {
 // Clobbers the torrent display name. The display name is used as the torrent
 // name if the metainfo is not available.
 func (t *Torrent) SetDisplayName(dn string) {
-	t.cl.lock()
-	defer t.cl.unlock()
-	t.setDisplayName(dn)
+	t.nameMu.Lock()
+	defer t.nameMu.Unlock()
+	if t.haveInfo() {
+		return
+	}
+	t.displayName = dn
 }
 
 // The current working name for the torrent. Either the name in the info dict,
diff --git a/torrent.go b/torrent.go
index 6f609206..c9ccaa9f 100644
--- a/torrent.go
+++ b/torrent.go
@@ -196,13 +196,6 @@ func (t *Torrent) setChunkSize(size pp.Integer) {
 	}
 }
 
-func (t *Torrent) setDisplayName(dn string) {
-	if t.haveInfo() {
-		return
-	}
-	t.displayName = dn
-}
-
 func (t *Torrent) pieceComplete(piece pieceIndex) bool {
 	return t.completedPieces.Get(bitmap.BitIndex(piece))
 }
-- 
2.51.0