in_tail: drop only the event with invalid bytes in multiline parsing

Closed
BE
1h ago
+109-9

Which issue(s) this PR fixes:
Fixes #5720

What this PR does / why we need it:
With the multiline parser, a line with invalid UTF-8 bytes made LineFeeder#parse_multilines (firstline? and parse) and LineFeeder#flush_buffer raise ArgumentError or Encoding::CompatibilityError. The error escaped, so in_tail read the same lines again and failed again. The file stopped emitting and the pos file did not move.

This PR rescues these two errors in those places, logs the same "invalid line found" warning as convert_line_to_event, and drops the affected event. In parse_multilines, when firstline? fails it is unknown whether the bad line continues the buffered event or starts a new one, so the buffered event is dropped with the bad line instead of being emitted truncated. Events before it are still emitted, and lines appended later are read. It does not change the valid UTF-8 case covered by #5717.

Tests are added in test/plugin/in_tail/test_line_feeder.rb for the firstline? path (UTF-8 string with an ASCII regexp, and binary string with a UTF-8 regexp), the no-format_firstline path, and the timer flush path.

Docs Changes:
Not needed.

Release Note:
in_tail: Fix that the multiline parser stopped reading a file when a line has invalid UTF-8 bytes. The event with the invalid line (and the event being buffered when it is found) is dropped with a warning.


Summary by cubic

Fixes #5720.

With the multiline parser, a line containing invalid UTF-8 bytes made LineFeeder#parse_multilines and LineFeeder#flush_buffer raise ArgumentError or Encoding::CompatibilityError. The error escaped, so in_tail retried the same lines forever, stopped emitting events, and the pos file did not advance.

Now these errors are rescued in both places, a warning is logged (invalid line found, same as convert_line_to_event), and the event holding the invalid line is dropped. When firstline? fails, it is unknown whether the invalid line continues the buffered event or starts a new one, so the buffered event is dropped with the invalid line instead of being emitted truncated. Events before it are still emitted, and lines appended later are read. Valid UTF-8 handling is unchanged.

Tests were added in test/plugin/in_tail/test_line_feeder.rb covering the firstline? path (dropping the buffered event, and not emitting a truncated continuation), the no-format_firstline path, and the timer flush path.