dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.65k forks source link

[question] new process to create new process failed #39060

Open fawdlstty opened 4 years ago

fawdlstty commented 4 years ago

(ASP.Net Core) new process to create new process failed

[Public]
[AcceptVerbs ("GET", "POST")]
public IActionResult update () {
    _ = Task.Run (async () => {
        _hostApplicationLifetime.StopApplication ();
        var (_p1, _p2) = RuntimeInformation.IsOSPlatform (OSPlatform.Linux)
            ? ("nohup", $"./{Assembly.GetExecutingAssembly ().GetName ().Name} &")
            : ("dotnet", Assembly.GetExecutingAssembly ().Location);
        using (var _ps2 = Process.Start (new ProcessStartInfo (_p1, _p2) {
            WorkingDirectory = Environment.CurrentDirectory,//AppContext.BaseDirectory
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        })) { }
        await Task.Delay (1000);
        Environment.Exit (0);
    });
    return ApiReturns.Success ();
}

I use this code to restart the server under Linux, the effect is always, the first time to create successfully and exit itself, the second time (new process) to create failed and exit itself, what is the cause of this problem, how to fix?

General

centos7.5 .net core 3.1 (current latest version) independent publish

Pilchie commented 4 years ago

I don't think this is specific to ASP.NET. I think you need to look at the standard input/output/logs/etc to see what's going wrong.

Pilchie commented 4 years ago

@carlossanlop I would move this to the runtime repo for Process.Start.

fawdlstty commented 4 years ago

I don't think this is specific to ASP.NET too, but it's not standard output

scalablecory commented 4 years ago

Is it possible the 2nd process is trying to bind to a port that the 1st process still has, and bombing?

fawdlstty commented 4 years ago

_hostApplicationLifetime.StopApplication (); this code may unbind port and the first restart are success

fawdlstty commented 4 years ago

what should I do? close this issue and reopen from runtime?

ghost commented 4 years ago

Tagging subscribers to this area: @eiriktsarpalis Notify danmosemsft if you want to be subscribed.

adamsitnik commented 4 years ago

Hello @fawdlstty

[Public]

First of all, exposing a public API method for stopping (or crashing) your service can allow some attackers to easily take your service down (imagine that someone writes a script that pings this url in a neverending loop). Please don't do it. (cc @GrabYourPitchforks @bartonjs)

I use this code to restart the server under Linux

What do you use to host your app? Most of the web servers should allow for restarting selected services on demand.

Another question I have is why do you want to restart your app? Is it because it has entered a faulty state? Have you considered implementing a Fail Fast approach and configure your web server to reboot the app after every failure?

Thanks, Adam

fawdlstty commented 4 years ago

Another question I have is why do you want to restart your app?

Called by Webhook and used for continuous deployment, the address is normally so complex that there is almost no vulnerability. The software is very small and wants to be as simple as possible, so daemons and docker are not used.

What do you use to host your app?

Hosting mode is Kestrel.