Skip to content

Conversation

dinhngtu
Copy link
Contributor

@dinhngtu dinhngtu commented Jan 19, 2021

PR Summary

Fixes #13356 by adding custom functions for killing and waiting for SSH processes. Requires PowerShell/PowerShell-Native#63.

PR Context

On Unix, after a SSH session finishes, PowerShell uses System.Diagnostics.Process.Kill() to stop the process, but doesn't reap the process with waitpid(). This causes SSH to become a defunct (zombie) process.

PR Checklist

@dinhngtu dinhngtu changed the title Wait for SSH exit when closing remote connection WIP: Wait for SSH exit when closing remote connection Jan 19, 2021
Comment on lines 2480 to 2518
Copy link
Collaborator

@iSazonov iSazonov Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this in libpsl? Why not call from libc directly? Search by DllImport("libc" for examples.

Also we should add [return: MarshalAs(UnmanagedType.Bool)].

Copy link
Contributor Author

@dinhngtu dinhngtu Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signals and other constants don't have a defined, standardized value, so I prefer to wrap it in libpsl and use the header constants there rather than coding it into S.M.A.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see we use these constants - I see only 2 parameters and return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are referenced in libpsl wrappers: PowerShell/PowerShell-Native#63

If you called directly into libc from S.M.A then you'd need to call kill(pid, SIGKILL) and waitpid(pid, NULL, WNOHANG).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't both POSIX?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX doesn't define the specific values of either constants (though SIGKILL should equal 9 most of the time).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do they really different on supported platforms?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked into all platforms but following POSIX should be more portable than hardcoding the constants in S.M.A. While it might work with SIGKILL I doubt it'll be very reliable to hardcode WNOHANG. In addition, if we directly imported kill()/waitpid() in S.M.A it would then be possible to use them directly with nonportable constants, so I prefer to restrict the call interface in this fashion.

@ghost ghost added the Review - Needed The PR is being reviewed label Jan 28, 2021
@ghost
Copy link

ghost commented Jan 28, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@adityapatwardhan
Copy link
Member

@dinhngtu Just merged the PR in PowerShell Native.

@ghost ghost removed the Review - Needed The PR is being reviewed label May 17, 2021
@dinhngtu dinhngtu changed the title WIP: Wait for SSH exit when closing remote connection Wait for SSH exit when closing remote connection May 18, 2021
@dinhngtu
Copy link
Contributor Author

@dinhngtu Just merged the PR in PowerShell Native.

Should I update something in powershell to use the new libpsl? I had to manually replace libpsl-native.so to test the new native library.

@iSazonov
Copy link
Collaborator

iSazonov commented May 19, 2021

@dinhngtu It is still unpublished https://www.nuget.org/packages/Microsoft.PowerShell.Native/

Native binaries for PowerShell Core

@iSazonov
Copy link
Collaborator

@dinhngtu 7.2.0-preview.2 was published - please update the reference in csproj file.

@iSazonov
Copy link
Collaborator

@daxian-dbw @TravisEz13 Could you please clarify what is "1 workflow awaiting approval"? Is it CodeQL? Can I approve?

@ghost ghost added the Review - Needed The PR is being reviewed label May 28, 2021
@ghost
Copy link

ghost commented May 28, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@adityapatwardhan adityapatwardhan merged commit ea19cea into PowerShell:master Jun 3, 2021
@ghost ghost removed the Review - Needed The PR is being reviewed label Jun 3, 2021
@adityapatwardhan adityapatwardhan added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Jun 3, 2021
@adityapatwardhan adityapatwardhan added this to the 7.2.0-preview.7 milestone Jun 3, 2021
@dinhngtu dinhngtu deleted the ssh-waitfix branch June 3, 2021 20:21
@iSazonov
Copy link
Collaborator

iSazonov commented Jun 4, 2021

@dinhngtu Thanks for your contribution!

@ghost
Copy link

ghost commented Jun 17, 2021

🎉v7.2.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

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

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Defunct ssh processes and CPU load are kept after executing powershell through ssh on linux

3 participants