Skip to content

parser_multiline: match valid UTF-8 binary input with a UTF-8 regexp - #5717

Merged
Watson1978 merged 1 commit into
fluent:masterfrom
RjyavardhanSingh:fix-multiline-utf8-regexp-binary-input
Dec 3, 2026
Merged

Watson1978 merged 1 commit into
fluent:masterfrom
RjyavardhanSingh:fix-multiline-utf8-regexp-binary-input

Conversation

@RjyavardhanSingh

@RjyavardhanSingh RjyavardhanSingh commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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

What this PR does / why we need it:
When encoding is not set, in_tail passes lines to the parser as binary (ASCII-8BIT) strings. If the multiline parser's format_firstline or format1 is a UTF-8 regexp (for example, one with \p{Alpha} or a non-ASCII character), a line with non-ASCII bytes raises Encoding::CompatibilityError:

#5710 fixed the same error for the regexp parser, but MultilineRegexpParser#parse and firstline? do their own matching, so that fix did not reach them.

Each method below matches directly first:

  • RegexpParser#parse (behavior unchanged)
  • MultilineParser::MultilineRegexpParser#parse
  • MultilineParser#firstline?

If the match raises Encoding::CompatibilityError, the method asks RegexpParser.utf8_text_for_retry for a UTF-8 copy. The helper returns one only when the regexp is UTF-8 and the input is a binary string with valid UTF-8 bytes. The method then matches the copy. Otherwise it re-raises the error, so other incompatible input (invalid UTF-8 bytes, other encodings) still raises Encoding::CompatibilityError.

Tests:

  • test/plugin/test_parser_multiline.rb: firstline? and parse with UTF-8 regexps on binary input, and the invalid-UTF-8 case that still raises.
  • test/plugin/test_in_tail.rb: the reproduction from the issue (including a line appended after startup) for a UTF-8 format_firstline, a UTF-8 format1, and both. Each case checks the exact records emitted.

With only format_firstline being UTF-8 (the configuration in the issue), the event starting with café:2 still does not match format1 /^(?<key>\w+):.../ and is dropped with a warning, because \w is ASCII-only. in_tail now keeps reading the file and emits the other records.

Docs Changes:
None.

Release Note:
parser_multiline: Fix in_tail getting stuck, or dropping multiline events, on valid UTF-8 input containing non-ASCII characters when format_firstline or formatN is a UTF-8 regexp and encoding is not set.

@Watson1978 Watson1978 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. I checked it with in_tail and encoding not set. With the configuration in #5714, in_tail now reads past the café line. It emits a, c and the appended d and e, and the pos file moves to the end. The change also fixes the same stop when format_firstline is not set and format1 is a UTF-8 regexp, and when the last buffered event has the non-ASCII line and is flushed by multiline_flush_interval.

MUST: Please remove pylogs.db from the commit. It is a SQLite database with an empty logs table and is not related to this fix. fluentd.gemspec builds the file list from git ls-files, so the file would also be included in the released gem.

As the description notes, bytes that are not valid UTF-8 still raise the error. For example, with the configuration from #5714, if é is written as the single Latin-1 byte 0xE9, in_tail still stops reading that file. That is a separate problem in in_tail, and it is fine to leave it out of this PR.

Comment thread lib/fluent/plugin/parser_regexp.rb Resolved
Comment thread lib/fluent/plugin/parser_multiline.rb Resolved
Comment thread lib/fluent/plugin/parser_multiline.rb Resolved
Comment thread test/plugin/test_in_tail.rb Resolved
@Watson1978 Watson1978 added bug Something isn't working backport to v1.19 We will backport this fix to the LTS branch labels Dec 3, 2026
in_tail passes lines to the parser as ASCII-8BIT strings when
`encoding` is not set. When format_firstline was a UTF-8 regexp, such
as one with \p{Alpha}, MultilineParser#firstline? raised
Encoding::CompatibilityError at the first non-ASCII line. The error
escaped from in_tail's parse_multilines, so in_tail read the same lines
again on every notification and never moved past them. When format1
was a UTF-8 regexp, MultilineRegexpParser#parse raised the same error
and in_tail dropped the whole multiline event.

Apply the retry added in fluent#5710 in MultilineRegexpParser#parse and
MultilineParser#firstline? too. The check and the UTF-8 copy move into
RegexpParser.utf8_text_for_retry, which runs only after the error, so
matching without the error costs the same as before. A binary string
with valid UTF-8 bytes is matched again as UTF-8.
Other incompatible input still raises Encoding::CompatibilityError.

Fixes fluent#5714

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Rajyavardhan <rajyavardhansing2003@gmail.com>
@RjyavardhanSingh
RjyavardhanSingh force-pushed the fix-multiline-utf8-regexp-binary-input branch from 7ab0b08 to cfe68f2 Compare December 3, 2026 06:07
@RjyavardhanSingh

Copy link
Copy Markdown
Contributor Author

@Watson1978
Thanks for checking it with in_tail, and for testing the no-format_firstline and flush-interval cases too.

  • Removed pylogs.db from the commit. It was added by mistake and is unrelated to this fix.
  • Agreed on the single-byte Latin-1 case (e.g. 0xE9). I'll leave it out of this PR since it's a separate in_tail problem.

@Watson1978 Watson1978 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. All the points from my previous review are addressed in cfe68f2:

  • pylogs.db is removed.
  • RegexpParser#parse, MultilineRegexpParser#parse and MultilineParser#firstline? match directly first and call RegexpParser.utf8_text_for_retry only after Encoding::CompatibilityError.
  • The in_tail test uses timeout: 10 and checks assert_false(d.broken?).

I ran the configuration from #5714 with in_tail again. It emitted a and c, and then the appended d and e, all before shutdown. The pos file moved to the end of the file. test_parser_multiline.rb, test_parser_regexp.rb, in_tail/test_line_feeder.rb and test_in_tail.rb pass. LGTM.

@Watson1978 Watson1978 added this to the v1.21.0 milestone Dec 3, 2026
@Watson1978
Watson1978 merged commit 72930aa into fluent:master Dec 3, 2026
20 checks passed
@RjyavardhanSingh
RjyavardhanSingh deleted the fix-multiline-utf8-regexp-binary-input branch December 3, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport to v1.19 We will backport this fix to the LTS branch bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in_tail with the multiline parser stops reading a file at a non-ASCII line when format_firstline is a UTF-8 regexp

2 participants