From 4d410a5f047b5b4b55b2dcfc29d5264de695fe94 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 8 Sep 2017 14:21:38 -0400 Subject: nslcd_server: FIX: HandleRequest(): error handling Instead of having a separate channel+goroutine for the backend, just call .Write() right where we get results from the backend, but don't let an error cause us to stop reading from the backend, an error should just cause us to stop writing. This ensures that the backend now always has its output chan drained; before if there were a write error, the backend chan wouldn't get drained, and the resources never released. --- nslcd_server/func_handlerequest.go.gen | 37 ++++++++++++++-------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/nslcd_server/func_handlerequest.go.gen b/nslcd_server/func_handlerequest.go.gen index 40e00c0..d34db88 100755 --- a/nslcd_server/func_handlerequest.go.gen +++ b/nslcd_server/func_handlerequest.go.gen @@ -53,20 +53,15 @@ func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred } } }() - handleRequest(backend, in, out, cred) - return -} -func handleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred) { var version int32 maybePanic(p.Read(in, &version)) if version != p.NSLCD_VERSION { - panic(p.NslcdError(fmt.Sprintf("Version mismatch: server=%#08x client=%#08x", p.NSLCD_VERSION, version))) + return p.NslcdError(fmt.Sprintf("Version mismatch: server=%#08x client=%#08x", p.NSLCD_VERSION, version)) } var action int32 maybePanic(p.Read(in, &action)) - ch := make(chan interface{}) switch action { $( while read -r request; do @@ -99,27 +94,25 @@ while read -r request; do ;; esac ) - _ch := backend.${request}(cred, req) - go func() { - defer close(ch) - for obj := range _ch { - ch <- obj + maybePanic(p.Write(out, p.NSLCD_VERSION)) + maybePanic(p.Write(out, action)) + ch := backend.${request}(cred, req) + for result := range ch { + if err == nil { + err = p.Write(out, p.NSLCD_RESULT_BEGIN) + } + if err == nil { + err = p.Write(out, result) } - }() + } + maybePanic(err) + maybePanic(p.Write(out, p.NSLCD_RESULT_END)) + return nil EOT done < "$requests" ) default: - close(ch) - panic(p.NslcdError(fmt.Sprintf("Unknown request action: %#08x", action))) - } - maybePanic(p.Write(out, p.NSLCD_VERSION)) - maybePanic(p.Write(out, action)) - - for result := range ch { - maybePanic(p.Write(out, p.NSLCD_RESULT_BEGIN)) - maybePanic(p.Write(out, result)) + return p.NslcdError(fmt.Sprintf("Unknown request action: %#08x", action)) } - maybePanic(p.Write(out, p.NSLCD_RESULT_END)) } EOF -- cgit v1.2.3