[syscall/zx] Avoid data race when closing handles
This patch avoids a race closing handles across multiple threads.
TEST: Verified amber still works with this patch.
Change-Id: I3b11fc4d41521028fa2f82690eae580224ec1e29
diff --git a/src/syscall/zx/handle.go b/src/syscall/zx/handle.go
index d54f2b5..94a348b 100644
--- a/src/syscall/zx/handle.go
+++ b/src/syscall/zx/handle.go
@@ -6,7 +6,10 @@
package zx
-import "unsafe"
+import (
+ "sync/atomic"
+ "unsafe"
+)
// Process-wide FDIO handles.
var (
@@ -126,10 +129,10 @@
}
func (h *Handle) Close() error {
- if status := Sys_handle_close(*h); status != ErrOk {
+ handle := Handle(atomic.SwapUint32((*uint32)(h), uint32(HandleInvalid)))
+ if status := Sys_handle_close(handle); status != ErrOk {
return Error{Status: status, Text: "zx.Handle.Close"}
}
- *h = HandleInvalid
return nil
}