diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go index 6f7dc6eaa6..2c7bfd8a59 100644 --- a/src/runtime/mheap.go +++ b/src/runtime/mheap.go @@ -1283,9 +1283,10 @@ HaveSpan: // Publish the span in various locations. // This is safe to call without the lock held because the slots - // related to this span will only every be read or modified by - // this thread until pointers into the span are published or - // pageInUse is updated. + // related to this span will only ever be read or modified by + // this thread until pointers into the span are published (and + // we execute a publication barrier at the end of this function + // before that happens) or pageInUse is updated. h.setSpans(s.base(), npages, s) if !manual { @@ -1315,6 +1316,11 @@ HaveSpan: traceHeapAlloc() } } + + // Make sure the newly allocated span will be observed + // by the GC before pointers into the span are published. + publicationBarrier() + return s }