Skip to content

Commit

Permalink
Always set timeout on requests
Browse files Browse the repository at this point in the history
If you don't set a timeout, your request may hang indefinitely.

The timeout parameter is used for connect and read timeouts.  The whole request
may take longer than the timeout; but if the connection takes longer than the
timeout, or if the connection fails to send more data for an interval longer than
the timeout, it will fail.

I chose 5 seconds for the upload_file, which (at least when run on replicate
infrastructure) is always to an endpoint in the same data center.

I chose 10 seconds for downloading URLFile inputs as they may be making a
request over the public internet.

Co-authored-by: Philip Potter <[email protected]>
  • Loading branch information
tempusfrangit and philandstuff committed Nov 25, 2024
1 parent 3d9c298 commit 85a1624
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/cog/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def upload_file(fh: io.IOBase, output_file_prefix: Optional[str] = None) -> str:
if output_file_prefix is not None:
name = getattr(fh, "name", "output")
url = output_file_prefix + os.path.basename(name)
resp = requests.put(url, files={"file": fh}, timeout=None)
resp = requests.put(url, files={"file": fh}, timeout=5)
resp.raise_for_status()
return url

Expand Down
2 changes: 1 addition & 1 deletion python/cog/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def __wrapped__(self) -> Any:
except AttributeError:
pass
url = object.__getattribute__(self, "__url__")
resp = requests.get(url, stream=True, timeout=None)
resp = requests.get(url, stream=True, timeout=10)
resp.raise_for_status()
resp.raw.decode_content = True
object.__setattr__(self, "__target__", resp.raw)
Expand Down

0 comments on commit 85a1624

Please sign in to comment.