diff --git a/dotnet/Codexcw/AccountUsage.cs b/dotnet/Codexcw/AccountUsage.cs index 9768a19..4fe5112 100644 --- a/dotnet/Codexcw/AccountUsage.cs +++ b/dotnet/Codexcw/AccountUsage.cs @@ -215,6 +215,7 @@ public static async Task GetAccountUsageAsync( } var stderr = new TailBuffer(Runner.DefaultStderrLimit); + ProcessException failure; var stderrPump = stderr.PumpAsync(process.StandardError.BaseStream, CancellationToken.None); try { @@ -231,11 +232,11 @@ public static async Task GetAccountUsageAsync( }, }; await RequiredRequest(client, "initialize", initParams, InitTimeout, - "initialize codex app-server", stderr, cancellationToken).ConfigureAwait(false); + "initialize codex app-server", cancellationToken).ConfigureAwait(false); await client.NotifyAsync("initialized", new JsonObject(), cancellationToken).ConfigureAwait(false); var rawRateLimits = await RequiredRequest(client, "account/rateLimits/read", null, requestTimeout, - "read codex account rate limits", stderr, cancellationToken).ConfigureAwait(false); + "read codex account rate limits", cancellationToken).ConfigureAwait(false); AccountRateLimits rateLimits; IReadOnlyDictionary byLimitId; try @@ -260,7 +261,7 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout, try { var rawTokenUsage = await Request(client, "account/usage/read", null, requestTimeout, - "read codex account token usage", stderr, cancellationToken).ConfigureAwait(false); + "read codex account token usage", cancellationToken).ConfigureAwait(false); try { usage = usage with @@ -281,7 +282,7 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout, try { var rawAccount = await Request(client, "account/read", new JsonObject(), requestTimeout, - "read codex account", stderr, cancellationToken).ConfigureAwait(false); + "read codex account", cancellationToken).ConfigureAwait(false); try { usage = usage with @@ -301,6 +302,10 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout, return usage; } + catch (ProcessException ex) + { + failure = ex; + } finally { Session.KillProcessTree(process); @@ -314,13 +319,16 @@ await process.WaitForExitAsync(CancellationToken.None) } try { - process.StandardError.BaseStream.Dispose(); + await stderrPump.WaitAsync(TimeSpan.FromSeconds(1), CancellationToken.None).ConfigureAwait(false); } - catch (IOException) + catch (TimeoutException) { + process.StandardError.BaseStream.Dispose(); } await stderrPump.ConfigureAwait(false); } + // Format the failure after teardown so the stderr reader has consumed the final bytes. + throw new ProcessException(failure.Message + StderrSuffix(stderr), failure.InnerException ?? failure); } private static async Task RequiredRequest( @@ -329,17 +337,16 @@ private static async Task RequiredRequest( JsonObject? parameters, TimeSpan timeout, string context, - TailBuffer stderr, CancellationToken cancellationToken) { try { - return await Request(client, method, parameters, timeout, context, stderr, cancellationToken) + return await Request(client, method, parameters, timeout, context, cancellationToken) .ConfigureAwait(false); } catch (JsonRpcErrorException ex) { - throw new ProcessException($"{context}: {ex.Message}{StderrSuffix(stderr)}", ex); + throw new ProcessException($"{context}: {ex.Message}", ex); } } @@ -349,7 +356,6 @@ private static async Task Request( JsonObject? parameters, TimeSpan timeout, string context, - TailBuffer stderr, CancellationToken cancellationToken) { try @@ -366,7 +372,7 @@ private static async Task Request( } catch (Exception ex) { - throw new ProcessException($"{context}: {ex.Message}{StderrSuffix(stderr)}", ex); + throw new ProcessException($"{context}: {ex.Message}", ex); } }