Skip to content

Commit f4649b0

Browse files
committed
Complete HTTP problem responses before rejecting unread bodies
Serialize shared error responses once with the existing JSON options, set their UTF-8 content length, and complete the response before request teardown. Preserve the status, problem contract, cancellation, and request limits. Assert complete framing for real declared-length and chunked oversized requests while retaining healthy same-command-ID controls. Save standard native DCP diagnostics per cluster lane to diagnose resource-creation failures. Verified: full default Debug solution build with -warnaserror; format and format verification; 421 focused functional tests; real body-limit cluster case (old writer red, fixed writer green); seven compiled-inventory checks; repository governance and exact shard partition.
1 parent e6b771e commit f4649b0

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎scripts/Features/TestInfrastructure/ci-test-shards.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ function dotnetArguments(suite, run) {
4747

4848
function startRun(suite, run, environment, children) {
4949
mkdirSync(run.directory, { recursive: true });
50+
let processEnvironment = environment;
51+
if (suite.partition === 'class') {
52+
const diagnosticsDirectory = path.join(run.directory, 'dcp-diagnostics');
53+
mkdirSync(diagnosticsDirectory, { recursive: true });
54+
processEnvironment = { ...environment, DCP_DIAGNOSTICS_LOG_FOLDER: diagnosticsDirectory, DCP_DIAGNOSTICS_LOG_LEVEL: 'info' };
55+
}
5056
const started = Date.now();
5157
const args = dotnetArguments(suite, run);
5258
process.stdout.write(`[${run.label}] dotnet ${args.join(' ')}\n`);
53-
const child = spawn('dotnet', args, { cwd: root, env: environment, stdio: ['ignore', 'pipe', 'pipe'] });
59+
const child = spawn('dotnet', args, { cwd: root, env: processEnvironment, stdio: ['ignore', 'pipe', 'pipe'] });
5460
children.add(child);
5561
for (const [stream, target] of [[child.stdout, process.stdout], [child.stderr, process.stderr]]) {
5662
createInterface({ input: stream }).on('line', line => target.write(`[${run.label}] ${line}\n`));

‎src/KeyLoad/ClientApi/Hosting/ServerErrorMiddleware.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public async Task InvokeAsync(HttpContext context)
2727
var large = error.StatusCode == StatusCodes.Status413PayloadTooLarge;
2828
await WriteAsync(context, large ? ErrorCode.ResourceExhausted : ErrorCode.Validation,
2929
large ? ServerProtocol.BodyExceeded : ServerProtocol.InvalidJson);
30-
await context.Response.CompleteAsync();
3130
}
3231
catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested)
3332
{
@@ -42,9 +41,11 @@ await WriteAsync(context, large ? ErrorCode.ResourceExhausted : ErrorCode.Valida
4241

4342
internal static async Task WriteAsync(HttpContext context, ErrorCode code, string safeDetail)
4443
{
44+
var payload = JsonSerializer.SerializeToUtf8Bytes(Errors.Problem(code, safeDetail), JsonDefaults.Options);
4545
context.Response.StatusCode = Errors.Status(code);
46-
context.Response.ContentLength = null;
47-
await context.Response.WriteAsJsonAsync(Errors.Problem(code, safeDetail), JsonDefaults.Options,
48-
context.RequestAborted);
46+
context.Response.ContentType = ServerProtocol.JsonContentType;
47+
context.Response.ContentLength = payload.Length;
48+
await context.Response.Body.WriteAsync(payload, context.RequestAborted);
49+
await context.Response.CompleteAsync();
4950
}
5051
}

‎tests/KeyLoad.ClusterTests/ResourceExecution/Cases/RequestBodyLimitClusterTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public async Task OversizedDeclaredAndChunkedBodiesAreRejectedBeforeCommandsClai
3838
}
3939
using var rejected = await http.SendAsync(request, timeout.Token);
4040
await Assert.That(rejected.StatusCode).IsEqualTo(System.Net.HttpStatusCode.TooManyRequests);
41+
await Assert.That(rejected.Headers.TransferEncodingChunked is true).IsFalse();
42+
await Assert.That(rejected.Content.Headers.Contains("Content-Length")).IsTrue();
4143
var problem = await rejected.Content.ReadFromJsonAsync<ManagedCode.Communication.Problem>(JsonDefaults.Options, timeout.Token);
4244
await Assert.That(problem!.ErrorCode).IsEqualTo(nameof(ErrorCode.ResourceExhausted));
4345
using var control = new HttpRequestMessage(HttpMethod.Post, "/admin/dispatch?paused=false");

0 commit comments

Comments
 (0)