[zxwait] Handle SignalHandleClosed uniformly

Previously, `SignalHandleClosed` was transformed into an error,
conditionally based on the type of context that was provided to `Wait`.
Unify this behavior so that `SignalHandleClosed always becomes an
`ErrCanceled`.

Bug: 132321
Change-Id: I772c26ef45f358eabd9389b7099f41cedffc8044
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/go/+/908852
Reviewed-by: Bruno Dal Bo <brunodalbo@google.com>
diff --git a/src/syscall/zx/zxwait/zxwait.go b/src/syscall/zx/zxwait/zxwait.go
index a7a7c4f..6322d42 100644
--- a/src/syscall/zx/zxwait/zxwait.go
+++ b/src/syscall/zx/zxwait/zxwait.go
@@ -180,6 +180,14 @@
 func (w *waiter) Wait(ctx context.Context, handle zx.Handle, signals zx.Signals) (zx.Signals, error) {
 	var waiting *waitingG
 
+	// If we observe `zx.SignalHandleClosed`, generate a `zx.ErrCanceled` error.
+	errorFromSignals := func(obs zx.Signals) error {
+		if obs == zx.SignalHandleClosed {
+			return &zx.Error{Status: zx.ErrCanceled, Text: "zxwait.Wait"}
+		}
+		return nil
+	}
+
 	w.mu.Lock()
 	if len(w.mu.free) == 0 {
 		waiting = &waitingG{
@@ -204,7 +212,8 @@
 			select {
 			case <-ch:
 				// Wait complete.
-				return waiting.obs, nil
+				obs := waiting.obs
+				return obs, errorFromSignals(obs)
 			case <-done:
 				// Context canceled.
 				switch status := zx.Sys_port_cancel(zx.Handle(w.port), handle, waiting.key); status {
@@ -213,7 +222,8 @@
 				case zx.ErrNotFound:
 					// We lost the race.
 					<-ch
-					return waiting.obs, nil
+					obs := waiting.obs
+					return obs, errorFromSignals(obs)
 				default:
 					return 0, &zx.Error{Status: status, Text: "zx.Port.Cancel"}
 				}
@@ -251,13 +261,7 @@
 	gopark(w.unlockf, waiting, waitReasonIOWait, traceEvGoBlockSelect, 0)
 
 	obs := waiting.obs
-
-	return obs, func() error {
-		if obs == zx.SignalHandleClosed {
-			return &zx.Error{Status: zx.ErrCanceled, Text: "zxwait.Wait"}
-		}
-		return nil
-	}()
+	return obs, errorFromSignals(obs)
 }
 
 // unlockf is passed as a callback to gopark.