From 503bd3089694fedb7056e8cd5d081fff14545039 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Mon, 16 Nov 2026 14:27:18 +0900 Subject: [PATCH] msgpack_factory: fix events corrupted by a previous record that failed to be packed When packing a record raises an error, the partially written bytes stay in the thread local packer. The next event stream packed on the same thread is appended to them and gets corrupted. Clear the thread local packer every time it is taken, as #4178 did for the thread local unpacker. Use Packer#clear because Packer#reset needs msgpack 1.4.5 or later. Signed-off-by: Shizuo Fujita Co-Authored-By: Claude Opus 5.5 --- lib/fluent/msgpack_factory.rb | 6 +++++- test/test_msgpack_factory.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/fluent/msgpack_factory.rb b/lib/fluent/msgpack_factory.rb index 0d0c0ff01a..264dc852cd 100644 --- a/lib/fluent/msgpack_factory.rb +++ b/lib/fluent/msgpack_factory.rb @@ -96,7 +96,11 @@ def self.init(enable_time_support: false) end def self.thread_local_msgpack_packer - Thread.current[:local_msgpack_packer] ||= MessagePackFactory.engine_factory.packer + packer = Thread.current[:local_msgpack_packer] ||= MessagePackFactory.engine_factory.packer + # Discard bytes left by a write that raised. + # Use #clear because #reset needs msgpack 1.4.5 or later. + packer.clear + packer end def self.thread_local_msgpack_unpacker diff --git a/test/test_msgpack_factory.rb b/test/test_msgpack_factory.rb index ba9909590d..ab72eff8e0 100644 --- a/test/test_msgpack_factory.rb +++ b/test/test_msgpack_factory.rb @@ -22,6 +22,16 @@ class MessagePackFactoryTest < Test::Unit::TestCase packer2 = Fluent::MessagePackFactory.thread_local_msgpack_packer assert_equal packer1, packer2 end + + test 'clear the internal buffer of packer every time' do + packer1 = Fluent::MessagePackFactory.thread_local_msgpack_packer + assert_raise(RangeError) do + packer1.write([1, {"big" => 2**64}]) + end + + packer2 = Fluent::MessagePackFactory.thread_local_msgpack_packer + assert_equal [1, {"user" => "other"}], MessagePack.unpack(packer2.write([1, {"user" => "other"}]).full_pack) + end end sub_test_case 'thread_local_msgpack_unpacker' do