updated wrong fix on waitgroup in the previous commit as well as a type of a field name of ServerEventConnDeleted

This commit is contained in:
hyung-hwan 2025-03-13 22:59:38 +09:00
parent cd32380425
commit f1f74ed48d

View File

@ -90,7 +90,7 @@ type ServerEventConnAdded struct {
} }
type ServerEventConnDeleted struct { type ServerEventConnDeleted struct {
Conn ConnId `json:"conn-id:"` Conn ConnId `json:"conn-id"`
} }
type ServerEventRouteAdded struct { type ServerEventRouteAdded struct {
@ -651,7 +651,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
for { for cts.stop_req.Load() == false {
pkt, err = cts.pss.Recv() pkt, err = cts.pss.Recv()
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
cts.S.log.Write(cts.Sid, LOG_INFO, "RPC stream closed for client %s", cts.RemoteAddr) cts.S.log.Write(cts.Sid, LOG_INFO, "RPC stream closed for client %s", cts.RemoteAddr)
@ -891,10 +891,15 @@ func (cts *ServerConn) RunTask(wg *sync.WaitGroup) {
// function be the channel waiter only. // function be the channel waiter only.
// increment on the wait group is for the caller to wait for // increment on the wait group is for the caller to wait for
// these detached goroutines to finish. // these detached goroutines to finish.
//wg.Add(1) wg.Add(1)
//go cts.receive_from_stream(wg) go cts.receive_from_stream(wg)
// Add(1) to cts.route_wg is mostly performed inside a goroutine
// cts.receive_from_stream() invoked just above.
// this can potentially cause a race condition where cts.route_wg
// is still zero when Wait() is called far below.
// Increment the wait count here before the loop begins
cts.route_wg.Add(1) cts.route_wg.Add(1)
go cts.receive_from_stream(&cts.route_wg)
for { for {
// exit if context is done // exit if context is done
@ -918,6 +923,7 @@ func (cts *ServerConn) RunTask(wg *sync.WaitGroup) {
done: done:
cts.ReqStop() // just in case cts.ReqStop() // just in case
cts.route_wg.Done()
cts.route_wg.Wait() cts.route_wg.Wait()
cts.S.bulletin.Enqueue( cts.S.bulletin.Enqueue(
&ServerEvent{ &ServerEvent{