Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/fluent/plugin/in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def start
next
end

@parser.parse(msg) do |time, record|
parse_message(msg) do |time, record|
unless time && record
log.on_warn { warn_pattern_not_matched(msg) }
next
Expand Down Expand Up @@ -192,7 +192,7 @@ def start
next
end

@parser.parse(msg) do |time, record|
parse_message(msg) do |time, record|
unless time && record
log.on_warn { warn_pattern_not_matched(msg) }
next
Expand Down Expand Up @@ -221,6 +221,12 @@ def start

private

def parse_message(msg, &block)
@parser.parse(msg, &block)
rescue => e
log.warn "failed to parse message", error: e, message: msg[...MAX_LOGGED_MESSAGE_LENGTH], size: msg.bytesize

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.

NIT: Could you add log.debug_backtrace(e.backtrace) after this warn? in_tail does the same for an invalid line (lib/fluent/plugin/in_tail/line_feeder.rb). On master, the server helper logged the backtrace. With this change, it is not logged at any level, so a bug in a third-party parser is hard to locate.

end
Comment on lines +224 to +228

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: This rescue also covers the block. So in the single-emit path, it catches errors from router.emit on line 155.

The description says emit errors are unaffected because EventRouter rescues them. But without an @ERROR label, RootAgent#handle_emits_error logs emit transaction failed and re-raises the error. On master, the error reaches the server helper. It logs unexpected error on reading data and closes the connection. With this change, the error is logged as a parse failure, and the connection stays open.

To reproduce, use this config and send {"k":"1"} to {"k":"8"} as eight lines in one write:

<source>
  @type tcp
  port 5170
  bind 127.0.0.1
  tag test
  <parse>
    @type json
  </parse>
  <extract>
    tag_key tag
  </extract>
</source>
<match test>
  @type stdout
  <buffer>
    @type memory
    chunk_limit_size 64
    total_limit_size 64
    flush_mode interval
    flush_interval 1h
    overflow_action throw_exception
  </buffer>
</match>

Each line after the buffer is full logs this, and the connection stays open:

[warn]: failed to parse message error_class=Fluent::Plugin::Buffer::BufferOverflowError error="buffer space has too many data" message="{\"k\":\"3\"}" size=9

In the batch-emit path, router.emit_stream is outside parse_message, so its errors are not affected.

Please rescue only the errors from the parser, and re-raise the errors from the block. One way is to track whether the block is running. This keeps the block running inside @parser.parse, as on master:

Suggested change
def parse_message(msg, &block)
@parser.parse(msg, &block)
rescue => e
log.warn "failed to parse message", error: e, message: msg[...MAX_LOGGED_MESSAGE_LENGTH], size: msg.bytesize
end
def parse_message(msg)
yielding = false
@parser.parse(msg) do |time, record|
yielding = true
result = yield time, record
yielding = false
result
end
rescue => e
raise if yielding
log.warn "failed to parse message", error: e, message: msg[...MAX_LOGGED_MESSAGE_LENGTH], size: msg.bytesize
end

Do not reset yielding in an ensure clause. It must stay true when the block raises.

Please also add a test for this case. For example, use the single-emit config and stub d.instance.router.emit to raise. Then check that d.run raises that error, and that failed to parse message is not logged. The test driver re-raises errors from the server callback, so d.run raises on master too.


def warn_pattern_not_matched(msg)
if msg.length > MAX_LOGGED_MESSAGE_LENGTH
log.warn "pattern not matched", message: msg[...MAX_LOGGED_MESSAGE_LENGTH], size: msg.bytesize
Expand Down
25 changes: 25 additions & 0 deletions test/plugin/test_in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,31 @@ def create_tcp_socket(host, port, &block)
end
end

sub_test_case "parser error" do
data("batch_emit", { extract: "" }, keep: true)
data("single_emit", { extract: "<extract>\ntag_key tag\n</extract>\n" }, keep: true)
test "drop only the line that fails to parse and keep the connection open" do |data|
d = create_driver(base_config + %!
<parse>
@type json
time_key time
time_format %Y-%m-%d
</parse>
#{data[:extract]}
!)
d.run(expect_records: 3, timeout: 10) do
create_tcp_socket('127.0.0.1', @port) do |sock|
sock.send(%Q({"k":"1"}\n{"time":"bad","k":"2"}\n{"k":"3"}\n), 0)
sleep 0.5
sock.send(%Q({"k":"4"}\n), 0)
end
end

assert_equal(%w[1 3 4], d.events.map { |_tag, _time, record| record["k"] })
assert(d.logs.any? { |log| log.include?("[warn]: failed to parse message") })
end
end

sub_test_case "pattern not matched" do
data("batch_emit", { extract: "" }, keep: true)
data("single_emit", { extract: "<extract>\ntag_key tag\n</extract>\n" }, keep: true)
Expand Down