From f598e2962d3a358b59faa68471b6ed378fc68806 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Mon, 22 Nov 2021 15:33:01 -0500 Subject: [PATCH] runtime: fix preemption sensitivity in TestTinyAllocIssue37262 TestTinyAllocIssue37262 assumes that all of its allocations will come from the same tiny allocator (that is, the same P), and that nothing else will allocate from that tiny allocator while it's running. It can fail incorrectly if these assumptions aren't met. Fix this potential test flakiness by disabling preemption during this test. As far as I know, this has never happened on the builders. It was found by mayMoreStackPreempt. Change-Id: I59f993e0bdbf46a9add842d0e278415422c3f804 Reviewed-on: https://go-review.googlesource.com/c/go/+/366994 Trust: Austin Clements Run-TryBot: Austin Clements TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Reviewed-by: Michael Knyszek --- src/runtime/export_test.go | 9 +++++++++ src/runtime/malloc_test.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/src/runtime/export_test.go b/src/runtime/export_test.go index b2e64f14ad..ef601f770c 100644 --- a/src/runtime/export_test.go +++ b/src/runtime/export_test.go @@ -1307,3 +1307,12 @@ func escape(x interface{}) interface{} { escapeSink = nil return x } + +// Acquirem blocks preemption. +func Acquirem() { + acquirem() +} + +func Releasem() { + releasem(getg().m) +} diff --git a/src/runtime/malloc_test.go b/src/runtime/malloc_test.go index e028554b23..757f945393 100644 --- a/src/runtime/malloc_test.go +++ b/src/runtime/malloc_test.go @@ -198,6 +198,10 @@ func TestTinyAllocIssue37262(t *testing.T) { runtime.GC() runtime.GC() + // Disable preemption so we stay on one P's tiny allocator and + // nothing else allocates from it. + runtime.Acquirem() + // Make 1-byte allocations until we get a fresh tiny slot. aligned := false for i := 0; i < 16; i++ { @@ -208,6 +212,7 @@ func TestTinyAllocIssue37262(t *testing.T) { } } if !aligned { + runtime.Releasem() t.Fatal("unable to get a fresh tiny slot") } @@ -229,6 +234,8 @@ func TestTinyAllocIssue37262(t *testing.T) { tinyByteSink = nil tinyUint32Sink = nil tinyObj12Sink = nil + + runtime.Releasem() } func TestPageCacheLeak(t *testing.T) { -- 2.50.0