Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions tests/integrations/socket/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

from sentry_sdk import start_transaction
from sentry_sdk.integrations.socket import SocketIntegration
from tests.conftest import ApproxDict
from tests.conftest import ApproxDict, create_mock_http_server

PORT = create_mock_http_server()
Copy link

Choose a reason for hiding this comment

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

Bug: Global HTTP Server Causes Test Isolation Issues

The mock HTTP server is initialized at the module level (PORT = create_mock_http_server()), creating a global, shared resource. This violates test isolation, leading to resource leaks (due to lack of cleanup), potential port conflicts (especially in parallel test execution), and race conditions. The server should be managed within a test fixture for proper setup, teardown, and test isolation.

Locations (1)

Fix in CursorFix in Web

Copy link
Member Author

@sl0thentr0py sl0thentr0py Jul 25, 2025

Choose a reason for hiding this comment

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

intentional module level



def test_getaddrinfo_trace(sentry_init, capture_events):
sentry_init(integrations=[SocketIntegration()], traces_sample_rate=1.0)
events = capture_events()

with start_transaction():
socket.getaddrinfo("example.com", 443)
socket.getaddrinfo("localhost", PORT)

(event,) = events
(span,) = event["spans"]

assert span["op"] == "socket.dns"
assert span["description"] == "example.com:443"
assert span["description"] == f"localhost:{PORT}" # noqa: E231
assert span["data"] == ApproxDict(
{
"host": "example.com",
"port": 443,
"host": "localhost",
"port": PORT,
}
)

Expand All @@ -32,28 +34,28 @@ def test_create_connection_trace(sentry_init, capture_events):
events = capture_events()

with start_transaction():
socket.create_connection(("example.com", 443), timeout, None)
socket.create_connection(("localhost", PORT), timeout, None)

(event,) = events
(connect_span, dns_span) = event["spans"]
# as getaddrinfo gets called in create_connection it should also contain a dns span

assert connect_span["op"] == "socket.connection"
assert connect_span["description"] == "example.com:443"
assert connect_span["description"] == f"localhost:{PORT}" # noqa: E231
assert connect_span["data"] == ApproxDict(
{
"address": ["example.com", 443],
"address": ["localhost", PORT],
"timeout": timeout,
"source_address": None,
}
)

assert dns_span["op"] == "socket.dns"
assert dns_span["description"] == "example.com:443"
assert dns_span["description"] == f"localhost:{PORT}" # noqa: E231
assert dns_span["data"] == ApproxDict(
{
"host": "example.com",
"port": 443,
"host": "localhost",
"port": PORT,
}
)

Expand All @@ -66,7 +68,7 @@ def test_span_origin(sentry_init, capture_events):
events = capture_events()

with start_transaction(name="foo"):
socket.create_connection(("example.com", 443), 1, None)
socket.create_connection(("localhost", PORT), 1, None)

(event,) = events

Expand Down
Loading