Skip to content

Commit 49169f8

Browse files
thomaslevesquenatemcmaster
authored andcommitted
fix: deadlock when calling Environment.Exit (#301)
1 parent 4f4b644 commit 49169f8

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
[See unreleased changes][unreleased].
44

5+
## [v2.4.3]
6+
* Fix [#292] by [@thomaslevesque] - fix deadlock when `Environment.Exit` is called
7+
8+
[#292]: https://github.com/natemcmaster/CommandLineUtils/issues/292
9+
510
## [v2.4.2]
611
* Fix [#286] - fix deadlock in CTRL+C handling on Windows
712

@@ -384,6 +389,7 @@ Other:
384389
[@sebastienros]: https://github.com/sebastienros
385390
[@SteveBenz]: https://github.com/SteveBenz
386391
[@TheConstructor]: https://github.com/TheConstructor
392+
[@thomaslevesque]: https://github.com/thomaslevesque
387393
[@vpkopylov]: https://github.com/vpkopylov
388394

389395
[unreleased]: https://github.com/natemcmaster/CommandLineUtils/compare/v2.4.0...HEAD

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -886,22 +886,10 @@ void cancelHandler(object o, ConsoleCancelEventArgs e)
886886
e.Cancel = true;
887887
}
888888

889-
#if !NETSTANDARD1_6
890-
void processExitHandler(object o, EventArgs e)
891-
{
892-
handlerCancellationTokenSource.Cancel();
893-
handlerCompleted.Wait();
894-
}
895-
#endif
896-
897889
try
898890
{
899891
// blocks .NET's CTRL+C handler from completing until after async completions are done
900892
_context.Console.CancelKeyPress += cancelHandler;
901-
#if !NETSTANDARD1_6
902-
// blocks .NET's process unloading from completing until after async completions are done
903-
AppDomain.CurrentDomain.ProcessExit += processExitHandler;
904-
#endif
905893

906894
return await command._handler(handlerCancellationTokenSource.Token);
907895
}
@@ -912,9 +900,6 @@ void processExitHandler(object o, EventArgs e)
912900
finally
913901
{
914902
_context.Console.CancelKeyPress -= cancelHandler;
915-
#if !NETSTANDARD1_6
916-
AppDomain.CurrentDomain.ProcessExit -= processExitHandler;
917-
#endif
918903
handlerCompleted.Set();
919904
}
920905
}

src/Hosting.CommandLine/Internal/CommandLineLifetime.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal class CommandLineLifetime : IHostLifetime, IDisposable
2020
private readonly ICommandLineService _cliService;
2121
private readonly IConsole _console;
2222
private readonly IUnhandledExceptionHandler? _unhandledExceptionHandler;
23-
private readonly ManualResetEvent _blockProcessExit = new ManualResetEvent(false);
2423

2524
/// <summary>
2625
/// Creates a new instance.
@@ -81,13 +80,6 @@ public Task WaitForStartAsync(CancellationToken cancellationToken)
8180
}
8281
});
8382

84-
AppDomain.CurrentDomain.ProcessExit += (_, __) =>
85-
{
86-
_applicationLifetime.StopApplication();
87-
// Ensures services are disposed before the application exits.
88-
_blockProcessExit.WaitOne();
89-
};
90-
9183
// Capture CTRL+C and prevent it from immediately force killing the app.
9284
_console.CancelKeyPress += (_, e) =>
9385
{
@@ -100,7 +92,6 @@ public Task WaitForStartAsync(CancellationToken cancellationToken)
10092

10193
public void Dispose()
10294
{
103-
_blockProcessExit.Set();
10495
}
10596
}
10697
}

0 commit comments

Comments
 (0)