From d39fb736dc85d72474570401a1cb05e88b3e940b Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 31 Dec 2018 15:53:51 +0100 Subject: [PATCH] Fix getting stuck when closing pager without reading all entries --- main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 01c5145..aced510 100644 --- a/main.go +++ b/main.go @@ -55,9 +55,13 @@ func showQueue() { reader, writer := io.Pipe() pagerDone := make(chan bool) go launchPager(reader, pagerDone) - // TODO: Figure out how to handle the pager closing before we're done writing for _, entry := range entries { - fmt.Fprintf(writer, "%s\n", entry.String()) + _, writeError := fmt.Fprintf(writer, "%s\n", entry.String()) + if writeError != nil { + // A writeError is expected once the reader was closed + // This happens when the pager application got terminated + break + } } writer.Close() // Wait for pager to be closed before continuing the main program @@ -74,6 +78,7 @@ func launchPager(reader *io.PipeReader, pagerDone chan<- bool) { if err := cmd.Wait(); err != nil { fmt.Printf("Error terminating pager: %s\n", err.Error()) } else { + reader.Close() pagerDone <- true } }