From 9611199b944e18b6330b8d32e63ac6ac71588d58 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Thu, 12 Jul 2018 18:29:05 -0700 Subject: Fix race condition and logging The extra `defer os.Remove(fifo)` was sometimes racing with the next instance of ninja, removing the file in between when it was re-created and used. Since we're always removing the file before creating it, it's safe to just remove that. The error message for this failure wasn't all that good either, so move so use the status Print/Error calls inside the goroutine instead of the logging ones. Test: `build/soong/build_test.bash -only-soong` repeatedly Change-Id: Icfeb6b68802093bd3a07d3e46046ef7d1a89d4a1 --- ui/status/ninja.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ui/status/ninja.go b/ui/status/ninja.go index 4cce6815..7d330f9a 100644 --- a/ui/status/ninja.go +++ b/ui/status/ninja.go @@ -37,15 +37,13 @@ func NinjaReader(ctx logger.Logger, status ToolStatus, fifo string) { ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err) } - go ninjaReader(ctx, status, fifo) + go ninjaReader(status, fifo) } -func ninjaReader(ctx logger.Logger, status ToolStatus, fifo string) { - defer os.Remove(fifo) - +func ninjaReader(status ToolStatus, fifo string) { f, err := os.Open(fifo) if err != nil { - ctx.Fatal("Failed to open fifo:", err) + status.Error(fmt.Sprintf("Failed to open fifo:", err)) } defer f.Close() @@ -57,7 +55,7 @@ func ninjaReader(ctx logger.Logger, status ToolStatus, fifo string) { size, err := readVarInt(r) if err != nil { if err != io.EOF { - ctx.Println("Got error reading from ninja:", err) + status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) } return } @@ -66,9 +64,9 @@ func ninjaReader(ctx logger.Logger, status ToolStatus, fifo string) { _, err = io.ReadFull(r, buf) if err != nil { if err == io.EOF { - ctx.Printf("Missing message of size %d from ninja\n", size) + status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size)) } else { - ctx.Fatal("Got error reading from ninja:", err) + status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) } return } @@ -76,7 +74,7 @@ func ninjaReader(ctx logger.Logger, status ToolStatus, fifo string) { msg := &ninja_frontend.Status{} err = proto.Unmarshal(buf, msg) if err != nil { - ctx.Printf("Error reading message from ninja: %v\n", err) + status.Print(fmt.Sprintf("Error reading message from ninja: %v", err)) continue } -- cgit v1.2.3