Skip to content

Commit 381a13c

Browse files
committed
world/world.go: Protect viewers with mutex.
Resolves #967.
1 parent a8687b0 commit 381a13c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

server/world/loader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func (l *Loader) ChangeWorld(tx *Tx, new *World) {
5353
}
5454
})
5555
clear(l.loaded)
56+
l.w.viewerMu.Lock()
5657
delete(l.w.viewers, l)
58+
l.w.viewerMu.Unlock()
5759

5860
l.world(new)
5961
}
@@ -130,7 +132,10 @@ func (l *Loader) Close(tx *Tx) {
130132
tx.World().removeViewer(tx, pos, l)
131133
}
132134
l.loaded = map[ChunkPos]*Column{}
135+
136+
l.w.viewerMu.Lock()
133137
delete(l.w.viewers, l)
138+
l.w.viewerMu.Unlock()
134139

135140
l.closed = true
136141
l.viewer = nil

server/world/world.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ type World struct {
6666
scheduledUpdates *scheduledTickQueue
6767
neighbourUpdates []neighbourUpdate
6868

69-
viewers map[*Loader]Viewer
69+
viewerMu sync.Mutex
70+
viewers map[*Loader]Viewer
7071
}
7172

7273
// transaction is a type that may be added to the transaction queue of a World.
@@ -1023,6 +1024,9 @@ func (w *World) close() {
10231024
// allViewers returns all viewers and loaders, regardless of where in the world
10241025
// they are viewing.
10251026
func (w *World) allViewers() ([]Viewer, []*Loader) {
1027+
w.viewerMu.Lock()
1028+
defer w.viewerMu.Unlock()
1029+
10261030
viewers, loaders := make([]Viewer, 0, len(w.viewers)), make([]*Loader, 0, len(w.viewers))
10271031
for k, v := range w.viewers {
10281032
viewers = append(viewers, v)
@@ -1034,7 +1038,10 @@ func (w *World) allViewers() ([]Viewer, []*Loader) {
10341038
// addWorldViewer adds a viewer to the world. Should only be used while the
10351039
// viewer isn't viewing any chunks.
10361040
func (w *World) addWorldViewer(l *Loader) {
1041+
w.viewerMu.Lock()
10371042
w.viewers[l] = l.viewer
1043+
w.viewerMu.Unlock()
1044+
10381045
l.viewer.ViewTime(w.Time())
10391046
w.set.Lock()
10401047
raining, thundering := w.set.Raining, w.set.Raining && w.set.Thundering

0 commit comments

Comments
 (0)