diff --git a/docs/03_file_management.md b/docs/03_file_management.md index 0a14d30..20d8096 100644 --- a/docs/03_file_management.md +++ b/docs/03_file_management.md @@ -32,6 +32,7 @@ File management on the Colab VM will be implemented using the Jupyter Contents A - **API**: `GET /api/contents/?content=1` (as seen in HAR). - **Response**: JSON with `content` field. - **Handling**: Decodes content based on `format` (`base64`, `json`, or plain `text`) and saves it locally. +- **Timeouts**: Every Contents API request uses a `(connect, read)` timeout of `(30, 300)` seconds. The read timeout bounds the wait between bytes, not the whole transfer, so large files still finish while a dropped connection fails with an error instead of hanging `ls`, `rm`, `upload`, `download`, or `edit` forever. ### 4. Deleting Files (`colab rm`) - **API**: `DELETE /api/contents/`. diff --git a/src/colab_cli/contents.py b/src/colab_cli/contents.py index d794b5c..79993c0 100644 --- a/src/colab_cli/contents.py +++ b/src/colab_cli/contents.py @@ -21,6 +21,11 @@ from colab_cli.state import SessionState from colab_cli.utils import get_status_code +# (connect, read) seconds. The read timeout bounds the wait between bytes, not +# the whole transfer, so large uploads/downloads still finish; a dropped +# connection fails instead of hanging forever. +REQUEST_TIMEOUT = (30, 300) + class ContentsClient: def __init__(self, session_state: SessionState): @@ -38,7 +43,9 @@ def _request( if params: req_params.update(params) - response = requests.request(method, url, params=req_params, json=json_data) + response = requests.request( + method, url, params=req_params, json=json_data, timeout=REQUEST_TIMEOUT + ) if get_status_code(response) == 404: raise FileNotFoundError(f"File or directory not found: {path}") diff --git a/tests/test_contents.py b/tests/test_contents.py index 26d0f3c..7123b7a 100644 --- a/tests/test_contents.py +++ b/tests/test_contents.py @@ -17,7 +17,7 @@ from unittest.mock import MagicMock, patch import pytest -from colab_cli.contents import ContentsClient +from colab_cli.contents import REQUEST_TIMEOUT, ContentsClient from requests import Response from colab_cli.state import SessionState @@ -59,6 +59,7 @@ def test_list_dir(mock_request, client): "https://fake-endpoint.colab.dev/api/contents/content", params={"authuser": "0", "colab-runtime-proxy-token": "test-token"}, json=None, + timeout=REQUEST_TIMEOUT, ) assert res["type"] == "directory" assert len(res["content"]) == 2 @@ -77,6 +78,7 @@ def test_rm_file(mock_request, client): "https://fake-endpoint.colab.dev/api/contents/content/file.txt", params={"authuser": "0", "colab-runtime-proxy-token": "test-token"}, json=None, + timeout=REQUEST_TIMEOUT, ) @@ -119,6 +121,7 @@ def test_download_file(mock_request, client, tmp_path): "content": "1", }, json=None, + timeout=REQUEST_TIMEOUT, ) assert local_file.read_bytes() == content_bytes @@ -186,4 +189,38 @@ def test_upload_file(mock_request, client, tmp_path): "content": expected_b64, "chunk": 1, }, + timeout=REQUEST_TIMEOUT, ) + + +def test_request_times_out_when_server_stops_responding(client, monkeypatch): + # A server that accepts the connection but never answers (e.g. the network + # dropped mid-transfer) must not make upload/download hang forever. + import socket + import threading + + import requests + + import colab_cli.contents as contents + + server = socket.socket() + server.bind(("127.0.0.1", 0)) + server.listen(1) + client.base_url = f"http://127.0.0.1:{server.getsockname()[1]}" + monkeypatch.setattr(contents, "REQUEST_TIMEOUT", (1, 1)) + + errors = [] + + def call(): + try: + client.list_dir("content") + except Exception as e: + errors.append(e) + + worker = threading.Thread(target=call, daemon=True) + worker.start() + worker.join(10) + server.close() + + assert not worker.is_alive(), "request hung with no timeout" + assert isinstance(errors[0], requests.exceptions.Timeout)