From 1591d86e238921f085e8f8ba50294c17e43d5193 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 22 Jan 2022 14:33:49 -0500 Subject: [PATCH] Quick note about the importance of the copy --- system/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/utils.go b/system/utils.go index 58aa09d..f556b9f 100644 --- a/system/utils.go +++ b/system/utils.go @@ -99,6 +99,8 @@ func ScanReader(r io.Reader, callback func(line []byte)) error { // the websocket. The front-end can handle the linebreaks in the middle of // the output, it simply expects that the end of the event emit is a newline. if buf.Len() > 0 { + // You need to make a copy of the buffer here because the callback will encounter + // a race condition since "buf.Bytes()" is going to be by-reference if passed directly. c := make([]byte, buf.Len()) copy(c, buf.Bytes()) callback(c)