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 2, 2024
1 parent 3d79bbf commit be69b76
Show file tree
Hide file tree
Showing 2 changed files with 28 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
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 be69b76

Please sign in to comment.