Skip to content

Commit

Permalink
Merge pull request #4131 from daipom/fix-udp-server-error-over-max_by…
Browse files Browse the repository at this point in the history
…tes-on-windows

Server helper: Suppress error of UDPServer over max_bytes on Windows
  • Loading branch information
ashie authored Apr 10, 2023
2 parents 24c8bdd + 1bd8b00 commit 9170cda
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/fluent/plugin_helper/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ def on_readable_without_sock
data = @sock.recv(@max_bytes, @flags)
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINTR, Errno::ECONNRESET, IOError, Errno::EBADF
return
rescue Errno::EMSGSIZE
# Windows ONLY: This happens when the data size is larger than `@max_bytes`.
@log.info "A received data was ignored since it was too large."
return
end
@callback.call(data)
rescue => e
Expand All @@ -558,6 +562,10 @@ def on_readable_with_sock
data, addr = @sock.recvfrom(@max_bytes)
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINTR, Errno::ECONNRESET, IOError, Errno::EBADF
return
rescue Errno::EMSGSIZE
# Windows ONLY: This happens when the data size is larger than `@max_bytes`.
@log.info "A received data was ignored since it was too large."
return
end
@callback.call(data, UDPCallbackSocket.new(@sock, addr, close_socket: @close_socket))
rescue => e
Expand Down
49 changes: 49 additions & 0 deletions test/plugin_helper/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Dummy < Fluent::Plugin::TestBase
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s

@d = Dummy.new
@d.under_plugin_development = true
@d.start
@d.after_start
end
Expand Down Expand Up @@ -794,6 +795,50 @@ class Dummy < Fluent::Plugin::TestBase
end
end
end

sub_test_case 'over max_bytes' do
data("cut off on Non-Windows", { max_bytes: 32, records: ["a" * 40], expected: ["a" * 32] }, keep: true) unless Fluent.windows?
data("drop on Windows", { max_bytes: 32, records: ["a" * 40], expected: [] }, keep: true) if Fluent.windows?
test 'with sock' do |data|
max_bytes, records, expected = data.values

actual_records = []
@d.server_create_udp(:myserver, @port, max_bytes: max_bytes) do |data, sock|
actual_records << data
end

open_client(:udp, "127.0.0.1", @port) do |sock|
records.each do |record|
sock.send(record, 0)
end
end

waiting(10) { sleep 0.1 until actual_records.size >= expected.size }
sleep 1 if expected.size == 0 # To confirm no record recieved.

assert_equal expected, actual_records
end

test 'without sock' do |data|
max_bytes, records, expected = data.values

actual_records = []
@d.server_create_udp(:myserver, @port, max_bytes: max_bytes) do |data|
actual_records << data
end

open_client(:udp, "127.0.0.1", @port) do |sock|
records.each do |record|
sock.send(record, 0)
end
end

waiting(10) { sleep 0.1 until actual_records.size >= expected.size }
sleep 1 if expected.size == 0 # To confirm no record recieved.

assert_equal expected, actual_records
end
end
end

module CertUtil
Expand Down Expand Up @@ -1575,6 +1620,10 @@ def assert_certificate(cert, expected_extensions)

def open_client(proto, addr, port)
client = case proto
when :udp
c = UDPSocket.open
c.connect(addr, port)
c
when :tcp
TCPSocket.open(addr, port)
when :tls
Expand Down

0 comments on commit 9170cda

Please sign in to comment.