From 0beddf551976f693de4fc3f00ffec3a000639857 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 17 Jun 2020 03:19:31 +0900 Subject: [PATCH] buffered: Bug fix: Race condition at checkDealeydCommandsNil This change also renames checkDealeydCommandsNil to checkDealeydCommandsFlushed, which makes more sense. Updates #1195 --- internal/buffered/command.go | 4 ++-- internal/buffered/image.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/buffered/command.go b/internal/buffered/command.go index cf4f39bf5..f74112a76 100644 --- a/internal/buffered/command.go +++ b/internal/buffered/command.go @@ -89,8 +89,8 @@ func tryAddDelayedCommandSlow(f func(obj interface{}) error, ondelayed func() in } } -func checkDelayedCommandsNil(fname string) { - if delayedCommands != nil { +func checkDelayedCommandsFlushed(fname string) { + if atomic.LoadUint32(&delayedCommandsFlushed) == 0 { panic("buffered: the command queue is not available yet at " + fname) } } diff --git a/internal/buffered/image.go b/internal/buffered/image.go index bb0ec1d37..9eba55329 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -125,7 +125,7 @@ func (i *Image) MarkDisposed() { } func (img *Image) Pixels(x, y, width, height int) (pix []byte, err error) { - checkDelayedCommandsNil("Pixels") + checkDelayedCommandsFlushed("Pixels") if !image.Rect(x, y, x+width, y+height).In(image.Rect(0, 0, img.width, img.height)) { return nil, fmt.Errorf("buffered: out of range") @@ -160,7 +160,7 @@ func (img *Image) Pixels(x, y, width, height int) (pix []byte, err error) { } func (i *Image) Dump(name string, blackbg bool) error { - checkDelayedCommandsNil("Dump") + checkDelayedCommandsFlushed("Dump") return i.img.Dump(name, blackbg) }