Skip to content

fix(http): handle null request, keep span open over call, and isolate options in HeadersInspectionHandler - #821

Merged
Vincent Biret (baywet) merged 3 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-handler-lifecycle-options
Oct 21, 2026
Merged

Vincent Biret (baywet) merged 3 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-handler-lifecycle-options

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This PR brings HeadersInspectionHandler into full alignment with the middleware patterns and error handling across Kiota HTTP:

  1. Null Request Check:
    Validates if request is None: raise TypeError("request cannot be null") matching BodyInspectionHandler, preventing an unexpected AttributeError on invalid requests.
  2. Span Lifecycle & Timing:
    Keeps the observability span open over the downstream super().send(request, transport) call and ensures span.end() is invoked within a try ... finally block (mirroring the fix in UrlReplaceHandler and BodyInspectionHandler), ensuring correct timing and preventing dropped spans on transport failures.
  3. Per-Request Options Clearing:
    Clears captured request and response headers on each call on whichever option object is active (current_options), preventing headers from previous requests or retry attempts from accumulating when passing custom HeadersInspectionHandlerOption per request.
  4. Preserve Multi-Valued Headers:
    Uses request.headers.get_list(header) and response.headers.get_list(header) when feeding into HeadersCollection.add, accurately preserving discrete multi-valued headers (e.g. multiple cookies or accepts) without comma concatenation.

Testing

Added comprehensive unit tests in packages/http/httpx/tests/middleware_tests/test_headers_inspection_handler.py:

  • test_send_request_none_raises_type_error
  • test_headers_inspection_handler_request_options_isolation_and_clearing
  • test_headers_inspection_handler_multi_valued_headers
  • test_span_ends_on_transport_failure

cc Vincent Biret (@baywet)

@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 21, 2026 05:21
Copilot AI balanced review requested due to automatic review settings October 21, 2026 05:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet), I've created this PR to resolve the span lifecycle, per-request option isolation, multi-valued header preservation, and null checks for HeadersInspectionHandler, complete with comprehensive regression unit tests. Ready for your review whenever convenient!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Request and response capture discard repeated Content-Encoding values after the first.

2 open issues

Review effort: Balanced

Comment thread packages/http/httpx/kiota_http/middleware/headers_inspection_handler.py Outdated
Copilot AI balanced review requested due to automatic review settings October 21, 2026 14:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

The review comments have been addressed in the latest commit:

  1. Preserve Repeated Single-Value Headers (\Content-Encoding):

    • In \HeadersInspectionHandler._capture_headers(), headers in \HeadersCollection.SINGLE_VALUE_HEADERS\ (such as \Content-Encoding, \Content-Type, \Content-Length) now pass the combined comma-separated string \source_headers[header]\ into \HeadersCollection.add(), preventing truncation of repeated values like \gzip, br.
    • Other multi-valued headers continue using \get_list()\ to preserve discrete header values.
    • Added unit tests covering multiple \Content-Encoding\ values on both requests and responses.
  2. Assert Span Remains Open During Transport Execution:

    • Updated \ est_span_ends_on_transport_failure\ to assert \len(span_exporter.get_finished_spans()) == 0\ inside the transport callback prior to raising the error, detecting premature span termination.
    • Added \ est_span_remains_open_during_successful_transport\ to verify span lifecycle during normal transport execution.

All linters and test suites pass cleanly. Ready for review!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

Static review found no unresolved issues, and the regression tests address prior feedback; tests were not executed.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution!

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Oct 21, 2026
Copilot AI balanced review requested due to automatic review settings October 21, 2026 16:15
@baywet
Vincent Biret (baywet) force-pushed the fix/headers-inspection-handler-lifecycle-options branch from 095d388 to 7f2763c Compare October 21, 2026 16:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused changes address prior feedback and include regression coverage, with no unresolved blocking findings.

No open issues 🎉

Review effort: Balanced

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks for the review Vincent Biret (@baywet)

@baywet
Vincent Biret (baywet) merged commit 5dc3d01 into microsoft:main Oct 21, 2026
67 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Oct 21, 2026
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

3 participants