Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instrumentation/urllib3: fix urllib3 2.0.1+ crash with many args #2002

Merged
merged 1 commit into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions elasticapm/instrumentation/packages/urllib3.py
Expand Up @@ -61,12 +61,7 @@ def update_headers(args, kwargs, instance, transaction, trace_parent):
:param trace_parent: the TraceParent object
:return: an (args, kwargs) tuple
"""
from urllib3._version import __version__ as urllib3_version

if urllib3_version.startswith("2") and len(args) >= 5 and args[4]:
headers = args[4].copy()
args = tuple(itertools.chain((args[:4]), (headers,), args[5:]))
elif len(args) >= 4 and args[3]:
if len(args) >= 4 and args[3]:
headers = args[3].copy()
args = tuple(itertools.chain((args[:3]), (headers,), args[4:]))
elif "headers" in kwargs and kwargs["headers"]:
Expand Down
17 changes: 17 additions & 0 deletions tests/instrumentation/urllib3_tests.py
Expand Up @@ -294,3 +294,20 @@ def test_instance_headers_are_respected(
assert "kwargs" in request_headers
if instance_headers and not (header_arg or header_kwarg):
assert "instance" in request_headers


def test_connection_pool_urlopen_does_not_crash_with_many_args(instrument, elasticapm_client, waiting_httpserver):
"""Mimics ConnectionPool.urlopen error path with broken connection, see #1928"""
waiting_httpserver.serve_content("")
url = waiting_httpserver.url + "/hello_world"
parsed_url = urllib.parse.urlparse(url)
pool = urllib3.HTTPConnectionPool(
parsed_url.hostname,
parsed_url.port,
maxsize=1,
block=True,
)
retry = urllib3.util.Retry(10)
elasticapm_client.begin_transaction("transaction")
r = pool.urlopen("GET", url, None, {"args": "true"}, retry, False, False)
assert r.status == 200