From 7e452b630a794314b981e7a333fc7462f933b9d4 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 6 Apr 2017 22:52:15 +0200 Subject: [PATCH] Create new fields, tags and message objects on Buffer#clear This helps in ensuring thread-safety when flushing unbuffered Buffers. Previously, it was possibly for client code to still hold references to fields on a flushed and cleared buffer, potentially resulting in unintended side-effects if the client code was not aware that the buffer was cleared. By creating ompletely new object instances, existing references only point to data from before the clear. This ensures a clean cut. --- lib/rackstash/buffer.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/rackstash/buffer.rb b/lib/rackstash/buffer.rb index b558168..0eb7ca7 100644 --- a/lib/rackstash/buffer.rb +++ b/lib/rackstash/buffer.rb @@ -76,10 +76,8 @@ module Rackstash @buffering = !!buffering @allow_empty = !!allow_empty - @messages = [] - @fields = Rackstash::Fields::Hash.new(forbidden_keys: FORBIDDEN_FIELDS) - @tags = Rackstash::Fields::Tags.new - @timestamp = nil + # initialize the internal data structures for fields, tags, ... + clear end # Add a new message to the buffer. This will mark the current buffer as @@ -130,9 +128,9 @@ module Rackstash # # @return [self] def clear - @messages.clear - @fields.clear - @tags.clear + @messages = [] + @fields = Rackstash::Fields::Hash.new(forbidden_keys: FORBIDDEN_FIELDS) + @tags = Rackstash::Fields::Tags.new @timestamp = nil self