From 9d6b34c73ad2c3c68d60eacbfec899e09f89ac37 Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Wed, 9 Jan 2019 00:18:43 +0100 Subject: [PATCH 1/2] release ref on failing connectTCP fix #115 --- source/vibe/core/net.d | 9 ++++++++- tests/issue-115-connect-fail-leak.d | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/issue-115-connect-fail-leak.d diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 778c2e7..b1d7f34 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -216,7 +216,14 @@ TCPConnection connectTCP(NetworkAddress addr, NetworkAddress bind_address = anyA enforce(!cancelled, "Failed to connect to " ~ addr.toString() ~ ": timeout"); - enforce(status == ConnectStatus.connected, "Failed to connect to "~addr.toString()~": "~status.to!string); + + if (status != ConnectStatus.connected) { + if (sock != SocketFD.invalid) + assert(!eventDriver.sockets.releaseRef(sock)); + + enforce(false, "Failed to connect to "~addr.toString()~": "~status.to!string); + assert(false); + } return TCPConnection(sock, uaddr); } (); diff --git a/tests/issue-115-connect-fail-leak.d b/tests/issue-115-connect-fail-leak.d new file mode 100644 index 0000000..5a6577c --- /dev/null +++ b/tests/issue-115-connect-fail-leak.d @@ -0,0 +1,22 @@ +/+ dub.sdl: + name "test" + dependency "vibe-core" path=".." ++/ +module test; + +import vibe.core.core; +import vibe.core.log; +import vibe.core.net; + +void main() +{ + foreach (_; 0 .. 20) { + TCPConnection conn; + try { + conn = connectTCP("127.0.0.1", 16565); + logError("Connection: %s", conn); + conn.close(); + assert(false, "Didn't expect TCP connection on port 16565 to succeed"); + } catch (Exception) { } + } +} \ No newline at end of file From 4d4401c31f12d5c1ecbcf787510e4f3e03b4848e Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Wed, 9 Jan 2019 00:28:27 +0100 Subject: [PATCH 2/2] Fix assert not compiling in releaseRef --- source/vibe/core/net.d | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index b1d7f34..8eb1bb1 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -218,8 +218,10 @@ TCPConnection connectTCP(NetworkAddress addr, NetworkAddress bind_address = anyA ": timeout"); if (status != ConnectStatus.connected) { - if (sock != SocketFD.invalid) - assert(!eventDriver.sockets.releaseRef(sock)); + if (sock != SocketFD.invalid) { + bool refsLeft = eventDriver.sockets.releaseRef(sock); + assert(!refsLeft); + } enforce(false, "Failed to connect to "~addr.toString()~": "~status.to!string); assert(false);