Skip to content

[Bug]: httpx handle_request_with_instana returns None instead of re-raising exception #866

@Rafal-Chrzanowski-IBM

Description

@Rafal-Chrzanowski-IBM

Problem Description

Greetings,

I've encountered an issue regarding handling httpx requests with Instana. In this file, the httpx.HTTPTransport.handle_request and httpx.AsyncHTTPTransport.handle_async_request methods are overwritten. When tracking is enabled and an exception (for example self-signed certificate exception when verify=True is set) appears during the original handle_request method execution, the execution jumps to the except block:

try:
    request = args[0]
    _set_request_span_attributes(span, request)
    tracer.inject(span.context, Format.HTTP_HEADERS, request.headers)

    response = wrapped(*args, **kwargs)
    _set_response_span_attributes(span, response)
except Exception as e:
    span.record_exception(e)
else:
    return response

As you can observe, the exception is not re-raised neither inside nor after span.record_exception(e) is executed. This causes the transport to return None, which is outside of that method's expected return values. This then causes a 'NoneType' object has no attribute 'stream' exception later in the request process.

Would it be possible to update the code so it re-raises the exception if it happens?

Minimal, Complete, Verifiable, Example

You can use a linter like Pylance to detect that these functions can return None, even though it's not expected by the type hints.

Without instana imported, the following snippet raises an exception, which means it is working as expected:

>>> import httpx
>>> request = httpx.Request("GET", "http://my-website.local")
>>> transport = httpx.HTTPTransport()
>>> print(transport.handle_request(request))
ConnectError: [Errno 8] nodename nor servname provided, or not known

When instana is imported, the httpx call returns None instead of raising an exception:

>>> import instana
>>> import httpx
>>> request = httpx.Request("GET", "http://my-website.local")
>>> transport = httpx.HTTPTransport()
>>> print(transport.handle_request(request))
None

Python Version

Python 3.12

Python Modules

Not applicable - this is related only to the implementation

Python Environment

Not applicable - this is related only to the implementation

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions