kbot7 / NgrokAspNetCore

Integration of Ngrok with the AspNetCore pipeline. Tools to automatically create ngrok tunnels on application startup
MIT License
25 stars 10 forks source link

Fetching tunnel via INgrokHostedService a second time never finishes #27

Open WizX20 opened 4 years ago

WizX20 commented 4 years ago

See the example at: https://github.com/kg73/NgrokAspNetCore/issues/24#issuecomment-621046267

Steps to reproduce (while debugging the example)

Enxyphered commented 4 years ago

I'm having the same issue.

kbot7 commented 4 years ago

Hi All - I'll be taking a look at this issue today. Are you experiencing this in the 1.0.6 version, or the 2.0.0-alpha1 version?

kbot7 commented 4 years ago

Following the repro steps on the latest develop branch, I'm seeing this error, and the app hanging as described. image

Looks to be 2 issues: 1) The managed ngrok.exe is not shutting down correctly when stopping debugging in VS. It does appear to work correctly though when exiting the console dotnet app via CTRL-C 2) The app hangs when there's an issue with the ngrok process. There's a hard coded time out of 5 minutes, and it appears to work correctly after that time out expires. This behavior should be adjusted to not hang the app, and the timeout made configurable.

kbot7 commented 4 years ago

Regarding the first part of the issue, when shutting down debugging from VS, it forces shutdown instead of following graceful shutdown, therefore the lifecycle hooks to stop the managed ngrok process do not execute. The end result is an orphaned ngrok process remains after debugging ends. There does not appear to be a workaround in VS to stop this behavior.

The orphaned ngrok process can be found in task manager under a VS debugger console image

Relevant issue: https://stackoverflow.com/questions/55499755/gracefully-stopping-asp-net-core-web-app-from-within-visual-studio-debugger-a

WizX20 commented 4 years ago

I am now using CLI to check if the service is already running, for example:

var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var rootPath = $"{assemblyPath}\\Deployment\\Resources\\Ngrok";
var programName = "ngrok.exe";

// Check if the ngrok service is running.
var findResult = await Cli.Wrap("cmd")
    .WithWorkingDirectory(@"C:\Windows\System32")
    .WithArguments($"/C TASKLIST | FINDSTR {programName} || START \"\" \"{rootPath}\\{programName}\"")
    .ExecuteBufferedAsync();

After some time, the ngrok tunnel is closed by the ngrok serviec itself; I have not yet found a way to detect that.

hhoangnl commented 3 years ago

Could this be the solution? https://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed

Marco Regueira's answer.