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
8 changes: 4 additions & 4 deletions src/openai/lib/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def wait_for_file_processing(
"""Poll a file using the caller's resource and sleep hooks."""
TERMINAL_STATES = {"processed", "error", "deleted"}

start = time.time()
start = time.monotonic()
file = files.retrieve(id)
while file.status not in TERMINAL_STATES:
files._sleep(poll_interval)

file = files.retrieve(id)
if time.time() - start > max_wait_seconds:
if time.monotonic() - start > max_wait_seconds:
raise RuntimeError(
f"Giving up on waiting for file {id} to finish processing after {max_wait_seconds} seconds."
)
Expand All @@ -43,13 +43,13 @@ async def async_wait_for_file_processing(
"""Poll a file using the caller's async resource and sleep hooks."""
TERMINAL_STATES = {"processed", "error", "deleted"}

start = time.time()
start = time.monotonic()
file = await files.retrieve(id)
while file.status not in TERMINAL_STATES:
await files._sleep(poll_interval)

file = await files.retrieve(id)
if time.time() - start > max_wait_seconds:
if time.monotonic() - start > max_wait_seconds:
raise RuntimeError(
f"Giving up on waiting for file {id} to finish processing after {max_wait_seconds} seconds."
)
Expand Down
26 changes: 25 additions & 1 deletion tests/lib/test_file_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,35 @@ async def test_timeout(files_resource: Files | AsyncFiles) -> None:
mock.patch.object(files_resource, "retrieve", return_value=make_file("uploaded")),
mock.patch.object(files_resource, "_sleep"),
):
clock.time.side_effect = [0.0, 11.0]
clock.monotonic.side_effect = [0.0, 11.0]
with pytest.raises(RuntimeError, match=f"Giving up on waiting for file {FILE_ID}"):
await wait(files_resource, max_wait_seconds=10)


async def test_timeout_uses_monotonic_clock_after_wall_clock_rollback(
files_resource: Files | AsyncFiles,
) -> None:
class PollContinued(RuntimeError):
pass

with (
mock.patch.object(file_helpers, "time") as clock,
mock.patch.object(files_resource, "retrieve", return_value=make_file("uploaded")),
mock.patch.object(
files_resource,
"_sleep",
side_effect=[None, PollContinued("poll continued after the deadline")],
) as sleep,
):
clock.time.side_effect = [100.0, 90.0]
clock.monotonic.side_effect = [100.0, 102.0]

with pytest.raises(RuntimeError, match=f"Giving up on waiting for file {FILE_ID}"):
await wait(files_resource, poll_interval=5.0, max_wait_seconds=1.0)

sleep.assert_called_once_with(5.0)


async def test_retrieve_error_propagates(files_resource: Files | AsyncFiles) -> None:
error = RuntimeError("synthetic retrieval failure")
with mock.patch.object(files_resource, "retrieve", side_effect=error):
Expand Down