From e7b6b3a7ae2e53d807e14697708c4110c038303b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 8 Sep 2017 14:33:35 -0400 Subject: nslcd_systemd: FIX: avoid a race condition Spawn handler goroutines from listener goroutine. This fixes a race condition where main | listener ----------+----------------- | conn = accept() return | | sock <- conn and the connection is *not* handled (despite being accepted). --- nslcd_systemd/nslcd_systemd.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'nslcd_systemd') diff --git a/nslcd_systemd/nslcd_systemd.go b/nslcd_systemd/nslcd_systemd.go index f7936cb..97991c8 100644 --- a/nslcd_systemd/nslcd_systemd.go +++ b/nslcd_systemd/nslcd_systemd.go @@ -130,7 +130,7 @@ func Main(backend Backend) uint8 { return sd_daemon.EXIT_NOTRUNNING } defer func() { socket.(*net.UnixListener).SetDeadline(time.Now()) }() - sock := make(chan *net.UnixConn) + socket_error := make(chan error) wg.Add(1) go func() { @@ -161,7 +161,12 @@ func Main(backend Backend) uint8 { } } if conn != nil { - sock <- conn.(*net.UnixConn) + wg.Add(1) + go func() { + defer sd_daemon.Recover() + defer wg.Done() + handler(conn.(*net.UnixConn), backend) + }() } } }() @@ -188,13 +193,6 @@ func Main(backend Backend) uint8 { case err = <-socket_error: sd_daemon.Log.Err(fmt.Sprintf("%v", err)) return sd_daemon.EXIT_FAILURE - case conn := <-sock: - wg.Add(1) - go func() { - defer sd_daemon.Recover() - defer wg.Done() - handler(conn, backend) - }() } } } -- cgit v1.2.3-2-g168b