Skip to content

parser_regexp: handle UTF-8 binary input - #5710

Merged
Watson1978 merged 4 commits into
fluent:masterfrom
sanketpatel32:devin/1796164525-tcp-regexp-encoding
Dec 2, 2026
Merged

Watson1978 merged 4 commits into
fluent:masterfrom
sanketpatel32:devin/1796164525-tcp-regexp-encoding

Conversation

@sanketpatel32

@sanketpatel32 sanketpatel32 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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

What this PR does / why we need it:

in_tcp passes socket lines to parsers as ASCII-8BIT strings. A UTF-8-only regexp — including one written in an ASCII-only configuration with \p{Alpha} or a Unicode escape — raises Encoding::CompatibilityError for valid non-ASCII UTF-8 input. The exception escapes parsing, so the server helper closes the connection and the other lines in the same read are lost.

This PR retries the regexp match with a UTF-8 copy when the regexp is UTF-8, the input is binary, and the bytes are valid UTF-8. Binary input whose bytes are not valid UTF-8, and input tagged with another encoding, still raise Encoding::CompatibilityError as before.

Tests cover UTF-8 regexp matching of binary input, both error paths, and in_tcp in batch and single-emit modes with several lines in one read followed by another write on the same connection.

Tests run in the ruby:3.4-slim Docker image on Linux:

  • bundle exec rake test TEST=test/plugin/test_in_tcp.rb: 44 tests, 89 assertions, 100% passed
  • bundle exec rake test TEST=test/plugin/test_parser_regexp.rb: 22 tests, 44 assertions, 100% passed
  • rubocop: 474 files inspected, no offenses detected

Docs Changes:
None.

Release Note:
parser_regexp: parse valid UTF-8 messages passed as binary strings with UTF-8 regexps.

Signed-off-by: Sanket Patel <sanpatel323@gmail.com>
Signed-off-by: Sanket Patel <sanpatel323@gmail.com>
Signed-off-by: Sanket Patel <sanpatel323@gmail.com>

@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. The parser change fixes #5691. Valid UTF-8 lines now match a UTF-8 expression, so the other lines and the connection are kept. The new tests for this case fail on master and pass with this change.

I have one request. Please remove the in_tcp change from this PR, because #5640 changes the same code for #5629. See the inline comment.

Comment thread lib/fluent/plugin/in_tcp.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 2, 2026
Signed-off-by: Sanket Patel <sanpatel323@gmail.com>
@sanketpatel32 sanketpatel32 changed the title in_tcp, parser_regexp: handle UTF-8 binary input without dropping lines parser_regexp: handle UTF-8 binary input Dec 2, 2026

@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. This addresses my previous review. lib/fluent/plugin/in_tcp.rb matches master again, and the new parser test covers both invalid bytes in binary input and input tagged with another encoding. I confirmed that the new tests for valid UTF-8 input fail on master and pass with this change, in both emit modes for in_tcp.

#5640 was closed, so I opened #5711 for the in_tcp side. I tested both PRs together locally. They merge cleanly, the tests in both changed test files pass, and a line with invalid UTF-8 bytes no longer closes the connection.

LGTM.

@Watson1978 Watson1978 modified the milestones: v1.19.5, v1.21.0 Dec 2, 2026
@Watson1978
Watson1978 merged commit 683dd17 into fluent:master Dec 2, 2026
20 checks passed
RjyavardhanSingh added a commit to RjyavardhanSingh/fluentd that referenced this pull request 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>
Watson1978 pushed a commit that referenced this pull request Dec 3, 2026
…5717)

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 #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 #5714

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Rajyavardhan <rajyavardhansing2003@gmail.com>
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_tcp loses other lines and closes the connection when a regexp with non-ASCII characters gets a non-ASCII message

2 participants