talariadb/talaria

Thread safety - flush

crphang opened this issue · 0 comments

The way bytes is being copied is not thread safe https://github.com/kelindar/talaria/blob/master/internal/storage/flush/flush.go#L109 since buffer is being reused.

// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
// The slice is valid for use only until the next buffer modification (that is,
// only until the next call to a method like Read, Write, Reset, or Truncate).
// The slice aliases the buffer content at least until the next buffer modification,
// so immediate changes to the slice will affect the result of future reads.
func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }

In flush, if another thread runs buffer might be written, which overwrites the output that is used to generate the orc file.

output := buffer.Bytes()
buffer.Reset()
s.memoryPool.Put(buffer)

return s.generateFileName(blocks[0]), output