Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: fix packet handling race condition #920

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,23 @@ func (s *Session) handlePackets() {
_ = s.Close()
}()
for {
pk, err := s.conn.ReadPacket()
if err != nil {
return
}
if err := s.handlePacket(pk); err != nil {
// An error occurred during the handling of a packet. Print the error and stop handling any more
// packets.
s.log.Debugf("failed processing packet from %v (%v): %v\n", s.conn.RemoteAddr(), s.c.Name(), err)
return
s.c.World().Wait()

for {
pk, err := s.conn.ReadPacket()
if err != nil {
return
}
if pk == nil {
break
}

if err := s.handlePacket(pk); err != nil {
// An error occurred during the handling of a packet. Print the error and stop handling any more
// packets.
s.log.Debugf("failed processing packet from %v (%v): %v\n", s.conn.RemoteAddr(), s.c.Name(), err)
return
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions server/world/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/sirupsen/logrus"
"math/rand"
"sync"
"time"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func (conf Config) New() *World {
conf: conf,
ra: conf.Dim.Range(),
set: s,
cond: *sync.NewCond(&sync.Mutex{}),
}
w.weather, w.ticker = weather{w: w}, ticker{w: w}
var h Handler = NopHandler{}
Expand Down
2 changes: 2 additions & 0 deletions server/world/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func (t ticker) tickLoop() {
for {
select {
case <-tc.C:
t.w.cond.Broadcast()

t.tick()
case <-t.w.closing:
// World is being closed: Stop ticking and get rid of a task.
Expand Down
12 changes: 12 additions & 0 deletions server/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type World struct {
closing chan struct{}
running sync.WaitGroup

cond sync.Cond

chunkMu sync.Mutex
// chunks holds a cache of chunks currently loaded. These chunks are cleared from this map after some time
// of not being used.
Expand Down Expand Up @@ -953,6 +955,16 @@ func (w *World) ScheduleBlockUpdate(pos cube.Pos, delay time.Duration) {
w.scheduledUpdates[pos] = t + delay.Nanoseconds()/int64(time.Second/20)
}

// Wait waits for broadcast from w.cond, used for packet processing.
func (w *World) Wait() {
if w == nil {
return
}
w.cond.L.Lock()
w.cond.Wait()
w.cond.L.Unlock()
}

// doBlockUpdatesAround schedules block updates directly around and on the position passed.
func (w *World) doBlockUpdatesAround(pos cube.Pos) {
if w == nil || pos.OutOfBounds(w.Range()) {
Expand Down
Loading