Skip to content

Commit 6738575

Browse files
committed
backport overlarge-payload fixes from 6.1.1
1 parent c5e7637 commit 6738575

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 6.0.3
22
- Pulled applicable back-ports from 6.1.0
33
- Fix: Ensure sockets are closed when this plugin is closed
4+
- Fix: Fixes an issue where payloads larger than a connection's current TCP window could be silently truncated
45

56
## 6.0.2
67
- Fix: unable to start with password protected key [#45](https://github.com/logstash-plugins/logstash-output-tcp/pull/45)

lib/logstash/outputs/tcp.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def register
141141
break if @closed.value
142142
Thread.start(server_socket.accept) do |client_socket|
143143
# monkeypatch a 'peer' method onto the socket.
144-
client_socket.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
144+
client_socket.extend(::LogStash::Util::SocketPeer)
145145
@logger.debug("Accepted connection", :client => client_socket.peer,
146146
:server => "#{@host}:#{@port}")
147147
client = Client.new(client_socket, self)
@@ -164,14 +164,22 @@ def register
164164
@codec.on_event do |event, payload|
165165
begin
166166
client_socket = connect unless client_socket
167-
r,w,e = IO.select([client_socket], [client_socket], [client_socket], nil)
168-
# don't expect any reads, but a readable socket might
169-
# mean the remote end closed, so read it and throw it away.
170-
# we'll get an EOFError if it happens.
171-
client_socket.sysread(16384) if r.any?
167+
168+
writable_io = nil
169+
while writable_io.nil? || writable_io.any? == false
170+
readable_io, writable_io, _ = IO.select([client_socket],[client_socket])
171+
172+
# don't expect any reads, but a readable socket might
173+
# mean the remote end closed, so read it and throw it away.
174+
# we'll get an EOFError if it happens.
175+
readable_io.each { |readable| readable.sysread(16384) }
176+
end
172177

173178
# Now send the payload
174-
client_socket.syswrite(payload) if w.any?
179+
while payload && payload.bytesize > 0
180+
written_bytes_size = client_socket.syswrite(payload)
181+
payload = payload.byteslice(written_bytes_size..-1)
182+
end
175183
rescue => e
176184
log_warn "client socket failed:", e, host: @host, port: @port, socket: client_socket&.to_s
177185
client_socket.close rescue nil
@@ -210,7 +218,7 @@ def connect
210218
raise
211219
end
212220
end
213-
client_socket.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
221+
client_socket.extend(::LogStash::Util::SocketPeer)
214222
@logger.debug("Opened connection", :client => "#{client_socket.peer}")
215223
return client_socket
216224
rescue StandardError => e

0 commit comments

Comments
 (0)