Quick note about the importance of the copy

This commit is contained in:
Dane Everitt 2022-01-22 14:33:49 -05:00
parent b5536dfc77
commit 1591d86e23

View File

@ -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)