Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep TCPServer.open(port) working with the Happy Eyeballs patch

Gerhard Schlager6b89982a91ec
diff --git a/lib/freedom_patches/final_destination_connect.rb b/lib/freedom_patches/final_destination_connect.rb
index 2eda144c3c6..1cae62af383 100644
--- a/lib/freedom_patches/final_destination_connect.rb
+++ b/lib/freedom_patches/final_destination_connect.rb
@@ -21,9 +21,16 @@ end
# getaddrinfo patch. Instead, open the socket with `Socket.tcp`, then set up a `TCPSocket`
# instance with the result.
module FinalDestinationTCPSocketPatch
- def open(host, port, local_host = nil, local_port = nil, **kwargs)
+ # Takes *args because TCPServer inherits this singleton method and
+ # `TCPServer.open(port)` has fewer arguments — a fixed host/port signature
+ # fails the arity check before `super` can run. A bare `super` forwards the
+ # original arguments untouched.
+ def open(*args, **kwargs)
+ host = args[0]
return super unless FinalDestination::Connector.token?(host)
+ _, port, local_host, local_port = args
+
socket =
Socket.tcp(
host,
diff --git a/spec/lib/final_destination/http_spec.rb b/spec/lib/final_destination/http_spec.rb
index baec18093d6..45f88bda929 100644
--- a/spec/lib/final_destination/http_spec.rb
+++ b/spec/lib/final_destination/http_spec.rb
@@ -216,6 +216,15 @@ describe FinalDestination::HTTP do
socket.close
end
end
+
+ it "does not break TCPServer.open with only a port argument" do
+ # TCPServer inherits TCPSocket's singleton methods, and TCPServer.open
+ # takes just a port; the patch must keep that shorter form working.
+ server = TCPServer.open(0)
+ expect(server).to be_a(TCPServer)
+ ensure
+ server&.close
+ end
end
describe "via a proxy" do