Skip to content

Commit

Permalink
MessagePackFactory: Make sure to reset local unpacker every time
Browse files Browse the repository at this point in the history
Fixes bug in c6c6c03 (#2559).

Received incomplete data must not affect data from other senders.

Signed-off-by: Daijiro Fukuda <[email protected]>
  • Loading branch information
daipom committed Apr 26, 2023
1 parent 0a6d706 commit 3c4fbf0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/fluent/msgpack_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ def self.thread_local_msgpack_packer
end

def self.thread_local_msgpack_unpacker
Thread.current[:local_msgpack_unpacker] ||= MessagePackFactory.engine_factory.unpacker
unpacker = Thread.current[:local_msgpack_unpacker]
if unpacker.nil?
return Thread.current[:local_msgpack_unpacker] = MessagePackFactory.engine_factory.unpacker
end
unpacker.reset
unpacker
end
end
end
32 changes: 32 additions & 0 deletions test/test_msgpack_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,36 @@ class MessagePackFactoryTest < Test::Unit::TestCase
assert mp.msgpack_factory
assert mp.msgpack_factory
end

sub_test_case 'thread_local_msgpack_packer' do
test 'packer is cached' do
packer1 = Fluent::MessagePackFactory.thread_local_msgpack_packer
packer2 = Fluent::MessagePackFactory.thread_local_msgpack_packer
assert_equal packer1, packer2
end
end

sub_test_case 'thread_local_msgpack_unpacker' do
test 'unpacker is cached' do
unpacker1 = Fluent::MessagePackFactory.thread_local_msgpack_unpacker
unpacker2 = Fluent::MessagePackFactory.thread_local_msgpack_unpacker
assert_equal unpacker1, unpacker2
end

# We need to reset the buffer every time so that received incomplete data
# must not affect data from other senders.
test 'reset the internal buffer of unpacker every time' do
unpacker1 = Fluent::MessagePackFactory.thread_local_msgpack_unpacker
unpacker1.feed_each("\xA6foo") do |result|
flunk("This callback must not be called since the data is uncomplete.")
end

records = []
unpacker2 = Fluent::MessagePackFactory.thread_local_msgpack_unpacker
unpacker2.feed_each("\xA3foo") do |result|
records.append(result)
end
assert_equal ["foo"], records
end
end
end

0 comments on commit 3c4fbf0

Please sign in to comment.