Skip to content

Commit

Permalink
fix: 修复 server 包死锁检测中 Message 读写的竞态问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Feb 22, 2024
1 parent 6846c9d commit b81f972
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/super"
"sync"
)

const (
Expand Down Expand Up @@ -87,6 +88,7 @@ type Message struct {
producer string
name string
t MessageType
l *sync.RWMutex
}

// bindDispatcher 绑定分发器
Expand All @@ -101,6 +103,10 @@ func (slf *Message) GetProducer() string {

// reset 重置消息结构体
func (slf *Message) reset() {
if slf.l != nil {
slf.l.Lock()
defer slf.l.Unlock()
}
slf.conn = nil
slf.ordinaryHandler = nil
slf.exceptionHandler = nil
Expand Down
6 changes: 5 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/signal"
"runtime/debug"
"sync"
"sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -437,13 +438,16 @@ func (srv *Server) dispatchMessage(dispatcherIns *dispatcher.Dispatcher[string,
cancel context.CancelFunc
)
if srv.deadlockDetect > 0 {
msg.l = new(sync.RWMutex)
ctx, cancel = context.WithTimeout(context.Background(), srv.deadlockDetect)
go func(ctx context.Context, srv *Server, msg *Message) {
select {
case <-ctx.Done():
if err := ctx.Err(); errors.Is(err, context.DeadlineExceeded) {
log.Warn("Server", log.String("MessageType", messageNames[msg.t]), log.String("Info", msg.String()), log.Any("SuspectedDeadlock", msg))
msg.l.RLock()
log.Warn("Server", log.String("SuspectedDeadlock", msg.String()))
srv.OnDeadlockDetectEvent(msg)
msg.l.RUnlock()
}
}
}(ctx, srv, msg)
Expand Down

0 comments on commit b81f972

Please sign in to comment.