Skip to content

Commit

Permalink
Ensure uncaught throwables in task fibers are visible to the user.
Browse files Browse the repository at this point in the history
Prefers logFatal over stderr.writeln so that the error also ends up in log files that may be configured. Also displays a message box for subsystem "windows" executables that don't have a console attached.
  • Loading branch information
s-ludwig committed Apr 1, 2024
1 parent 3d79bbf commit f13096f
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions source/vibe/core/task.d
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,35 @@ final package class TaskFiber : Fiber {
} catch (Throwable th) {
import std.stdio : stderr, writeln;
import core.stdc.stdlib : abort;
import core.memory : GC;
try {
stderr.writeln("TaskFiber getting terminated due to an uncaught ", th.classinfo.name);
stderr.writeln(th);
version (Windows) {
import core.sys.windows.windows : MessageBoxA, MB_ICONERROR, GetConsoleWindow;
import std.format : formattedWrite;
import vibe.container.internal.appender : BufferOverflowMode, FixedAppender;

if (!GetConsoleWindow()) {
FixedAppender!(char[], 2048, BufferOverflowMode.ignore) msg = void;
msg.formattedWrite("%s: %s\r\n\r\n", th.classinfo.name, th.msg);
foreach (ln; th.info)
msg.formattedWrite("%s\r\n", ln);
msg.put('\0');
msg.data[msg.data.length-1] = '\0';
MessageBoxA(null, msg.data.ptr, "FATAL: Uncaught exception in task fiber", MB_ICONERROR);
}
}
if (GC.inFinalizer) {
stderr.writeln("TaskFiber getting terminated due to an uncaught ", th.classinfo.name, ": ", th.msg);
foreach (ln; th.info) stderr.writeln(ln);
} else {
logFatal("TaskFiber getting terminated due to an uncaught %s: %s", th.classinfo.name, th.msg);
foreach (ln; th.info) logFatal("%s", ln);
}
} catch (Exception e) {
try stderr.writeln(th.msg);
catch (Exception e) {}
if (GC.inFinalizer) {
try stderr.writeln(th.msg);
catch (Exception e) {}
} else logFatal("%s", th.msg);
}
abort();
}
Expand Down

0 comments on commit f13096f

Please sign in to comment.