NgrokAspNetCore icon indicating copy to clipboard operation
NgrokAspNetCore copied to clipboard

Fetching tunnel via INgrokHostedService a second time never finishes

Open WizX20 opened this issue 5 years ago • 6 comments

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

Steps to reproduce (while debugging the example)

  • Open VS and hit debug
  • Ngrok fires up nicely and we can fetch the url using the injected INgrokHostedService
  • Stop the debugging session
  • Notice that Ngrok.exe is still running in the background (see the task manager for example)
  • Fire up a new debugging session
  • The code will never finish and get past, for example var nGrokTunnel = await this.NgrokHostedService?.GetTunnelsAsync();

WizX20 avatar Apr 29 '20 13:04 WizX20

I'm having the same issue.

Enxyphered avatar Jul 12 '20 13:07 Enxyphered

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 avatar Aug 16 '20 14:08 kbot7

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 avatar Aug 16 '20 15:08 kbot7

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

kbot7 avatar Aug 16 '20 15:08 kbot7

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.

WizX20 avatar Oct 27 '20 08:10 WizX20

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

Marco Regueira's answer.

hhoangnl avatar May 21 '21 19:05 hhoangnl