Skip to content

Commit

Permalink
test_dns: Fix FD leak
Browse files Browse the repository at this point in the history
The listening TCP socket is closed by `with_udp_and_tcp` helper, but
the connected socket is leaking.

```
Leaked file descriptor: TestResolvDNS#test_multiple_servers_with_timeout_and_truncated_tcp_fallback: 12 : #<TCPSocket:fd 12, AF_INET, 127.0.0.1, 50888>
COMMAND     PID     USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
ruby    3248055 chkbuild   12u  IPv4 112546322      0t0  TCP localhost:50888->localhost:40112 (CLOSE_WAIT)
```

For the purpose of the test case to simulate a timeout over TCP
transport, we have to delay closing this socket until the end the test
case.

Fixup: #50
  • Loading branch information
hanazuki committed Sep 1, 2024
1 parent d9bb1a7 commit 236c38b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions test/resolv/test_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ def test_multiple_servers_with_timeout_and_truncated_tcp_fallback
u1.send(msg[0...512], 0, client_address, client_port)
end

tcp_server1_thread = Thread.new { t1.accept; t1.close }
tcp_server1_thread = Thread.new do
# Keep this socket open so that the client experiences a timeout
t1.accept
end

tcp_server2_thread = Thread.new do
ct = t2.accept
Expand Down Expand Up @@ -800,7 +803,7 @@ def test_multiple_servers_with_timeout_and_truncated_tcp_fallback
ct.send(msg, 0)
ct.close
end
result, = assert_join_threads([client_thread, udp_server1_thread, tcp_server1_thread, tcp_server2_thread])
result, _, tcp_server1_socket, = assert_join_threads([client_thread, udp_server1_thread, tcp_server1_thread, tcp_server2_thread])
assert_instance_of(Array, result)
assert_equal(50, result.length)
result.each_with_index do |rr, i|
Expand All @@ -809,6 +812,8 @@ def test_multiple_servers_with_timeout_and_truncated_tcp_fallback
assert_equal("192.0.2.#{i}", rr.address.to_s)
assert_equal(3600, rr.ttl)
end
ensure
tcp_server1_socket&.close
end
end
end
Expand Down

0 comments on commit 236c38b

Please sign in to comment.