Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/api-reference/dotnet/v1/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ is no separate public attached-console launch API. Typed V1 does not accept
[V1.Dev exact-JSON API](https://github.com/microsoft/mxc/blob/main/docs/api-reference/dotnet/v1/dev.md)
accepts caller-authored requests separately.

Captured existing-container execution closes its output readers on timeout
and returns the text collected so far with `TimedOut = true`.

Read the terminal output concurrently rather than only after waiting. On macOS
a workload cannot exit while its terminal output is still unread, so a caller
that waits first without reading can block indefinitely.
Expand Down
25 changes: 25 additions & 0 deletions sdk/dotnet/Microsoft.Mxc.Sdk.Tests/V1/MxcLifecycleE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ public void Exec_ReportsConfiguredTimeout()
}
}

[Fact]
public async Task Exec_CapturedTimeoutClosesDescendantHeldOutput()
{
IsolationSessionHost.Require();

var started = ProvisionAndStart();
using (started.Teardown)
{
var request = new ExecutionRequest(
$"{Cmd} /c echo MXC_TIMEOUT_PARTIAL & start /b {Cmd} /c ping -n 60 127.0.0.1 >nul & ping -n 60 127.0.0.1 >nul")
{
TimeoutMs = 750,
};
var capture = MxcLifecycle.RunInContainerAsync(
started.Id, request, cancellationToken: TestContext.Current.CancellationToken);
var finished = await Task.WhenAny(
capture, Task.Delay(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken));
Assert.Same(capture, finished);

var result = await capture;
Assert.True(result.TimedOut);
Assert.Contains("MXC_TIMEOUT_PARTIAL", result.Stdout);
}
}

/// <summary>
/// Stop and deprovision must be reachable through this binding, and a
/// deprovisioned id must not still be usable.
Expand Down
19 changes: 19 additions & 0 deletions sdk/dotnet/Microsoft.Mxc.Sdk/V1/MxcProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,21 @@ Task<byte[]> ReadAll(Stream? s) =>

var stdoutTask = ReadAll(outStream);
var stderrTask = ReadAll(errStream);
var timeoutCloseFailed = false;
try
{
var result = await WaitAsync(cancellationToken).ConfigureAwait(false);
if (result.TimedOut)
{
timeoutCloseFailed =
!CloseQuietly(outCloser, outStream is not null) |
!CloseQuietly(errCloser, errStream is not null);
if (timeoutCloseFailed)
{
throw new MxcException(ErrorCode.BackendError,
"timed-out capture could not close its output readers");
}
}
var stdout = await stdoutTask.ConfigureAwait(false);
var stderr = await stderrTask.ConfigureAwait(false);
return (result, stdout, stderr);
Expand All @@ -635,6 +647,13 @@ await Task.WhenAll(SwallowAsync(stdoutTask), SwallowAsync(stderrTask))
}
catch
{
if (timeoutCloseFailed)
{
// Without a closer, waiting for a descendant-held pipe can never finish.
KillQuietly();
_ = Task.WhenAll(SwallowAsync(stdoutTask), SwallowAsync(stderrTask));
throw;
}
// On cancellation/failure the reader tasks may be parked in a blocking
// native read that only returns once the child's pipes reach EOF. Kill
// the child first so those write ends close and the reads return;
Expand Down
Loading