File tree Expand file tree Collapse file tree 3 files changed +9
-7
lines changed Expand file tree Collapse file tree 3 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -127,16 +127,17 @@ def timeout(seconds: Optional[float]) -> ContextManager:
127
127
NetworkTimeout) as exc:
128
128
print(f"block timed out: {exc!r}")
129
129
130
- When nesting :func:`~pymongo.timeout`, the nested block overrides the
131
- timeout. When exiting the block, the previous deadline is restored::
130
+ When nesting :func:`~pymongo.timeout`, the newly computed deadline is capped to at most
131
+ the existing deadline. The deadline can only be shortened, not extended.
132
+ When exiting the block, the previous deadline is restored::
132
133
133
134
with pymongo.timeout(5):
134
135
coll.find_one() # Uses the 5 second deadline.
135
136
with pymongo.timeout(3):
136
137
coll.find_one() # Uses the 3 second deadline.
137
138
coll.find_one() # Uses the original 5 second deadline.
138
139
with pymongo.timeout(10):
139
- coll.find_one() # Uses the 10 second deadline.
140
+ coll.find_one() # Still uses the original 5 second deadline.
140
141
coll.find_one() # Uses the original 5 second deadline.
141
142
142
143
:Parameters:
Original file line number Diff line number Diff line change @@ -70,9 +70,9 @@ def __init__(self, timeout: Optional[float]):
70
70
71
71
def __enter__ (self ):
72
72
timeout_token = TIMEOUT .set (self ._timeout )
73
- deadline_token = DEADLINE .set (
74
- time .monotonic () + self ._timeout if self ._timeout else float ("inf" )
75
- )
73
+ prev_deadline = DEADLINE .get ()
74
+ next_deadline = time .monotonic () + self ._timeout if self ._timeout else float ("inf" )
75
+ deadline_token = DEADLINE . set ( min ( prev_deadline , next_deadline ) )
76
76
rtt_token = RTT .set (0.0 )
77
77
self ._tokens = (timeout_token , deadline_token , rtt_token )
78
78
return self
Original file line number Diff line number Diff line change @@ -43,10 +43,11 @@ def test_timeout_nested(self):
43
43
self .assertEqual (_csot .get_timeout (), 10 )
44
44
deadline_10 = _csot .get_deadline ()
45
45
46
+ # Capped at the original 10 deadline.
46
47
with pymongo .timeout (15 ):
47
48
coll .find_one ()
48
49
self .assertEqual (_csot .get_timeout (), 15 )
49
- self .assertGreater (_csot .get_deadline (), deadline_10 )
50
+ self .assertEqual (_csot .get_deadline (), deadline_10 )
50
51
51
52
# Should be reset to previous values
52
53
self .assertEqual (_csot .get_timeout (), 10 )
You can’t perform that action at this time.
0 commit comments