From aa3659fca61f162d726ebe38aa7993df27f1b3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 19 May 2020 22:53:29 +0200 Subject: [PATCH] Handle redundant cancelReceive calls gracefully and clean up the callback. --- source/eventcore/drivers/posix/sockets.d | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index fe6aaea..1f8557a 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -866,9 +866,12 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets @nogc { if (!isValid(socket)) return; - assert(m_loop.m_fds[socket].datagramSocket.readCallback !is null, "Cancelling read when there is no read in progress."); + auto slot = () @trusted { return &m_loop.m_fds[socket].datagramSocket(); } (); + if (slot.readCallback is null) return; + m_loop.setNotifyCallback!(EventType.read)(socket, null); - m_loop.m_fds[socket].datagramSocket.readBuffer = null; + slot.readCallback = null; + slot.readBuffer = null; } private void onDgramRead(FD fd) @@ -894,7 +897,9 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets auto l = lockHandle(socket); m_loop.setNotifyCallback!(EventType.read)(socket, null); scope src_addrc = new RefAddress(() @trusted { return cast(sockaddr*)&src_addr; } (), src_addr.sizeof); - () @trusted { return cast(DatagramIOCallback)slot.readCallback; } ()(socket, IOStatus.ok, ret, src_addrc); + auto cb = () @trusted { return cast(DatagramIOCallback)slot.readCallback; } (); + slot.readCallback = null; + cb(socket, IOStatus.ok, ret, src_addrc); } void send(DatagramSocketFD socket, const(ubyte)[] buffer, IOMode mode, Address target_address, DatagramIOCallback on_send_finish)