From 9270d940eb1a6e31587c34f4644189f3b2c002e1 Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Wed, 9 Jan 2019 17:53:31 -0800 Subject: Minor memevent fixes. - Call MemoryEvents.done.Add(1) outside of MemoryEvents.run() so that if MemoryEvents.Stop() => MemoryEvents.done.Wait() is called before the goroutine starts running, it still waits for the goroutine to stop. - Use defer to call MemoryEvents.done.Done() in MemoryEvents.run() so that it's called even if the goroutine panics. PiperOrigin-RevId: 228623307 Change-Id: I1b0459e7999606c1a1a271b16092b1ca87005015 --- pkg/sentry/kernel/memevent/memory_events.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/sentry/kernel/memevent/memory_events.go b/pkg/sentry/kernel/memevent/memory_events.go index 1a8e86827..b6283c5d1 100644 --- a/pkg/sentry/kernel/memevent/memory_events.go +++ b/pkg/sentry/kernel/memevent/memory_events.go @@ -69,11 +69,12 @@ func (m *MemoryEvents) Start() { if m.period == 0 { return } + m.done.Add(1) go m.run() // S/R-SAFE: doesn't interact with saved state. } func (m *MemoryEvents) run() { - m.done.Add(1) + defer m.done.Done() // Emit the first event immediately on startup. totalTicks.Increment() @@ -85,7 +86,6 @@ func (m *MemoryEvents) run() { for { select { case <-m.stop: - m.done.Done() return case <-ticker.C: totalTicks.Increment() -- cgit v1.2.3