Skip to content

fix: deduplicate urllib3 spans when PoolManager delegates to HTTPConnectionPool#82

Merged
sohankshirsagar merged 2 commits intomainfrom
sohan/deduplicate-urllib3
Mar 20, 2026
Merged

fix: deduplicate urllib3 spans when PoolManager delegates to HTTPConnectionPool#82
sohankshirsagar merged 2 commits intomainfrom
sohan/deduplicate-urllib3

Conversation

@sohankshirsagar
Copy link
Contributor

@sohankshirsagar sohankshirsagar commented Mar 20, 2026

Summary

Urllib3Instrumentation patches both PoolManager.urlopen() and HTTPConnectionPool.urlopen() to cover two distinct user-facing entry paths. However, when a request goes through PoolManager, it internally delegates to HTTPConnectionPool.urlopen(), causing both patches to fire and producing two identical spans for a single HTTP request (a parent PoolManager span and a redundant child ConnectionPool span).

This fix introduces a ContextVar (_inside_poolmanager) that the PoolManager patch sets to True for the duration of its call. The ConnectionPool patch checks this flag and passes through without creating a span when it detects the request already originated from PoolManager. Direct HTTPConnectionPool usage (bypassing PoolManager) is unaffected since the flag remains False.

Changes

  • Added _inside_poolmanager ContextVar to signal when a request is already being handled by the PoolManager patch
  • Set the flag in _patch_pool_manager and reset it in the finally block (thread-safe and async-safe)
  • Added early-exit check in _patch_connection_pool to skip span creation when inside a PoolManager call
  • Extracted repeated passthrough argument lists in the ConnectionPool patch into a functools.partial (_passthrough) to reduce duplication across the three early-return paths (disabled, inside-poolmanager, higher-level instrumentation)

@tusk-dev
Copy link

tusk-dev bot commented Mar 20, 2026

Generated 11 tests - 11 passed

Commit tests View tests

Tip

New to Tusk Unit Tests? Learn more here.

Test Summary

  • Urllib3Instrumentation._patch_connection_pool - 4 ✓
  • Urllib3Instrumentation._patch_pool_manager - 3 ✓
  • Urllib3Instrumentation._patch_pool_manager, Urllib3Instrumentation._patch_connection_pool - 1 ✓
  • _inside_poolmanager - 3 ✓

Results

Tusk's tests all pass and validate the core deduplication fix. The ContextVar (_inside_poolmanager) is properly initialized, set/reset during PoolManager execution, and isolated across contexts—critical for thread-safety. The PoolManager patch correctly sets the flag and cleans up even on exceptions, while the ConnectionPool patch skips span creation when the flag is set but still instruments direct ConnectionPool calls. The integration test confirms the fix works end-to-end: a single request through PoolManager now produces one span instead of two. This directly addresses the duplicate span problem that was polluting traces and making debugging harder.

Key Points:

  • _inside_poolmanager context isolation prevents state leaks across concurrent requests
  • PoolManager patch reliably sets/resets the flag, including exception paths
  • ConnectionPool patch correctly gates span creation based on the flag while preserving direct usage instrumentation
  • Integration test validates the deduplication works in practice—no more redundant child spans
View check history

Commit Status Output Created (UTC)
b1c3f69 Generated 11 tests - 11 passed Tests Mar 20, 2026 3:12AM
8d85236 Generated 11 tests - 11 passed Tests Mar 20, 2026 5:47PM

Was Tusk helpful? Give feedback by reacting with 👍 or 👎

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

@sohankshirsagar sohankshirsagar merged commit b19d9e6 into main Mar 20, 2026
27 checks passed
@sohankshirsagar sohankshirsagar deleted the sohan/deduplicate-urllib3 branch March 20, 2026 18:03
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.

2 participants