Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure uncaught throwables in task fibers are visible to the user #397

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading