Fix UnboundLocalError in asgi handler #1564
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Currently the duration calculation in the ASGI instrumentation fails if anything outside the
self.appcall raises an error because start will not have been instantiated.This hides any instansation errors are actually occurring since they will be replaced by the
UnboundLocalErrorThis PR moves the duration tracking into its own try/finally block so it will not be ran unless self.app was called.
It would have also been possible to set
startto None at the top of the function and then have an if statement inside the finally block, however I decided that was too close to overloading the concept of thestartvariable as ashould record durationcontrol flow flag.The actual issue is a unicode decoding error in the handling of headers, however since #1492 potentially fixes this issue too I have restricted the scope of this PR to be focused on error propagation.
Fixes (#1478)
Type of change
How Has This Been Tested?
Added new unit test (
test_failed_instrumentation_errors_propagate) it currently uses theserver_request_hookfunctionality to force an instrumentation error without relying on any specific bugs.The test does also rely on setting self.communicator to None in order to prevent the error being re-raised in teardown, relevant code links:
https://github.com/open-telemetry/opentelemetry-python/blob/b184dc95844561f0dce3f5c0a496bc89d0fb9a6d/tests/opentelemetry-test-utils/src/opentelemetry/test/asgitestutil.py#L38
https://github.com/django/asgiref/blob/36022636d3390c1c1cbb952336a34d652a6bfb26/asgiref/testing.py#L8
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.