Skip to content

Allow Breakaway OK on SSH Job #1032

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

Closed
jborean93 opened this issue Jan 23, 2018 · 11 comments
Closed

Allow Breakaway OK on SSH Job #1032

jborean93 opened this issue Jan 23, 2018 · 11 comments
Assignees

Comments

@jborean93
Copy link

"OpenSSH for Windows" version
1.0.0.0

Server OperatingSystem
Windows Server 2016 Standard Evaluation
Still applies to all other hosts as well

Client OperatingSystem
N/A (tested on MacOS though)

What is failing
The ssh-shellhost.exe process that is spawned on an SSH connection does not allow processes to breakaway from the job. This means any process it spawns won't be able to breakaway from the SSH job. This is useful if you want to run a process while not keeping the SSH connection open.

As you can see by Process Explorer, the only limit placed on the Job is Kill on Job Close

image

Compared to a process that is spawned by WinRM, it contains some more job limits that are mostly configurable by the service. The key one I am looking for is Breakaway OK which allows the process to call the CreateProcess (and derivative) the CREATE_BREAKAWAY_FROM_JOB creation flag https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx. You can see this based on the cmd.exe process that I spawned on a WinRM process

image

I'm not sure if this is intentional on your part but it would be good to hear your thoughts on this as this is a key feature Ansible uses to achieve async support on the current WinRM connection. If we were to loose this feature, our options are limited and we may need to resort to scheduled tasks which aren't really that easy to manage.

On a side note this is not a "regression" of the 1.0.0.0 release as this has been the case for the past few releases.

@manojampalam
Copy link
Contributor

This is intentional to keep the implementation in line with how it works on Unix.
Otherwise, you may use Task Scheduler to achieve background job/process execution.

@jborean93
Copy link
Author

jborean93 commented Jan 23, 2018

@manojampalam thanks for the update, we will have to see what our other options are. In regards to keeping things how they work on Unix, I'm not 100% confident as to how it all works but you can easily run a process "outside" of the SSH shell. For example I can run

nohup cmd="echo"; for i in {1..50}; do $cmd "count: $i"; sleep 1;done > stdout.txt &

Once I exit from the SSH session, the process is still running in the background and the SSH tty no longer exists when running who. I would have thought this would be similar to Windows when running CreateProcess with the CREATE_BREAKAWAY_FROM_JOB creation flag on the process.

IMO task scheduler is a massive pain when it comes to things like this as;

  • You need to be an administrator to create the jobs
  • Cleanup can fail causing all sorts of issues
  • The API to interact with them is not fun/simple to use
  • Hard to get the stdout/stderr of the task
  • The reporting on the task result is difficult to determine
  • Windows 10 Anniversary Update/Server 1709 seems to have issues with Task Scheduler which is worrysome

Ultimately if that is what we need to do then that's what we need to do but I'm hoping to avoid it if possible :)

@nitzmahone
Copy link

@manojampalam Agree 100% with @jborean93 - the job handling is a fine way to deal with cleaning up after a session closes, but processes that explicitly request breakaway should be allowed to do so (ala nohup on POSIX systems).

@manojampalam manojampalam added this to the 1.0.1.0 milestone Jan 24, 2018
@manojampalam
Copy link
Contributor

manojampalam commented Jan 24, 2018

Thanks for following up and apologies for a misunderstanding of the original ask.
Yes, we should allow child processes to break away at their liberty. This will be fixed in the next drop.

Attached a private if you want to try out.
ssh-shellhost.zip

@jborean93
Copy link
Author

Fantastic, I just tried it out locally and it works as you say, it even fixed another issue where we were creating some jobs, looking forward to the release.

manojampalam added a commit to PowerShell/openssh-portable that referenced this issue Jan 26, 2018
PowerShell/Win32-OpenSSH#1032
Issue: Ssh-shellhost.exe creates a job object and ties all its child processes to its lifetime. It does not allow the child processes to break away from this job. This restriction is unnecessary.
Fix: Modified job object to allow child processes to break away if they need to.
@manojampalam manojampalam self-assigned this Jan 27, 2018
@RDCH106
Copy link

RDCH106 commented Apr 17, 2018

Hi!

I am using Win32-OpenSSH and have read this issue searching a solution to the process killing when the ssh connection is closed.

According to the release notes version v7.6.0.0p1-Beta let applications break away from ssh session. I am using version v7.6.1.0p1-Beta 64 bits version but I can not launch processes in background and they don't continue working when the ssh session is closed.

What is the way to achieve this?

Thanks in advance!

@jborean93
Copy link
Author

jborean93 commented Apr 17, 2018

@RDCH106, we currently achieve this through some P/Invoke calls in PoweShell and call the CreateProcess Win32 API with a specific flag. This isn't something simple to do and requires a bit of code to achieve but the easiest way I know of to do this in PowerShell is to use WMI. Here is an example script that uses Win32_Process as sets the CREATE_BREAKAWAY_FROM_JOB flag when creating the process

$proc_info = New-CimInstance -CimClass (Get-CimClass -ClassName Win32_ProcessStartup) -Property @{CreateFlags=16777216} -ClientOnly
$arguments = @{CommandLine="cmd.exe"; ProcessStartupInformation=$proc_info}
Invoke-CimMethod -ClassName Win32_Process -Name Create -Arguments $arguments

@RDCH106
Copy link

RDCH106 commented Apr 17, 2018

@jborean93 Thanks for your answer!

@DerekBennett
Copy link

I am using OpenSSH-Win64-v7.7.0.0p1-Beta to invoke a BAT file remotely. The BAT file sets some environment variables and then calls START "" someprogram.exe. This used to work just fine in prior releases of SSH. Now, however, START no longer works. Instead the someprogram.exe instance dies as soon as the BAT file exits. Is this a bug, or should I be doing something differently?

@manojampalam
Copy link
Contributor

@DerekBennett can you please open a new issue and add a reference to this thread?

@DerekBennett
Copy link

DerekBennett commented Jun 21, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants