Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/03_file_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ File management on the Colab VM will be implemented using the Jupyter Contents A
- **API**: `GET /api/contents/<remote_path>?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/<remote_path>`.
Expand Down
9 changes: 8 additions & 1 deletion src/colab_cli/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}")
Expand Down
39 changes: 38 additions & 1 deletion tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
)


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)