Skip to content

Commit 8556c66

Browse files
committed
fix: panic: send on closed channel
1 parent f02c9e7 commit 8556c66

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

manager/SessionsManager.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ func (sess *SessionsManager) DelSession(id string) {
105105
myconn := *sess.Session[id].Conn
106106
myconn.Close()
107107
}
108-
if sess.Session[id].WorkConn != nil {
108+
sess.Session[id].WorkConnMutex.Lock()
109+
if sess.Session[id].WorkConnIsOpen && sess.Session[id].WorkConn != nil {
110+
sess.Session[id].WorkConnIsOpen = false
109111
close(sess.Session[id].WorkConn)
110112
}
113+
sess.Session[id].WorkConnMutex.Unlock()
111114
}
112115
delete(sess.Session, id)
113116
}
@@ -159,6 +162,7 @@ func (sess *SessionsManager) connHdl(conn net.Conn) {
159162
DisableMuxer: m.DisableMuxer,
160163
Conn: &conn,
161164
GatewaySession: nil,
165+
WorkConnIsOpen: true,
162166
WorkConn: make(chan net.Conn, 5)}
163167
//:TODO 新的登录存储之前先清除旧的同id登录
164168
sess.SetSession(token.RunId, gatewaySession)
@@ -181,6 +185,7 @@ func (sess *SessionsManager) connHdl(conn net.Conn) {
181185
Version: m.Version,
182186
Conn: &conn,
183187
GatewaySession: yamuxSession,
188+
WorkConnIsOpen: true,
184189
WorkConn: make(chan net.Conn, 5)}
185190
//:TODO 新的登录存储之前先清除旧的同id登录
186191
sess.SetSession(token.RunId, gatewaySession)
@@ -217,7 +222,11 @@ func (sess *SessionsManager) connHdl(conn net.Conn) {
217222
// /Users/iotserv/git/server-go/manager/SessionsManager.go:209 +0x4cc
218223
//created by github.com/OpenIoTHub/server-go/manager.SessionsManager.listenerHdl in goroutine 12
219224
// /Users/iotserv/git/server-go/manager/listen.go:131 +0x1e5
220-
session.WorkConn <- conn
225+
session.WorkConnMutex.Lock()
226+
if session.WorkConnIsOpen {
227+
session.WorkConn <- conn
228+
}
229+
session.WorkConnMutex.Unlock()
221230
}
222231

223232
case *models.OpenIoTHubLogin:

session/Session.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/libp2p/go-yamux"
88
"log"
99
"net"
10+
"sync"
1011
"time"
1112
)
1213

@@ -18,6 +19,9 @@ type Session struct {
1819
DisableMuxer bool
1920
Conn *net.Conn
2021
GatewaySession *yamux.Session
22+
//写入之前先获取锁判断WorkConn有没有关闭
23+
WorkConnMutex sync.Mutex
24+
WorkConnIsOpen bool
2125
WorkConn chan net.Conn
2226
}
2327

0 commit comments

Comments
 (0)