m/p/logtree/unraw: close fifo on context cancel

This makes unraw runnables capable of being restarted instead of being
stuck forever in canceling.

Change-Id: I99d66d25b96644cc6a2da431fd4ca1873e552104
Reviewed-on: https://review.monogon.dev/c/monogon/+/416
Reviewed-by: Leopold Schabel <leo@nexantic.com>
diff --git a/metropolis/pkg/logtree/unraw/unraw.go b/metropolis/pkg/logtree/unraw/unraw.go
index 8e319d2..ef9d913 100644
--- a/metropolis/pkg/logtree/unraw/unraw.go
+++ b/metropolis/pkg/logtree/unraw/unraw.go
@@ -113,6 +113,10 @@
 		if err != nil {
 			return fmt.Errorf("when opening named pipe: %w", err)
 		}
+		go func() {
+			<-ctx.Done()
+			fifo.Close()
+		}()
 		defer fifo.Close()
 		supervisor.Signal(ctx, supervisor.SignalHealthy)
 		for {
@@ -131,7 +135,12 @@
 				// is not an issue for us.
 				time.Sleep(10 * time.Millisecond)
 			} else if err != nil {
-				return fmt.Errorf("log pump failed: %v", err)
+				// Since we close fifo on context cancel, we'll get a 'file is already closed'
+				// io error here. Translate that over to the context error that caused it.
+				if ctx.Err() != nil {
+					return ctx.Err()
+				}
+				return fmt.Errorf("log pump failed: %w", err)
 			}
 
 		}