Skip to content

Commit

Permalink
Merge pull request #229 from vibe-d/fix_macos_shutdown_crash
Browse files Browse the repository at this point in the history
Avoid possible assertion error on macOS/BSD at shutdown
  • Loading branch information
l-kramer authored Jan 12, 2024
2 parents 8a03986 + 19b36af commit 7fa0a15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion source/eventcore/drivers/posix/kqueue.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class KqueueEventLoop : KqueueEventLoopBase {

abstract class KqueueEventLoopBase : PosixEventLoop {
protected {
int m_queue;
int m_queue = -1;
size_t m_changeCount = 0;
kevent_t[100] m_changes;
kevent_t[100] m_events;
Expand Down Expand Up @@ -101,6 +101,7 @@ abstract class KqueueEventLoopBase : PosixEventLoop {
super.dispose();
import core.sys.posix.unistd : close;
close(m_queue);
m_queue = -1;
}

override void registerFD(FD fd, EventMask mask, bool edge_triggered = true)
Expand Down Expand Up @@ -154,6 +155,10 @@ abstract class KqueueEventLoopBase : PosixEventLoop {

private void putChange(ref kevent_t ev)
@safe nothrow @nogc {
if (m_queue == -1) {
printWarningWithStackTrace("Warning: generating kqueue event after the driver has been disposed");
return;
}
m_changes[m_changeCount++] = ev;
if (m_changeCount == m_changes.length) {
auto ret = (() @trusted => kevent(m_queue, &m_changes[0], cast(int)m_changes.length, null, 0, null)) ();
Expand Down
15 changes: 15 additions & 0 deletions source/eventcore/internal/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ void print(ARGS...)(string str, ARGS args)
s.r.put('\n');
}

void printWarningWithStackTrace(ARGS...)(string str, ARGS args)
@trusted @nogc nothrow {
try print(str, args);
catch (Exception e) {}
debug {
try throw new Exception("");
catch (Exception e) {
try {
print("Call stack:");
foreach (ln; e.info) print("%s", ln);
} catch (Exception e2) {}
}
}
}

T mallocT(T, ARGS...)(ARGS args)
{
import core.stdc.stdlib : malloc;
Expand Down

0 comments on commit 7fa0a15

Please sign in to comment.