From 9b5dd58e1b0d3dfa1cae35a47c9c445f65593b74 Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Thu, 12 Nov 2026 01:55:39 -0300 Subject: [PATCH 1/2] time: don't reuse a cached subsecond string for an Integer time TimeFormatter#format_with_subsec checks its cache with EventTime.eq?, which compares only the seconds when one side is an EventTime and the other an Integer. With a %N or %L time_format, an Integer time that followed an EventTime of the same second got the cached string of the EventTime, so it was written with the subsecond part of another event. This happens when one output gets events from clients that send EventTime and from clients that send Integer times. Only reuse a cache slot when the cached time and the given time have the same class. Signed-off-by: Lucas Barros --- lib/fluent/time.rb | 4 ++-- test/test_time_formatter.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index 793dd58e61..19b98bbd2c 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -408,9 +408,9 @@ def format_without_subsec(time) end def format_with_subsec(time) - if @tc1_str && Fluent::EventTime.eq?(@tc1, time) + if @tc1_str && @tc1.class == time.class && Fluent::EventTime.eq?(@tc1, time) return @tc1_str - elsif @tc2_str && Fluent::EventTime.eq?(@tc2, time) + elsif @tc2_str && @tc2.class == time.class && Fluent::EventTime.eq?(@tc2, time) return @tc2_str else str = format_nocache(time) diff --git a/test/test_time_formatter.rb b/test/test_time_formatter.rb index 3780fd7e8b..64a74ab27e 100644 --- a/test/test_time_formatter.rb +++ b/test/test_time_formatter.rb @@ -241,6 +241,22 @@ def test_format_epoch_with_subsec assert_equal("19700101 000000.999999999", formatter.format(Fluent::EventTime.new(0, 999999999))) end + # A cached EventTime must not answer for an Integer time of the same second: + # EventTime.eq? compares the seconds alone then, so the cached string carried + # the subsecond part of the earlier event. + def test_format_with_subsec_integer_and_event_time_of_the_same_second + time = Time.new(2014, 9, 27, 0, 0, 0, 0).to_i + formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%N", false, nil) + assert_equal("20140927 000000.123456789", formatter.format(Fluent::EventTime.new(time, 123456789))) + assert_equal("20140927 000000.000000000", formatter.format(time)) + assert_equal("20140927 000000.123456789", formatter.format(Fluent::EventTime.new(time, 123456789))) + assert_equal("20140927 000000.000000000", formatter.format(time)) + + formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%3N", false, nil) + assert_equal("20140927 000000.000", formatter.format(time)) + assert_equal("20140927 000000.500", formatter.format(Fluent::EventTime.new(time, 500_000_000))) + end + sub_test_case 'TimeMixin::Formatter' do class DummyForTimeFormatter include Fluent::Configurable From e182fd600cb85821b570d6782e76f01fb5ca3bac Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Fri, 13 Nov 2026 20:55:37 -0300 Subject: [PATCH 2/2] time: compare only the same class in EventTime.eq? EventTime.eq? compared an EventTime and an Integer by their seconds, so format_with_subsec had to reject mixed classes before calling it. Make eq? return false for different classes and drop the check at the call site. Signed-off-by: Lucas Barros --- lib/fluent/time.rb | 8 +++++--- test/test_event_time.rb | 4 ++-- test/test_time_formatter.rb | 9 +++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index 19b98bbd2c..691db93e9a 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -104,7 +104,9 @@ def self.from_time(time) end def self.eq?(a, b) - if a.is_a?(Fluent::EventTime) && b.is_a?(Fluent::EventTime) + return false unless a.class == b.class + + if a.is_a?(Fluent::EventTime) a.sec == b.sec && a.nsec == b.nsec else a == b @@ -408,9 +410,9 @@ def format_without_subsec(time) end def format_with_subsec(time) - if @tc1_str && @tc1.class == time.class && Fluent::EventTime.eq?(@tc1, time) + if @tc1_str && Fluent::EventTime.eq?(@tc1, time) return @tc1_str - elsif @tc2_str && @tc2.class == time.class && Fluent::EventTime.eq?(@tc2, time) + elsif @tc2_str && Fluent::EventTime.eq?(@tc2, time) return @tc2_str else str = format_nocache(time) diff --git a/test/test_event_time.rb b/test/test_event_time.rb index 23343bfa22..26aa0ee773 100644 --- a/test/test_event_time.rb +++ b/test/test_event_time.rb @@ -100,10 +100,10 @@ class EventTimeTest < Test::Unit::TestCase refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), Fluent::EventTime.new(3, 2))) refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), Fluent::EventTime.new(3, 4))) - assert(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 1)) + refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 1)) refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 2)) - assert(Fluent::EventTime.eq?(1, Fluent::EventTime.new(1, 2))) + refute(Fluent::EventTime.eq?(1, Fluent::EventTime.new(1, 2))) refute(Fluent::EventTime.eq?(2, Fluent::EventTime.new(1, 2))) end diff --git a/test/test_time_formatter.rb b/test/test_time_formatter.rb index 64a74ab27e..ec808b959f 100644 --- a/test/test_time_formatter.rb +++ b/test/test_time_formatter.rb @@ -227,8 +227,9 @@ def test_format_negative_timestamp assert_equal("1969-12-31T23:59:59Z", formatter.format(Fluent::EventTime.new(-1))) end - # Here the slot is compared with EventTime.eq?, which falls back to comparing - # seconds alone, so the whole first second of the epoch matched an empty slot. + # Here the slot is compared with EventTime.eq?, which used to fall back to + # comparing seconds alone, so the whole first second of the epoch matched an + # empty slot. def test_format_epoch_with_subsec # Only the tc2 slot is used here, so every timestamp is a miss. formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%N", false, nil) @@ -242,8 +243,8 @@ def test_format_epoch_with_subsec end # A cached EventTime must not answer for an Integer time of the same second: - # EventTime.eq? compares the seconds alone then, so the cached string carried - # the subsecond part of the earlier event. + # EventTime.eq? used to compare the seconds alone then, so the cached string + # carried the subsecond part of the earlier event. def test_format_with_subsec_integer_and_event_time_of_the_same_second time = Time.new(2014, 9, 27, 0, 0, 0, 0).to_i formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%N", false, nil)