Skip to content

Commit a524096

Browse files
committed
Use job object to control child process
1 parent 73f2c3b commit a524096

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

Flow.Launcher.Core/Flow.Launcher.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<ItemGroup>
5656
<PackageReference Include="Droplex" Version="1.7.0" />
5757
<PackageReference Include="FSharp.Core" Version="7.0.401" />
58+
<PackageReference Include="Meziantou.Framework.Win32.Jobs" Version="3.2.1" />
5859
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
5960
<PackageReference Include="squirrel.windows" Version="1.5.2" NoWarn="NU1701" />
6061
<PackageReference Include="StreamJsonRpc" Version="2.17.8" />

Flow.Launcher.Core/Plugin/ProcessStreamPluginV2.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55
using System.Threading.Tasks;
66
using Flow.Launcher.Infrastructure;
77
using Flow.Launcher.Plugin;
8+
using Meziantou.Framework.Win32;
89
using Nerdbank.Streams;
910

1011
namespace Flow.Launcher.Core.Plugin
1112
{
1213
internal abstract class ProcessStreamPluginV2 : JsonRPCPluginV2
1314
{
15+
private static JobObject _jobObject = new JobObject();
16+
17+
static ProcessStreamPluginV2()
18+
{
19+
_jobObject.SetLimits(new JobObjectLimits()
20+
{
21+
Flags = JobObjectLimitFlags.KillOnJobClose | JobObjectLimitFlags.DieOnUnhandledException
22+
});
23+
24+
_jobObject.AssignProcess(Process.GetCurrentProcess());
25+
}
1426

1527
public override string SupportedLanguage { get; set; }
1628
protected sealed override IDuplexPipe ClientPipe { get; set; }
@@ -30,22 +42,22 @@ public override async Task InitAsync(PluginInitContext context)
3042

3143
ClientProcess = Process.Start(StartInfo);
3244
ArgumentNullException.ThrowIfNull(ClientProcess);
33-
45+
3446
SetupPipe(ClientProcess);
3547

3648
ErrorStream = ClientProcess.StandardError;
3749

3850
await base.InitAsync(context);
3951
}
40-
52+
4153
private void SetupPipe(Process process)
4254
{
4355
var (reader, writer) = (PipeReader.Create(process.StandardOutput.BaseStream),
4456
PipeWriter.Create(process.StandardInput.BaseStream));
4557
ClientPipe = new DuplexPipe(reader, writer);
4658
}
47-
48-
59+
60+
4961
public override async Task ReloadDataAsync()
5062
{
5163
var oldProcess = ClientProcess;
@@ -57,8 +69,8 @@ public override async Task ReloadDataAsync()
5769
await oldProcess.WaitForExitAsync();
5870
oldProcess.Dispose();
5971
}
60-
61-
72+
73+
6274
public override async ValueTask DisposeAsync()
6375
{
6476
ClientProcess.Kill(true);

0 commit comments

Comments
 (0)