-
Notifications
You must be signed in to change notification settings - Fork 22
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
Cannot debug graceful shutdown stop events #451
Comments
One clarifying question -- From the "my ideal here would be that ... would perform a graceful shutdown" I take it that at least part of this request is to change the stop debugging behavior as you have described. Is that your entire request, or are you also having problems where if you trigger shutdown in some other way the debugger exits? If so, what are you trying? Currently, stop debugging will just terminate the process, and no additional code will get to run. Closing the browser will be equivalent to stop debugging. I don't think we would want to change stop debugging, but I could potentially see changing the browser close behavior and/or introducing another command that would be a graceful terminate. Today, what I think would work would be to open a command window and execute something like |
Hello Gregg, Thanks very much for your reply. My use case is that on app startup I'm pulling in content from a GitHub repo and on shutdown I wanted to delete those files to ensure a clean restart. I currently have no other triggers in the app, but was assuming (incorrectly I now think) that hitting 'Restart' in the Azure portal (App Service) would cycle the app rather than the entire container. From a debug perspective, the repo content gets added to wwwroot and is part of the binding mounts, so I'd also like the content to be removed when I stop debugging. What's confused me is my assumption that stopping debugging in VS would hit all of the lifecycle events before killing the container. I also assumed that there was a problem in my BackgroundService code, but I now understand that this is by design / expected behaviour. If there were a launchSettings property to adjust browser shutdown behaviour or another command as you suggest, that would be great. Re the workaround - that works thank you. It enabled me to hit all of the events:
For anyone else, to get it to work in my default (debian) image I carried out the following (where 'RazorPages' is the container name): Add procps to the base stage in DockerFile (I found I couldn't run kill without this) Get the process ID for the app dll Call kill with SIGINT to the target process as per your instructions: Anyway, thank you very much for the insight and workaround. Best regards John |
@JohnGoldsmith a recommendation on your workaround: You can add a stage to your Dockerfile that is only used for debugging to install procps into so that it doesn't add to your final image. For an example see the testCerts stage used in my certs example. |
Glad to hear you're unblocked |
Thank you @NCarlsonMSFT for that link. That's helped my understanding a lot. With this workaround I can now debug the shutdown events, which is great. I think I would still put my hand up for a low/no touch solution to have code run on shutdown as it something I know I want to happen at the end of every debug session. Anyway, thanks again to you both for the workaround. For anyone else, the setup now looks like this: Dockerfile (added withprocps stage) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM base AS withprocps
USER root
RUN apt-get update && apt-get install -y procps
USER $APP_UID
# remaining stages... .csproj <PropertyGroup>
<DockerfileFastModeStage>withprocps</DockerfileFastModeStage>
</PropertyGroup> MyBackgroundSvc.cs public Task StartingAsync(CancellationToken cancellationToken)
{
logger.LogInformation("STARTING_ASYNC was called.");
// Log cmd for easy copy/paste
var processId = Process.GetCurrentProcess().Id;
logger.LogInformation($"GRACEFUL SHUTDOWN COMMAND:\ndocker exec RazorPages kill -2 {processId}");
return Task.CompletedTask;
} |
Hi, I'm unable to hit stop events from BackgroundService while debugging in Visual Studio 2022 (17.12.3) and docker desktop (4.36.0).
Neither registering a delegate nor implementing IHostedLifecycleService appear to work. If you step out of docker and just run the standard https profile then it works fine.
From my limited understanding it seems like SIGTERM isn't handled by 'VS / docker desktop' and the process gets killed rather than going through the stopping / stopped events.
My ideal here would be that either closing the browser (if launched) or Stop Debugging (Shift+F5) would perform a graceful shutdown.
I've tested this with the default razor pages template (with docker support) plus the following:
Program.cs
builder.Services.AddHostedService<MyBackgroundSvc>();
MyBackgroundSvc.cs
The text was updated successfully, but these errors were encountered: