Skip to content

Fix Func<string> response body being invoked twice per request#128

Merged
hibri merged 7 commits intomasterfrom
copilot/fix-request-handler-invocation
Apr 16, 2026
Merged

Fix Func<string> response body being invoked twice per request#128
hibri merged 7 commits intomasterfrom
copilot/fix-request-handler-invocation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

  • Identify root cause: ThreadLocal doesn't work with HttpListener's single-thread async model
  • Fix ResponseBuilder.BuildHeaders(int? contentLength = null) — accept only the pre-computed content length to derive Content-Length via contentLength ?? _contentLength(), removing all shared state
  • Update RequestProcessor.HandleRequest to pass responseBody?.Length to BuildHeaders
  • Update unit test to use the new signature and cleaner imports
  • All 36 unit tests and 70 integration tests pass

Copilot AI linked an issue Apr 16, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix request handler invoking twice issue Fix Func<string> response body being invoked twice per request Apr 16, 2026
Copilot AI requested a review from hibri April 16, 2026 14:20
Comment thread src/HttpMock/ResponseBuilder.cs Outdated
}

public HttpMockResponseHead BuildHeaders() {
public HttpMockResponseHead BuildHeaders(byte[] body = null) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Do not pass the whole body in here as only the content length is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 125e59a. BuildHeaders now accepts int? contentLength = null and the call site passes responseBody?.Length — only the length crosses the boundary, not the body bytes.

Comment thread src/HttpMock/RequestProcessor.cs Outdated

var responseHead = handler.ResponseBuilder.BuildHeaders();
var responseHead = handler.ResponseBuilder.BuildHeaders(responseBody);
var statusCode = responseHead.Status?.Split(' ') is { Length: > 0 } parts ? parts[0] : null;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This line of code is hard to read, keep the code readable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6c9c1ab. The one-liner is now split into two steps:

var statusParts = responseHead.Status?.Split(' ');
var statusCode = statusParts is { Length: > 0 } ? statusParts[0] : null;

@hibri hibri marked this pull request as ready for review April 16, 2026 19:21
@hibri hibri merged commit e33e83a into master Apr 16, 2026
1 check passed
@hibri hibri deleted the copilot/fix-request-handler-invocation branch April 16, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request handler invokes 2 times

2 participants