Skip to content

Commit

Permalink
fix most deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
RestartFU committed Dec 24, 2024
1 parent db313f4 commit 5b8ec4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion inv/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func CloseContainer(p *player.Player) {
ServerSide: true,
})

removeClientSideMenu(p, m)
removeClientSideMenu(s, p.Tx(), m)
}
}
menuMu.Unlock()
Expand Down
7 changes: 4 additions & 3 deletions inv/handler_packet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inv

import (
"fmt"
"github.com/bedrock-gophers/intercept/intercept"
"github.com/bedrock-gophers/unsafe/unsafe"
"github.com/df-mc/dragonfly/server/player"
Expand Down Expand Up @@ -32,7 +33,8 @@ func (h packetHandler) HandleClientPacket(ctx *intercept.Context, pk packet.Pack
case *packet.ItemStackRequest:
handleItemStackRequest(s, pkt.Requests)
case *packet.ContainerClose:
handleContainerClose(ctx, p, s, pkt.WindowID)
ctx.Cancel()
handleContainerClose(p, s, pkt.WindowID)
}
})
}
Expand All @@ -41,7 +43,7 @@ func (h packetHandler) HandleServerPacket(ctx *intercept.Context, pk packet.Pack
// Do nothing
}

func handleContainerClose(ctx *intercept.Context, p *player.Player, s *session.Session, windowID byte) {
func handleContainerClose(p *player.Player, s *session.Session, windowID byte) {
mn, ok := lastMenu(s)
if !ok {
return
Expand All @@ -53,7 +55,6 @@ func handleContainerClose(ctx *intercept.Context, p *player.Player, s *session.S
}
p.OpenBlockContainer(mn.pos, p.Tx())
closeLastMenu(p, mn)
ctx.Cancel()
}

func handleItemStackRequest(s *session.Session, req []protocol.ItemStackRequest) {
Expand Down
26 changes: 12 additions & 14 deletions inv/menu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package inv

import (
unsafe2 "github.com/bedrock-gophers/unsafe/unsafe"
"github.com/df-mc/dragonfly/server/world"
"reflect"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -170,34 +172,30 @@ func lastMenu(s *session.Session) (Menu, bool) {
}

func closeLastMenu(p *player.Player, mn Menu) {
s := player_session(p)
s := unsafe2.Session(p)
if s != session.Nop {
if closeable, ok := mn.submittable.(Closer); ok {
closeable.Close(p)
}
if mn.containerClose != nil {
mn.containerClose(mn.inventory)
}
removeClientSideMenu(p, mn)
removeClientSideMenu(s, p.Tx(), mn)
}

menuMu.Lock()
delete(lastMenus, s)
menuMu.Unlock()
}

func removeClientSideMenu(p *player.Player, m Menu) {
s := player_session(p)
if s != session.Nop {
s.ViewBlockUpdate(m.pos, p.Tx().Block(m.pos), 0)
airPos := m.pos.Add(cube.Pos{0, 1})
s.ViewBlockUpdate(airPos, p.Tx().Block(airPos), 0)
if c, ok := m.container.(ContainerChest); ok && c.DoubleChest {
s.ViewBlockUpdate(m.pos.Add(cube.Pos{1, 0, 0}), p.Tx().Block(m.pos), 0)
airPos = m.pos.Add(cube.Pos{1, 1})
s.ViewBlockUpdate(airPos, p.Tx().Block(airPos), 0)
}
delete(lastMenus, s)
func removeClientSideMenu(s *session.Session, tx *world.Tx, m Menu) {
s.ViewBlockUpdate(m.pos, tx.Block(m.pos), 0)
airPos := m.pos.Add(cube.Pos{0, 1})
s.ViewBlockUpdate(airPos, tx.Block(airPos), 0)
if c, ok := m.container.(ContainerChest); ok && c.DoubleChest {
s.ViewBlockUpdate(m.pos.Add(cube.Pos{1, 0, 0}), tx.Block(m.pos), 0)
airPos = m.pos.Add(cube.Pos{1, 1})
s.ViewBlockUpdate(airPos, tx.Block(airPos), 0)
}
}

Expand Down

0 comments on commit 5b8ec4b

Please sign in to comment.