diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-09-08 14:33:35 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-09-08 16:55:55 -0400 |
commit | e7b6b3a7ae2e53d807e14697708c4110c038303b (patch) | |
tree | 39b533c719e6cca58d0f63b43cc0be4c6cc8fafb /nslcd_systemd | |
parent | 4d410a5f047b5b4b55b2dcfc29d5264de695fe94 (diff) |
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).
Diffstat (limited to 'nslcd_systemd')
-rw-r--r-- | nslcd_systemd/nslcd_systemd.go | 16 |
1 files changed, 7 insertions, 9 deletions
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) - }() } } } |