From 837cb8b188991999f722cdd6b50fa9093bfb521d Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 23 Jan 2025 12:18:41 +0800 Subject: [PATCH] fix: Fix panic due to send on closed channel ``` 2025-01-23T12:19:46+08:00 |INFO| New session: bf1be06db4b7c04cb951e5cb90e60f3b panic: send on closed channel goroutine 8 [running]: main.(*user).WriteMsg(0xc000342140, 0x1, {0xc000124050, 0x41, 0x50}) /mnt/wsl/work/rttys/user.go:52 +0x8b main.userLoginAck(0x0, {0xf83100, 0xc000342140}) /mnt/wsl/work/rttys/user.go:75 +0xc9 main.(*broker).run(0xc000270c80) /mnt/wsl/work/rttys/broker.go:212 +0xd91 created by main.runRttys in goroutine 1 /mnt/wsl/work/rttys/main.go:86 +0x595 exit status 2 ``` Run this script to reproduce: ``` local websocket = require 'eco.websocket' local ws, err = websocket.connect('ws://127.0.0.1:5913/connect/test') if not ws then print('failed to connect: ' .. err) return end os.exit() ``` Signed-off-by: Jianhui Zhao --- broker.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/broker.go b/broker.go index 380c871..1ba7632 100644 --- a/broker.go +++ b/broker.go @@ -138,6 +138,8 @@ func (br *broker) run() { if dev, ok := br.devices[devid]; ok { sid := utils.GenUniqueID("sid") + c.(*user).sid = sid + s := &session{ dev: dev, user: c, @@ -206,9 +208,6 @@ func (br *broker) run() { } else { atomic.StoreUint32(&s.confirmed, 1) - u := s.user.(*user) - u.sid = msg.sid - userLoginAck(loginErrorNone, s.user) } }