Skip to content

Commit 7d19681

Browse files
committed
Disable the Windows Error Reporting dialog by default
1 parent ef170a6 commit 7d19681

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/ChildProcess/Interop/Windows/Kernel32.JobObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal static partial class Kernel32
1313
// https://docs.microsoft.com/en-us/windows/win32/procthread/job-objects
1414
// JOBOBJECT_BASIC_LIMIT_INFORMATION.LimitFlags
1515
public const int JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x00000800;
16+
public const int JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION = 0x00000400;
1617
public const int JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000;
1718

1819
[DllImport(DllName, EntryPoint = "CreateJobObjectW", SetLastError = true, CharSet = CharSet.Unicode)]

src/ChildProcess/ProcessManagement/ChildProcessStartInfo.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ public enum ChildProcessFlags
8080
/// This effectively gives you full control over the environment variables of the child process.
8181
/// </summary>
8282
DisableEnvironmentVariableInheritance = 0x0020,
83+
84+
/// <summary>
85+
/// (Windows-specific) Allows the child process to show the Windows Error Reporting dialog.
86+
/// This includes the "foo.dll was not found" dialog and the JIT debug dialog.
87+
/// </summary>
88+
/// <remarks>
89+
/// <para>
90+
/// This flag does not work if the current process has the Windows Error Reporting dialog disabled
91+
/// by calling SetErrorMode. The error mode will be inherited by the child process.
92+
/// </para>
93+
/// <para>
94+
/// This is done by removing the <a href="https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information">
95+
/// JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION</a> flag. (<see cref="ChildProcess"/> adds it by default.)
96+
/// </para>
97+
/// </remarks>
98+
EnableWindowsErrorReportingDialog = 0x0040,
8399
}
84100

85101
/// <summary>
@@ -93,6 +109,7 @@ internal static class ChildProcessFlagsExtensions
93109
public static bool HasAttachToCurrentConsole(this ChildProcessFlags flags) => (flags & ChildProcessFlags.AttachToCurrentConsole) != 0;
94110
public static bool HasDisableArgumentQuoting(this ChildProcessFlags flags) => (flags & ChildProcessFlags.DisableArgumentQuoting) != 0;
95111
public static bool HasDisableEnvironmentVariableInheritance(this ChildProcessFlags flags) => (flags & ChildProcessFlags.DisableEnvironmentVariableInheritance) != 0;
112+
public static bool HasEnableWindowsErrorReportingDialog(this ChildProcessFlags flags) => (flags & ChildProcessFlags.EnableWindowsErrorReportingDialog) != 0;
96113
}
97114

98115
/// <summary>

src/ChildProcess/ProcessManagement/ChildProcessStartInfoInternal.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ public ChildProcessStartInfoInternal(ChildProcessStartInfo startInfo)
9898
}
9999

100100
public bool AllowSignal => !Flags.HasAttachToCurrentConsole();
101+
public bool DisableWindowsErrorReportingDialog => !Flags.HasEnableWindowsErrorReportingDialog();
101102
}
102103
}

src/ChildProcess/ProcessManagement/WindowsChildProcessStateHelper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public unsafe IChildProcessStateHolder SpawnProcess(
5656
}
5757

5858
bool killOnClose = startInfo.AllowSignal && WindowsVersion.NeedsWorkaroundForWindows1809;
59-
jobObjectHandle = CreateJobObject(killOnClose);
59+
jobObjectHandle = CreateJobObject(killOnClose, startInfo.DisableWindowsErrorReportingDialog);
6060

6161
using var inheritableHandleStore = new InheritableHandleStore(3);
6262
var childStdIn = stdIn != null ? inheritableHandleStore.Add(stdIn) : null;
@@ -187,7 +187,9 @@ private static unsafe void ChangeCodePage(
187187
}
188188
}
189189

190-
private static unsafe SafeJobObjectHandle CreateJobObject(bool killOnClose)
190+
private static unsafe SafeJobObjectHandle CreateJobObject(
191+
bool killOnClose,
192+
bool disableWindowsErrorReportingDialog)
191193
{
192194
var jobObjectHandle = Kernel32.CreateJobObject(IntPtr.Zero, null);
193195
try
@@ -198,6 +200,10 @@ private static unsafe SafeJobObjectHandle CreateJobObject(bool killOnClose)
198200
}
199201

200202
var limitFlags = Kernel32.JOB_OBJECT_LIMIT_BREAKAWAY_OK;
203+
if (disableWindowsErrorReportingDialog)
204+
{
205+
limitFlags |= Kernel32.JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
206+
}
201207
if (killOnClose)
202208
{
203209
limitFlags |= Kernel32.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;

0 commit comments

Comments
 (0)