Skip to content

Commit 4c76c01

Browse files
committed
TestSocketLabel: use LockOSThread to avoid flakes
The SetSocketLabel documentation says the caller should use runtime.LockOSThread. Indeed, if not used, there is an occasional flake: > $ go test -count 10000 -run SocketLabel . > --- FAIL: TestSocketLabel (0.00s) > label_linux_test.go:189: write /proc/thread-self/attr/sockcreate: permission denied Add LockOSThread to fix the flake. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 13b180a commit 4c76c01

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

go-selinux/selinux_linux_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ func TestSocketLabel(t *testing.T) {
188188
t.Skip("SELinux not enabled, skipping.")
189189
}
190190

191+
// Ensure the thread stays the same for duration of the test.
192+
// Otherwise Go runtime can switch this to a different thread,
193+
// which results in EACCES in call to SetSocketLabel.
194+
runtime.LockOSThread()
195+
defer runtime.UnlockOSThread()
196+
191197
label := "system_u:object_r:container_t:s0:c1,c2"
192198
if err := SetSocketLabel(label); err != nil {
193199
t.Fatal(err)

0 commit comments

Comments
 (0)