Skip to content

Commit 58aaede

Browse files
committed
PYTHON-2281 Properly reduce keep alive time on Windows
1 parent 815c924 commit 58aaede

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

pymongo/pool.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
IPADDR_SAFE as _IPADDR_SAFE)
2929

3030
from bson import DEFAULT_CODEC_OPTIONS
31-
from bson.py3compat import imap, itervalues, _unicode, integer_types
31+
from bson.py3compat import imap, itervalues, _unicode
3232
from bson.son import SON
3333
from pymongo import auth, helpers, thread_util, __version__
3434
from pymongo.client_session import _validate_session_write_concern
@@ -132,31 +132,36 @@ def _set_non_inheritable_non_atomic(dummy):
132132
except ImportError:
133133
import winreg
134134

135+
def _query(key, name, default):
136+
try:
137+
value, _ = winreg.QueryValueEx(key, name)
138+
# Ensure the value is a number or raise ValueError.
139+
return int(value)
140+
except (OSError, ValueError):
141+
# QueryValueEx raises OSError when the key does not exist (i.e.
142+
# the system is using the Windows default value).
143+
return default
144+
135145
try:
136146
with winreg.OpenKey(
137147
winreg.HKEY_LOCAL_MACHINE,
138148
r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters") as key:
139-
_DEFAULT_TCP_IDLE_MS, _ = winreg.QueryValueEx(key, "KeepAliveTime")
140-
_DEFAULT_TCP_INTERVAL_MS, _ = winreg.QueryValueEx(
141-
key, "KeepAliveInterval")
142-
# Make sure these are integers.
143-
if not isinstance(_DEFAULT_TCP_IDLE_MS, integer_types):
144-
raise ValueError
145-
if not isinstance(_DEFAULT_TCP_INTERVAL_MS, integer_types):
146-
raise ValueError
147-
except (OSError, ValueError):
148-
# We could not check the default values so do not attempt to override.
149-
def _set_keepalive_times(dummy):
150-
pass
151-
else:
152-
def _set_keepalive_times(sock):
153-
idle_ms = min(_DEFAULT_TCP_IDLE_MS, _MAX_TCP_KEEPIDLE * 1000)
154-
interval_ms = min(_DEFAULT_TCP_INTERVAL_MS,
155-
_MAX_TCP_KEEPINTVL * 1000)
156-
if (idle_ms < _DEFAULT_TCP_IDLE_MS or
157-
interval_ms < _DEFAULT_TCP_INTERVAL_MS):
158-
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
159-
(1, idle_ms, interval_ms))
149+
_WINDOWS_TCP_IDLE_MS = _query(key, "KeepAliveTime", 7200000)
150+
_WINDOWS_TCP_INTERVAL_MS = _query(key, "KeepAliveInterval", 1000)
151+
except OSError:
152+
# We could not check the default values because winreg.OpenKey failed.
153+
# Assume the system is using the default values.
154+
_WINDOWS_TCP_IDLE_MS = 7200000
155+
_WINDOWS_TCP_INTERVAL_MS = 1000
156+
157+
def _set_keepalive_times(sock):
158+
idle_ms = min(_WINDOWS_TCP_IDLE_MS, _MAX_TCP_KEEPIDLE * 1000)
159+
interval_ms = min(_WINDOWS_TCP_INTERVAL_MS,
160+
_MAX_TCP_KEEPINTVL * 1000)
161+
if (idle_ms < _WINDOWS_TCP_IDLE_MS or
162+
interval_ms < _WINDOWS_TCP_INTERVAL_MS):
163+
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
164+
(1, idle_ms, interval_ms))
160165
else:
161166
def _set_tcp_option(sock, tcp_option, max_value):
162167
if hasattr(socket, tcp_option):

0 commit comments

Comments
 (0)