Skip to content

Commit 74b1757

Browse files
committed
Keep CLI installation process owned through timeout
1 parent 1ad0281 commit 74b1757

2 files changed

Lines changed: 317 additions & 4 deletions

File tree

‎CodexSharpSDK.Tests/Unit/CliInstallationTests.cs‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public sealed class CliInstallationTests
4343
private const string PipeHolderPidFileName = "pipe-holder.pid";
4444
private const string SetsidCommandName = "setsid";
4545
private const string CleanupMessage = "CLI installation process or output cleanup could not be confirmed within its configured timeout.";
46+
private const string TimeoutMessage = "CLI installation exceeded its configured process timeout.";
4647
private const string DetachedPipeHolderSkipReason = "The detached pipe-holder cleanup fixture requires Linux setsid.";
4748
private const string SetsidRequiredMessage = "The detached pipe-holder cleanup fixture requires Linux setsid.";
4849
private const string OverflowMarkerName = "overflow";
@@ -94,6 +95,7 @@ public sealed class CliInstallationTests
9495
process.stdout.write('x'.repeat(8192));
9596
setInterval(() => {}, 1000);
9697
}
98+
fs.writeFileSync(path.join(__dirname, 'child.pid'), String(process.pid));
9799
console.log('installation output');
98100
console.error('installation diagnostics');
99101
""";
@@ -162,6 +164,140 @@ await Assert.That(updates.Any(update => update.Stage == CliInstallationStage.Out
162164
}
163165
}
164166

167+
[Test]
168+
public async Task InstallOrUpdate_ReportsFinalOutputWhenConsumerResumesAfterProcessExit()
169+
{
170+
var fixture = await CreateFixtureAsync();
171+
try
172+
{
173+
await using var updates = CodexCliInstallation.InstallOrUpdateAsync(fixture.Options,
174+
fixture.LocalApplicationDataRoot, CancellationToken.None).GetAsyncEnumerator();
175+
await Assert.That(await updates.MoveNextAsync()).IsTrue();
176+
await Assert.That(updates.Current.Stage).IsEqualTo(CliInstallationStage.PackageManagerStarted);
177+
var childPidPath = Path.Combine(fixture.NpmRoot, BinDirectoryName, ChildPidFileName);
178+
using var markerTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(3));
179+
await WaitForFileAsync(childPidPath, markerTimeout.Token);
180+
var childPid = int.Parse(await File.ReadAllTextAsync(childPidPath),
181+
System.Globalization.CultureInfo.InvariantCulture);
182+
await WaitForProcessExitAsync(childPid, markerTimeout.Token);
183+
184+
var sawFinalOutput = false;
185+
var sawInstalled = false;
186+
while (await updates.MoveNextAsync())
187+
{
188+
var update = updates.Current;
189+
sawFinalOutput |= update.Stage == CliInstallationStage.OutputObserved &&
190+
update.StandardOutputCharactersObserved > 0 && update.StandardErrorCharactersObserved > 0;
191+
sawInstalled |= update.Stage == CliInstallationStage.Installed && update.Result is not null;
192+
}
193+
194+
await Assert.That(sawFinalOutput).IsTrue();
195+
await Assert.That(sawInstalled).IsTrue();
196+
}
197+
finally
198+
{
199+
DeleteFixture(fixture.FixtureRoot);
200+
}
201+
}
202+
203+
[Test]
204+
public async Task InstallOrUpdate_EnforcesTimeoutWhileConsumerIsPausedAfterStart()
205+
{
206+
var fixture = await CreateFixtureAsync();
207+
await File.WriteAllTextAsync(Path.Combine(fixture.NpmRoot, BinDirectoryName, HangMarkerName), string.Empty);
208+
var childPidPath = Path.Combine(fixture.NpmRoot, BinDirectoryName, ChildPidFileName);
209+
try
210+
{
211+
var options = fixture.Options with
212+
{
213+
InstallTimeout = TimeSpan.FromSeconds(1),
214+
ProcessTerminationTimeout = TimeSpan.FromSeconds(1)
215+
};
216+
await using var updates = CodexCliInstallation.InstallOrUpdateAsync(options,
217+
fixture.LocalApplicationDataRoot, CancellationToken.None).GetAsyncEnumerator();
218+
await Assert.That(await updates.MoveNextAsync()).IsTrue();
219+
await Assert.That(updates.Current.Stage).IsEqualTo(CliInstallationStage.PackageManagerStarted);
220+
221+
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(5));
222+
await WaitForFileAsync(childPidPath, deadline.Token);
223+
var childPid = int.Parse(await File.ReadAllTextAsync(childPidPath),
224+
System.Globalization.CultureInfo.InvariantCulture);
225+
await WaitForProcessExitAsync(childPid, deadline.Token);
226+
await Assert.That(IsProcessRunning(childPid)).IsFalse();
227+
228+
var exception = await Assert.That(async () =>
229+
{
230+
while (await updates.MoveNextAsync())
231+
{
232+
}
233+
}).ThrowsException();
234+
await Assert.That(exception).IsTypeOf<TimeoutException>();
235+
await Assert.That(exception!.Message).IsEqualTo(TimeoutMessage);
236+
}
237+
finally
238+
{
239+
DeleteFixture(fixture.FixtureRoot);
240+
}
241+
}
242+
243+
[Test]
244+
public async Task InstallOrUpdate_ExitedRootWithDetachedPipeHolderSurfacesBoundedCleanupFailure()
245+
{
246+
if (!OperatingSystem.IsLinux())
247+
{
248+
Skip.Test(DetachedPipeHolderSkipReason);
249+
return;
250+
}
251+
252+
var fixture = await CreateFixtureAsync();
253+
var holderPidFile = Path.Combine(fixture.NpmRoot, BinDirectoryName, PipeHolderPidFileName);
254+
var rootPidFile = Path.Combine(fixture.NpmRoot, BinDirectoryName, ChildPidFileName);
255+
var holderPid = 0;
256+
try
257+
{
258+
await File.WriteAllTextAsync(Path.Combine(fixture.NpmRoot, BinDirectoryName, PipeHolderMarkerName),
259+
string.Empty);
260+
var options = fixture.Options with
261+
{
262+
InstallTimeout = TimeSpan.FromSeconds(2),
263+
ProcessTerminationTimeout = TimeSpan.FromSeconds(1)
264+
};
265+
await using var updates = CodexCliInstallation.InstallOrUpdateAsync(options,
266+
fixture.LocalApplicationDataRoot, CancellationToken.None).GetAsyncEnumerator();
267+
await Assert.That(await updates.MoveNextAsync()).IsTrue();
268+
using var markerTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(3));
269+
await WaitForFileAsync(rootPidFile, markerTimeout.Token);
270+
await WaitForFileAsync(holderPidFile, markerTimeout.Token);
271+
var rootPid = int.Parse(await File.ReadAllTextAsync(rootPidFile),
272+
System.Globalization.CultureInfo.InvariantCulture);
273+
holderPid = int.Parse(await File.ReadAllTextAsync(holderPidFile),
274+
System.Globalization.CultureInfo.InvariantCulture);
275+
await WaitForProcessExitAsync(rootPid, markerTimeout.Token);
276+
await Assert.That(IsProcessRunning(rootPid)).IsFalse();
277+
await Assert.That(IsProcessRunning(holderPid)).IsTrue();
278+
279+
var exception = await Assert.That(async () =>
280+
{
281+
while (await updates.MoveNextAsync())
282+
{
283+
}
284+
}).ThrowsException();
285+
await Assert.That(exception).IsTypeOf<InvalidOperationException>();
286+
await Assert.That(exception!.Message).IsEqualTo(CleanupMessage);
287+
await Assert.That(IsProcessRunning(rootPid)).IsFalse();
288+
await Assert.That(IsProcessRunning(holderPid)).IsTrue();
289+
}
290+
finally
291+
{
292+
if (holderPid > 0)
293+
{
294+
StopProcess(holderPid);
295+
}
296+
297+
DeleteFixture(fixture.FixtureRoot);
298+
}
299+
}
300+
165301
[Test]
166302
public async Task InstallOrUpdate_RejectsPackageManagerVersionMismatch()
167303
{
@@ -382,6 +518,19 @@ private static async Task WaitForFileAsync(string path, CancellationToken cancel
382518
}
383519
}
384520

521+
private static async Task WaitForProcessExitAsync(int processId, CancellationToken cancellationToken)
522+
{
523+
try
524+
{
525+
using var process = Process.GetProcessById(processId);
526+
await process.WaitForExitAsync(cancellationToken);
527+
}
528+
catch (ArgumentException)
529+
{
530+
return;
531+
}
532+
}
533+
385534
private static void StopProcess(int processId)
386535
{
387536
try

0 commit comments

Comments
 (0)