Skip to content

Fix Encoding::CompatibilityError on non-ASCII binary input - #126

Merged
Watson1978 merged 2 commits into
fluent:masterfrom
RaphaelFakhri:fix-binary-non-ascii-input
Nov 8, 2026
Merged

Watson1978 merged 2 commits into
fluent:masterfrom
RaphaelFakhri:fix-binary-non-ascii-input

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

Fixes #107

Problem

The bundled MONTH pattern contains a non-ASCII character (ä), so every expanded regexp that uses it (for example HTTPDATE, SYSLOGTIMESTAMP, COMBINEDAPACHELOG) is a UTF-8 regexp. in_tail passes lines as ASCII-8BIT by default. When a line contains non-ASCII bytes, Regexp#match raises Encoding::CompatibilityError. Because of that:

  • The parser stops at the first such <grok> section instead of trying the next one or falling back to grok_failure_key.
  • in_tail drops the line with invalid line found.
  • With multiline_grok and multiline_flush_interval, the error escapes the flush timer and repeats on every flush, as reported in multiline_grok crashes when multiline_flush_interval is used. #107.

Changes

  • GrokParser#parse matches each section through a new private parse_by helper. When the match raises Encoding::CompatibilityError, it retries with the text reinterpreted as UTF-8. If the text is not valid UTF-8, the section counts as not matched, so the next section or the grok_failure_key fallback applies.
  • MultilineGrokParser#parse was an exact copy of GrokParser#parse, so it is removed and the subclass inherits the fix.

Input that matched before is unchanged. Only input that used to raise now goes through the retry.

Tests

  • test/test_grok_parser.rb: a MONTH-based pattern matches non-ASCII binary text, a non-matching MONTH-based section falls through to the next section, and invalid UTF-8 falls back to grok_failure_key.
  • test/test_multiline_grok_parser.rb: the multiline record from multiline_grok crashes when multiline_flush_interval is used. #107 parses.

bundle exec rake test: 50 tests, 0 failures, 0 errors. Without the change in lib/, the 4 new tests fail with Encoding::CompatibilityError.

The configuration from #107 also runs with fluentd 1.19.4. It emits the multiline record on flush and stops repeating the error.

Bundled patterns such as MONTH contain non-ASCII characters, so the
expanded regexps are UTF-8. Matching them against an ASCII-8BIT string
with non-ASCII bytes (the default for in_tail) raises
Encoding::CompatibilityError instead of trying the next <grok> section.
With multiline_grok and multiline_flush_interval, the error repeats on
every flush.

Retry the match with the text reinterpreted as UTF-8, and treat text
that is not valid UTF-8 as not matched.

Fixes fluent#107

Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.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 and the tests.

I checked f5fe675. The four new tests fail on master with Encoding::CompatibilityError and pass with this change. I also ran the config from #107 (in_tail with multiline_flush_interval) on fluentd 1.19.3. All four records are now emitted, and the flush error no longer repeats.

For input that parses without an error on master, I found no change in the records, including the encoding of the values. Some text in other encodings, such as UTF-16LE, used to raise. Now it can return a wrong value. I'd like this fixed before merging. Please see the inline comment, which limits the retry to binary text.

return time, record
end
nil
rescue Encoding::CompatibilityError

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.

MUST: Please add a guard in this rescue so that only binary (ASCII-8BIT) text is retried as UTF-8. The first parse attempt can stay as it is.

The retry currently runs for any encoding. Text tagged with another encoding, such as UTF-16LE or ISO-8859-1, is then reinterpreted as UTF-8 bytes. A wrong value can come back as a successful match. On this commit:

  • %{NUMBER:value:integer} with "42".encode("UTF-16LE") returns {"value" => 4}.
  • %{HTTPDATE:timestamp} %{GREEDYDATA:city} with "22/Nov/2023:18:18:09 +0100 München".encode("ISO-8859-1") returns "city" => "München".

On master, both raise Encoding::CompatibilityError. in_tail passes such text when its encoding is set to something other than UTF-8. With the change below, these cases raise as on master.

Suggested change
rescue Encoding::CompatibilityError
rescue Encoding::CompatibilityError
raise unless text.encoding == Encoding::ASCII_8BIT

The guard also bounds the retry. If parsing the retried UTF-8 text raises Encoding::CompatibilityError again, that error is raised instead of starting another retry. Today it recurses until SystemStackError. For example, a Hash config with "grok_pattern" => "%{HTTPDATE:timestamp} %{GREEDYDATA:message}" and "null_value_pattern" => "なし".b does this for "22/Nov/2023:18:18:09 +0100 München".b.

With this change, the four new tests still pass. Could you add two tests that expect Encoding::CompatibilityError: one for the ISO-8859-1 example above, and one for the null_value_pattern example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done in 970f77c. The rescue now re-raises unless the text is ASCII-8BIT. The two new tests (ISO-8859-1 text, and the binary null_value_pattern) expect Encoding::CompatibilityError. On the previous commit, they fail with no error and with SystemStackError. All 52 tests pass, and every commit is signed off.

@Watson1978

Copy link
Copy Markdown
Contributor

Also, the DCO check is failing because commit f5fe675 has no Signed-off-by line. Could you sign off every commit in this PR, including the one for the change above?

git commit -s adds the line to new commits. For the existing commit, run git rebase --signoff HEAD~1 and then git push --force-with-lease. The DCO check details describe the same steps.

@RaphaelFakhri
RaphaelFakhri force-pushed the fix-binary-non-ascii-input branch from f5fe675 to ad3ddaf Compare November 6, 2026 20:07
Text in another encoding, such as UTF-16LE or ISO-8859-1, was
reinterpreted as UTF-8 bytes and could return a wrong match. Raise the
Encoding::CompatibilityError for such text, as before.

This also bounds the retry. If matching the UTF-8 text raises again, for
example because of a binary null_value_pattern, the error is raised
instead of recursing until SystemStackError.

Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.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 update. I checked 970f77c.

The guard and both requested tests are in. The tests fail on f5fe675 and pass on 970f77c. The ISO-8859-1 and UTF-16LE examples from my last review raise Encoding::CompatibilityError again, as on master. The binary null_value_pattern case raises the same error instead of SystemStackError.

Binary input that parsed on f5fe675 still gives the same records. I also re-ran the config from #107 on fluentd 1.19.3. All four records are emitted, and the flush error no longer repeats. DCO and CI pass.

LGTM.

@Watson1978
Watson1978 merged commit 84f449d into fluent:master Nov 8, 2026
14 checks passed
@Watson1978 Watson1978 added the bug label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multiline_grok crashes when multiline_flush_interval is used.

2 participants