Skip to content

Commit 4d78ce1

Browse files
Add the TerminateOrphanedConsoleApps option on Windows to kill orphaned console-attached process that may mess up reading from Console input (#3764)
1 parent 33c96af commit 4d78ce1

File tree

6 files changed

+474
-25
lines changed

6 files changed

+474
-25
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ public object ListPredictionTooltipColor
502502
set => _listPredictionTooltipColor = VTColorUtils.AsEscapeSequence(value);
503503
}
504504

505+
public bool TerminateOrphanedConsoleApps { get; set; }
506+
505507
internal string _defaultTokenColor;
506508
internal string _commentColor;
507509
internal string _keywordColor;
@@ -808,6 +810,14 @@ public PredictionViewStyle PredictionViewStyle
808810
[Parameter]
809811
public Hashtable Colors { get; set; }
810812

813+
[Parameter]
814+
public SwitchParameter TerminateOrphanedConsoleApps
815+
{
816+
get => _terminateOrphanedConsoleApps.GetValueOrDefault();
817+
set => _terminateOrphanedConsoleApps = value;
818+
}
819+
internal SwitchParameter? _terminateOrphanedConsoleApps;
820+
811821
[ExcludeFromCodeCoverage]
812822
protected override void EndProcessing()
813823
{

PSReadLine/Options.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using System.Collections;
77
using System.Collections.Generic;
88
using System.Diagnostics.CodeAnalysis;
9+
using System.Globalization;
910
using System.Management.Automation;
1011
using System.Reflection;
12+
using System.Runtime.InteropServices;
1113
using System.Threading;
1214
using Microsoft.PowerShell.PSReadLine;
1315

@@ -167,6 +169,22 @@ private void SetOptionsInternal(SetPSReadLineOption options)
167169
}
168170
}
169171
}
172+
if (options._terminateOrphanedConsoleApps.HasValue)
173+
{
174+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
175+
{
176+
Options.TerminateOrphanedConsoleApps = options.TerminateOrphanedConsoleApps;
177+
PlatformWindows.SetTerminateOrphanedConsoleApps(Options.TerminateOrphanedConsoleApps);
178+
}
179+
else
180+
{
181+
throw new PlatformNotSupportedException(
182+
string.Format(
183+
CultureInfo.CurrentUICulture,
184+
PSReadLineResources.OptionNotSupportedOnNonWindows,
185+
nameof(Options.TerminateOrphanedConsoleApps)));
186+
}
187+
}
170188
}
171189

172190
private void SetKeyHandlerInternal(string[] keys, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock)

PSReadLine/PSReadLine.format.ps1xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ $d = [Microsoft.PowerShell.KeyHandler]::GetGroupingDescription($_.Group)
164164
<ListItem>
165165
<PropertyName>PredictionViewStyle</PropertyName>
166166
</ListItem>
167+
<ListItem>
168+
<PropertyName>TerminateOrphanedConsoleApps</PropertyName>
169+
</ListItem>
167170
<ListItem>
168171
<Label>CommandColor</Label>
169172
<ScriptBlock>[Microsoft.PowerShell.VTColorUtils]::FormatColor($_.CommandColor)</ScriptBlock>

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,7 @@ Or not saving history with:
873873
<data name="UpcaseWordDescription" xml:space="preserve">
874874
<value>Find the next word starting from the current position and then make it upper case.</value>
875875
</data>
876+
<data name="OptionNotSupportedOnNonWindows" xml:space="preserve">
877+
<value>The '{0}' option is not supported on non-Windows platforms.</value>
878+
</data>
876879
</root>

0 commit comments

Comments
 (0)