Skip to content

Commit

Permalink
Merge pull request #397 from vibe-d/better_fatal_task_error_logging
Browse files Browse the repository at this point in the history
Ensure uncaught throwables in task fibers are visible to the user
  • Loading branch information
l-kramer committed Apr 3, 2024
2 parents 3d79bbf + 7ed58f6 commit b2462e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ copyright "Copyright © 2016-2020, Sönke Ludwig"
license "MIT"

dependency "eventcore" version="~>0.9.27"
dependency "vibe-container" version=">=1.1.0 <2.0.0-0"
dependency "vibe-container" version=">=1.3.1 <2.0.0-0"

targetName "vibe_core"

Expand Down
41 changes: 37 additions & 4 deletions source/vibe/core/task.d
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,45 @@ 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);
// On Windows, also show a message box to the user if the
// application is interactive and does not have a console
// associated
version (Windows) {
import core.sys.windows.windows : GetConsoleWindow, GetProcessWindowStation,
GetUserObjectInformationA, MessageBoxA, MB_ICONERROR, UOI_FLAGS,
USEROBJECTFLAGS, WSF_VISIBLE;
import std.format : formattedWrite;
import vibe.container.internal.appender : BufferOverflowMode, FixedAppender;

if (!GetConsoleWindow()) {
USEROBJECTFLAGS wsf;
GetUserObjectInformationA(GetProcessWindowStation(), UOI_FLAGS, &wsf, wsf.sizeof, null);
if (wsf.dwFlags & WSF_VISIBLE) {
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 b2462e4

Please sign in to comment.