Skip to content

Commit

Permalink
world/world.go: Protect viewers with mutex.
Browse files Browse the repository at this point in the history
Resolves #967.
  • Loading branch information
Sandertv committed Dec 23, 2024
1 parent a8687b0 commit 381a13c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions server/world/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func (l *Loader) ChangeWorld(tx *Tx, new *World) {
}
})
clear(l.loaded)
l.w.viewerMu.Lock()
delete(l.w.viewers, l)
l.w.viewerMu.Unlock()

l.world(new)
}
Expand Down Expand Up @@ -130,7 +132,10 @@ func (l *Loader) Close(tx *Tx) {
tx.World().removeViewer(tx, pos, l)
}
l.loaded = map[ChunkPos]*Column{}

l.w.viewerMu.Lock()
delete(l.w.viewers, l)
l.w.viewerMu.Unlock()

l.closed = true
l.viewer = nil
Expand Down
9 changes: 8 additions & 1 deletion server/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type World struct {
scheduledUpdates *scheduledTickQueue
neighbourUpdates []neighbourUpdate

viewers map[*Loader]Viewer
viewerMu sync.Mutex
viewers map[*Loader]Viewer
}

// transaction is a type that may be added to the transaction queue of a World.
Expand Down Expand Up @@ -1023,6 +1024,9 @@ func (w *World) close() {
// allViewers returns all viewers and loaders, regardless of where in the world
// they are viewing.
func (w *World) allViewers() ([]Viewer, []*Loader) {
w.viewerMu.Lock()
defer w.viewerMu.Unlock()

viewers, loaders := make([]Viewer, 0, len(w.viewers)), make([]*Loader, 0, len(w.viewers))
for k, v := range w.viewers {
viewers = append(viewers, v)
Expand All @@ -1034,7 +1038,10 @@ func (w *World) allViewers() ([]Viewer, []*Loader) {
// addWorldViewer adds a viewer to the world. Should only be used while the
// viewer isn't viewing any chunks.
func (w *World) addWorldViewer(l *Loader) {
w.viewerMu.Lock()
w.viewers[l] = l.viewer
w.viewerMu.Unlock()

l.viewer.ViewTime(w.Time())
w.set.Lock()
raining, thundering := w.set.Raining, w.set.Raining && w.set.Thundering
Expand Down

0 comments on commit 381a13c

Please sign in to comment.