Skip to content

Commit 3d75a97

Browse files
committed
Added timeout
1 parent a3e22b2 commit 3d75a97

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ private void CheckRuntimeIdentifier()
104104
}
105105

106106
var osPart = buildInfoRid.Split('-').First();
107-
108107
var mismatch = false;
109108

110109
switch (osPart)
@@ -161,6 +160,14 @@ protected override Task StopCore()
161160
private async Task StartInternal(string startCmd, string args, string directoriy)
162161
{
163162
var tcs = new TaskCompletionSource();
163+
using var cts = new CancellationTokenSource(2 * 60_000); // cancel after 2 minutes
164+
using var _ = cts.Token.Register(() =>
165+
{
166+
// Time is over - let's kill the process and move on
167+
this.process.Cancel();
168+
// We don't want to raise exceptions here - just pass the barrier
169+
tcs.SetResult();
170+
});
164171

165172
void Read_SocketIO_Parameters(object sender, string line)
166173
{
@@ -179,13 +186,26 @@ void Read_SocketIO_Parameters(object sender, string line)
179186
}
180187
}
181188

189+
void Monitor_SocketIO_Failure(object sender, EventArgs e)
190+
{
191+
// We don't want to raise exceptions here - just pass the barrier
192+
if (tcs.Task.IsCompleted)
193+
{
194+
this.Process_Exited(sender, e);
195+
}
196+
else
197+
{
198+
tcs.SetResult();
199+
}
200+
}
201+
182202
try
183203
{
184204
Console.Error.WriteLine("[StartInternal]: startCmd: {0}", startCmd);
185205
Console.Error.WriteLine("[StartInternal]: args: {0}", args);
186206

187207
this.process = new ProcessRunner("ElectronRunner");
188-
this.process.ProcessExited += this.Process_Exited;
208+
this.process.ProcessExited += Monitor_SocketIO_Failure;
189209
this.process.LineReceived += Read_SocketIO_Parameters;
190210
this.process.Run(startCmd, args, directoriy);
191211

@@ -199,11 +219,11 @@ void Read_SocketIO_Parameters(object sender, string line)
199219
Console.Error.WriteLine("[StartInternal]: Process is not running: " + this.process.StandardOutput);
200220

201221
Task.Run(() => this.TransitionState(LifetimeState.Stopped));
202-
203-
throw new Exception("Failed to launch the Electron process.");
204222
}
205-
206-
this.TransitionState(LifetimeState.Ready);
223+
else
224+
{
225+
this.TransitionState(LifetimeState.Ready);
226+
}
207227
}
208228
catch (Exception ex)
209229
{

0 commit comments

Comments
 (0)