From b122d55d0bb9469c5997960885e214b8b32c2a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Sch=C3=BCttel?= Date: Wed, 27 Jul 2016 16:52:19 +0200 Subject: [PATCH] stdin input plugin: avoid pushing input before it has been completly read Should something that we read from sysread() not end with a newline we keep it back until we see the next newline. This mitigates the situation where sysread returns after a certain amount of bytes, regardless if the latest line has terminated. --- lib/logstash/inputs/stdin.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/logstash/inputs/stdin.rb b/lib/logstash/inputs/stdin.rb index b6ed1d9..8e645e2 100644 --- a/lib/logstash/inputs/stdin.rb +++ b/lib/logstash/inputs/stdin.rb @@ -24,12 +24,30 @@ def run(queue) # Based on some testing, there is no way to interrupt an IO.sysread nor # IO.select call in JRuby. Bummer :( data = $stdin.sysread(16384) + if @carry_over + data = @carry_over + data + @carry_over = nil + end + unless data =~ /\n\Z/ + lines = data.lines.to_a + @carry_over = lines.pop + data = lines.join("\n") + end @codec.decode(data) do |event| decorate(event) event["host"] = @host if !event.include?("host") queue << event end rescue IOError, EOFError # stdin closed + if @carry_over + carry_over = @carry_over + @carry_over = nil + @codec.decode(carry_over) do |event| + decorate(event) + event["host"] = @host if !event.include?("host") + queue << event + end + end break rescue => e # ignore any exception in the shutdown process