-
Notifications
You must be signed in to change notification settings - Fork 458
Implement graceful shutdown timeout period #4540
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3247a6c
Implement timeout period for graceful shutdown
liliankasem 23d4cb3
Add unit test
liliankasem 65da3d2
Merge branch 'main' into liliankasem/force-exit
liliankasem f69c492
Address PR feedback
liliankasem 6e1b2b6
Merge branch 'main' into liliankasem/force-exit
liliankasem 99ded62
Address feedback and update tests
liliankasem 96266ee
Use ConsoleCancelEventHandler
liliankasem 582470e
Remove test
liliankasem 042c13b
Update unit tests to use polling helper
liliankasem bb4a8cb
Merge branch 'main' into liliankasem/force-exit
liliankasem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Azure.Functions.Cli.Interfaces; | ||
|
||
namespace Azure.Functions.Cli; | ||
|
||
internal static class CancelKeyHandler | ||
{ | ||
private static readonly TimeSpan _gracefulShutdownPeriod = TimeSpan.FromSeconds(2); | ||
private static IProcessManager _processManager = null!; | ||
private static Action _onShuttingDown; | ||
private static Action _onGracePeriodTimeout; | ||
private static bool _registered = false; | ||
|
||
public static void Register( | ||
IProcessManager processManager, | ||
Action onShuttingDown, | ||
Action onGracePeriodTimeout = null) | ||
{ | ||
if (_registered) | ||
liliankasem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return; | ||
} | ||
|
||
_processManager = processManager ?? throw new ArgumentNullException(nameof(processManager)); | ||
_onShuttingDown = onShuttingDown ?? throw new ArgumentNullException(nameof(onShuttingDown)); | ||
_onGracePeriodTimeout = onGracePeriodTimeout ?? (() => { }); | ||
|
||
Console.CancelKeyPress += HandleCancelKeyPress; | ||
_registered = true; | ||
} | ||
|
||
internal static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e) | ||
{ | ||
_processManager.KillChildProcesses(); | ||
liliankasem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_onShuttingDown?.Invoke(); | ||
|
||
_ = Task.Run(async () => | ||
{ | ||
await Task.Delay(_gracefulShutdownPeriod); | ||
_onGracePeriodTimeout?.Invoke(); | ||
}); | ||
} | ||
|
||
internal static void Dispose() | ||
liliankasem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (_registered) | ||
{ | ||
Console.CancelKeyPress -= HandleCancelKeyPress; | ||
_registered = false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Autofac; | ||
using Azure.Functions.Cli; | ||
using Azure.Functions.Cli.UnitTests.Helpers; | ||
using Xunit; | ||
|
||
public class CancelKeyHandlerTests | ||
{ | ||
[Fact] | ||
public async Task CtrlC_KillsChildImmediately_AndInvokesGracePeriodTimeout() | ||
{ | ||
// Arrange | ||
var ctsShuttingDown = new CancellationTokenSource(); | ||
var ctsGracePeriod = new CancellationTokenSource(); | ||
var mockProcessManager = new TestProcessManager(); | ||
|
||
CancelKeyHandler.Register( | ||
processManager: mockProcessManager, | ||
onShuttingDown: ctsShuttingDown.Cancel, | ||
onGracePeriodTimeout: ctsGracePeriod.Cancel); | ||
|
||
try | ||
{ | ||
// Act | ||
CancelKeyHandler.HandleCancelKeyPress(null, CreateFakeCancelEventArgs()); | ||
|
||
// Assert immediate child process kill | ||
Assert.True(mockProcessManager.KillChildProcessesCalled); | ||
Assert.False(ctsGracePeriod.IsCancellationRequested); | ||
|
||
// Assert graceful timeout is called after ~2 seconds | ||
var gracePeriodInvoked = await Task.Run(() => | ||
ctsGracePeriod.Token.WaitHandle.WaitOne(3000)); // Slight buffer | ||
|
||
Assert.True(gracePeriodInvoked, "Grace period timeout was not triggered."); | ||
} | ||
finally | ||
{ | ||
CancelKeyHandler.Dispose(); | ||
} | ||
} | ||
|
||
internal static ConsoleCancelEventArgs CreateFakeCancelEventArgs() | ||
{ | ||
var constructor = typeof(ConsoleCancelEventArgs) | ||
.GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0]; | ||
|
||
return (ConsoleCancelEventArgs)constructor.Invoke([ConsoleSpecialKey.ControlC]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Azure.Functions.Cli.Interfaces; | ||
|
||
namespace Azure.Functions.Cli.UnitTests.Helpers; | ||
|
||
internal class TestProcessManager : IProcessManager | ||
{ | ||
public bool KillChildProcessesCalled { get; private set; } | ||
|
||
public bool KillMainProcessCalled { get; private set; } | ||
|
||
public void KillChildProcesses() => KillChildProcessesCalled = true; | ||
|
||
public void KillMainProcess() => KillMainProcessCalled = true; | ||
|
||
// Other interface members omitted for this test | ||
public IEnumerable<IProcessInfo> GetProcessesByName(string processName) => null; | ||
|
||
public IProcessInfo GetCurrentProcess() => null; | ||
|
||
public IProcessInfo GetProcessById(int processId) => null; | ||
|
||
public bool RegisterChildProcess(System.Diagnostics.Process childProcess) => false; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.