Skip to content

Commit

Permalink
Fixed fd leak from TCPServer
Browse files Browse the repository at this point in the history
```
Leaked file descriptor: HTTPSProxyTest#test_https_proxy_ssl_connection: 8 : #<TCPServer:fd 8, AF_INET, 127.0.0.1, 63104>
```
  • Loading branch information
hsbt committed Jul 17, 2024
1 parent 4ee4ce8 commit 659eb3a
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions test/net/http/test_https_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,39 @@ def test_https_proxy_ssl_connection
omit 'autoload problem. see [ruby-dev:45021][Bug #5786]'
end

tcpserver = TCPServer.new("127.0.0.1", 0)
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = OpenSSL::PKey.read(read_fixture("server.key"))
ctx.cert = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
serv = OpenSSL::SSL::SSLServer.new(tcpserver, ctx)
TCPServer.open("127.0.0.1", 0) {|tcpserver|
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = OpenSSL::PKey.read(read_fixture("server.key"))
ctx.cert = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
serv = OpenSSL::SSL::SSLServer.new(tcpserver, ctx)

_, port, _, _ = serv.addr
client_thread = Thread.new {
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password', true)
http = proxy.new("foo.example.org", 8000)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
begin
http.start
rescue EOFError
end
}
server_thread = Thread.new {
sock = serv.accept
begin
proxy_request = sock.gets("\r\n\r\n")
assert_equal(
"CONNECT foo.example.org:8000 HTTP/1.1\r\n" +
"Host: foo.example.org:8000\r\n" +
"Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
"\r\n",
proxy_request,
"[ruby-core:96672]")
ensure
sock.close
end
_, port, _, _ = serv.addr
client_thread = Thread.new {
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password', true)
http = proxy.new("foo.example.org", 8000)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
begin
http.start
rescue EOFError
end
}
server_thread = Thread.new {
sock = serv.accept
begin
proxy_request = sock.gets("\r\n\r\n")
assert_equal(
"CONNECT foo.example.org:8000 HTTP/1.1\r\n" +
"Host: foo.example.org:8000\r\n" +
"Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
"\r\n",
proxy_request,
"[ruby-core:96672]")
ensure
sock.close
end
}
assert_join_threads([client_thread, server_thread])
}
assert_join_threads([client_thread, server_thread])
end
end if defined?(OpenSSL)

0 comments on commit 659eb3a

Please sign in to comment.