From c8ab6ae2f8a8ec4d343457c7b280144952ddcf36 Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Thu, 20 Dec 2018 02:10:40 +0100 Subject: [PATCH 1/2] Make waitForData not assert fail on close fix #110 --- source/vibe/core/net.d | 8 ++++- tests/issue-110-close-while-waitForData.d | 39 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/issue-110-close-while-waitForData.d diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 2c2a2a6..4cb08e7 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -569,7 +569,13 @@ mixin(tracer); alias waiter = Waitable!(IOCallback, cb => eventDriver.sockets.read(m_socket, m_context.readBuffer.peekDst(), mode, cb), (cb) { cancelled = true; eventDriver.sockets.cancelRead(m_socket); }, - (sock, st, nb) { assert(sock == m_socket); status = st; nbytes = nb; } + (sock, st, nb) { + if (m_socket == StreamSocketFD.invalid) { + cancelled = true; + return; + } + assert(sock == m_socket); status = st; nbytes = nb; + } ); asyncAwaitAny!(true, waiter)(timeout); diff --git a/tests/issue-110-close-while-waitForData.d b/tests/issue-110-close-while-waitForData.d new file mode 100644 index 0000000..e8a7906 --- /dev/null +++ b/tests/issue-110-close-while-waitForData.d @@ -0,0 +1,39 @@ +/+ dub.sdl: + name "test" + dependency "vibe-core" path=".." ++/ +module test; + +import core.time; + +import vibe.core.core; +import vibe.core.net; + +ushort port; +void main() +{ + runTask(&server); + runTask(&client); + + runEventLoop(); +} + +void server() +{ + auto listener = listenTCP(0, (conn) @safe nothrow { + try { sleep(200.msecs); } catch (Exception) {} + }); + port = listener[0].bindAddress.port; +} + +void client() +{ + sleep(100.msecs); + auto tcp = connectTCP("127.0.0.1", port); + runTask({ + sleep(10.msecs); + tcp.close(); + }); + assert(!tcp.waitForData()); + exitEventLoop(true); +} \ No newline at end of file From bc3b319783254e95cfa9a6ee0ef605837690e860 Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Thu, 20 Dec 2018 02:20:41 +0100 Subject: [PATCH 2/2] Fix potential crash if context is null on closed socket If the other side closes the connection this might happen Without this check the following m_context.readBuffer would segfault --- source/vibe/core/net.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 4cb08e7..c183d1d 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -580,7 +580,7 @@ mixin(tracer); asyncAwaitAny!(true, waiter)(timeout); - if (cancelled) return false; + if (cancelled || !m_context) return false; logTrace("Socket %s, read %s bytes: %s", m_socket, nbytes, status);