Skip to content

Commit b3a4bda

Browse files
ref: fix types for sentry.net.socket (#93477)
I suspect our socket default code wasn't even working because it did not match urllib3's <!-- Describe your PR here. -->
1 parent f23f06a commit b3a4bda

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ module = [
136136
"sentry.middleware.auth",
137137
"sentry.middleware.ratelimit",
138138
"sentry.net.http",
139-
"sentry.net.socket",
140139
"sentry.notifications.notifications.activity.base",
141140
"sentry.plugins.config",
142141
"sentry.release_health.metrics_sessions_v2",
@@ -328,6 +327,7 @@ module = [
328327
"sentry.models.options.*",
329328
"sentry.monitors.consumers.monitor_consumer",
330329
"sentry.monkey.*",
330+
"sentry.net.socket",
331331
"sentry.nodestore.*",
332332
"sentry.nodestore.base",
333333
"sentry.nodestore.bigtable.backend",

src/sentry/net/http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from urllib3.exceptions import ConnectTimeoutError, NewConnectionError
1616
from urllib3.poolmanager import PoolManager
1717
from urllib3.util.connection import _set_socket_options
18+
from urllib3.util.timeout import _DEFAULT_TIMEOUT
1819

1920
from sentry import VERSION as SENTRY_VERSION
2021
from sentry.net.socket import safe_create_connection
@@ -235,7 +236,7 @@ def _new_conn(self):
235236
# If provided, set socket level options before connecting.
236237
_set_socket_options(sock, self.socket_options)
237238

238-
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT: # type: ignore[attr-defined]
239+
if self.timeout is not _DEFAULT_TIMEOUT:
239240
sock.settimeout(self.timeout)
240241
sock.connect(self.socket_path)
241242
return sock

src/sentry/net/socket.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import functools
44
import ipaddress
55
import socket
6+
from collections.abc import Sequence
67
from typing import TYPE_CHECKING
78
from urllib.parse import urlparse
89

910
from django.conf import settings
1011
from django.utils.encoding import force_str
1112
from urllib3.exceptions import LocationParseError
1213
from urllib3.util.connection import _set_socket_options, allowed_gai_family
14+
from urllib3.util.timeout import _DEFAULT_TIMEOUT, _TYPE_DEFAULT
1315

1416
from sentry.exceptions import RestrictedIPAddress
1517

@@ -104,12 +106,12 @@ def is_safe_hostname(hostname: str | None) -> bool:
104106

105107
# Modifed version of urllib3.util.connection.create_connection.
106108
def safe_create_connection(
107-
address,
108-
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
109-
source_address=None,
110-
socket_options=None,
109+
address: tuple[str, int],
110+
timeout: _TYPE_DEFAULT | float | None = _DEFAULT_TIMEOUT,
111+
source_address: str | None = None,
112+
socket_options: Sequence[tuple[int, int, int | bytes]] | None = None,
111113
is_ipaddress_permitted: IsIpAddressPermitted = None,
112-
):
114+
) -> socket.socket:
113115
if is_ipaddress_permitted is None:
114116
is_ipaddress_permitted = is_ipaddress_allowed
115117

@@ -137,6 +139,7 @@ def safe_create_connection(
137139

138140
# Begin custom code.
139141
ip = sa[0]
142+
assert isinstance(ip, str), ip # we aren't running ipv6-disabled python
140143
if not is_ipaddress_permitted(ip):
141144
# I am explicitly choosing to be overly aggressive here. This means
142145
# the first IP that matches that hits our restricted set of IP networks,
@@ -155,7 +158,7 @@ def safe_create_connection(
155158
# If provided, set socket level options before connecting.
156159
_set_socket_options(sock, socket_options)
157160

158-
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
161+
if timeout is not _DEFAULT_TIMEOUT:
159162
sock.settimeout(timeout)
160163
if source_address:
161164
sock.bind(source_address)

0 commit comments

Comments
 (0)