Fix destruction of PosixEventDriverEvents event.

The reference count was one too high and the waiter count was not properly decremented when an event got destroyed.
This commit is contained in:
Sönke Ludwig 2017-11-23 02:40:39 +01:00
parent aa9a08f571
commit c8fd340240

View file

@ -49,6 +49,7 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
m_loop.m_fds[id].specific = EventSlot(new ConsumableQueue!EventCallback, false, is_internal); // FIXME: avoid dynamic memory allocation m_loop.m_fds[id].specific = EventSlot(new ConsumableQueue!EventCallback, false, is_internal); // FIXME: avoid dynamic memory allocation
m_loop.registerFD(id, EventMask.read); m_loop.registerFD(id, EventMask.read);
m_loop.setNotifyCallback!(EventType.read)(id, &onEvent); m_loop.setNotifyCallback!(EventType.read)(id, &onEvent);
releaseRef(id); // setNotifyCallback increments the reference count, but we need a value of 1 upon return
return id; return id;
} else { } else {
auto addr = new InternetAddress(0x7F000001, 0); auto addr = new InternetAddress(0x7F000001, 0);
@ -56,6 +57,7 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
if (s == DatagramSocketFD.invalid) return EventID.invalid; if (s == DatagramSocketFD.invalid) return EventID.invalid;
m_sockets.receive(s, m_buf, IOMode.once, &onSocketData); m_sockets.receive(s, m_buf, IOMode.once, &onSocketData);
m_events[s] = EventSlot(new ConsumableQueue!EventCallback, false, is_internal); // FIXME: avoid dynamic memory allocation m_events[s] = EventSlot(new ConsumableQueue!EventCallback, false, is_internal); // FIXME: avoid dynamic memory allocation
m_sockets.releaseRef(s); // receive() increments the reference count, but we need a value of 1 upon return
return cast(EventID)s; return cast(EventID)s;
} }
} }
@ -140,13 +142,14 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
assert(getRC(descriptor) > 0, "Releasing reference to unreferenced event FD."); assert(getRC(descriptor) > 0, "Releasing reference to unreferenced event FD.");
void destroy() { void destroy() {
() @trusted nothrow { () @trusted nothrow {
scope (failure) assert(false); try .destroy(getSlot(descriptor).waiters);
.destroy(getSlot(descriptor).waiters); catch (Exception e) assert(false, e.msg);
assert(getSlot(descriptor).waiters is null);
} (); } ();
} }
version (linux) { version (linux) {
if (--getRC(descriptor) == 0) { if (--getRC(descriptor) == 0) {
if (!isInternal(descriptor))
m_loop.m_waiterCount -= getSlot(descriptor).waiters.length;
destroy(); destroy();
m_loop.unregisterFD(descriptor, EventMask.read); m_loop.unregisterFD(descriptor, EventMask.read);
m_loop.clearFD(descriptor); m_loop.clearFD(descriptor);
@ -155,6 +158,8 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
} }
} else { } else {
if (!m_sockets.releaseRef(cast(DatagramSocketFD)descriptor)) { if (!m_sockets.releaseRef(cast(DatagramSocketFD)descriptor)) {
if (!isInternal(descriptor))
m_loop.m_waiterCount -= getSlot(descriptor).waiters.length;
destroy(); destroy();
m_events.remove(cast(DatagramSocketFD)descriptor); m_events.remove(cast(DatagramSocketFD)descriptor);
return false; return false;