Skip to content

Commit

Permalink
Fix unintended "wait forever" behavior with zero timeouts [#976].
Browse files Browse the repository at this point in the history
In a few places we did "if timeout:" or "if expiration:" when we
really meant "if timeout is not None:".  This meant that in the zero
timeout case we fell into the "wait forever" path instead of
immediately timing out.  In the case of UDP queries, we'd be waiting
on recvfrom() and if a packet was lost, then the code would never wake
up.

(cherry picked from commit 0c183f1)
  • Loading branch information
rthalley committed Aug 5, 2023
1 parent 1d9651c commit 52c2dc1
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dns/_asyncio_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def close(self):


async def _maybe_wait_for(awaitable, timeout):
if timeout:
if timeout is not None:
try:
return await asyncio.wait_for(awaitable, timeout)
except asyncio.TimeoutError:
Expand Down
2 changes: 1 addition & 1 deletion dns/_trio_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def _maybe_timeout(timeout):
if timeout:
if timeout is not None:
return trio.move_on_after(timeout)
else:
return dns._asyncbackend.NullContext()
Expand Down
2 changes: 1 addition & 1 deletion dns/asyncquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _source_tuple(af, address, port):


def _timeout(expiration, now=None):
if expiration:
if expiration is not None:
if not now:
now = time.time()
return max(expiration - now, 0)
Expand Down
2 changes: 1 addition & 1 deletion dns/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ def zone_for_name(
while 1:
try:
rlifetime: Optional[float]
if expiration:
if expiration is not None:
rlifetime = expiration - time.time()
if rlifetime <= 0:
rlifetime = 0
Expand Down

0 comments on commit 52c2dc1

Please sign in to comment.