Commit 58aca1d
fix: use monotonic clock for file processing timeout (openai#3748)
## Summary
- Use `time.monotonic()` for elapsed-time deadlines in both synchronous
and asynchronous file-processing polling.
- Add regression coverage for a wall-clock rollback in both resource
variants.
## Bug
`Files.wait_for_processing()` and its asynchronous counterpart used
`time.time()` to measure elapsed time. If the system wall clock moves
backwards while a file is still processing, the calculated elapsed time
also moves backwards and the timeout can be postponed indefinitely.
### Reproduction
With `max_wait_seconds=1`, `poll_interval=5`, and a wall clock that
changes from `100` to `90` after the first poll:
- Expected: raise the timeout error once the monotonic deadline has
elapsed.
- Actual before this change: continue polling and enter another
five-second sleep.
## Root cause
Wall-clock time is adjustable and is not suitable for measuring elapsed
durations.
## Fix
Use Python's monotonic clock for both the initial timestamp and every
deadline check. This does not change the public API, poll interval,
terminal states, or error message.
## Validation
- `tests/lib/test_file_processing.py`: 15 passed
- Ruff check: passed
- Ruff format check: passed
- Isolated synchronous and asynchronous rollback reproducer: passed
Commands used:
```text
PYTHONPATH=/tmp/openai-python-file-timeout-deps-20260828:src python -m pytest -p no:cacheprovider -o addopts= --confcutdir=tests/lib --asyncio-mode=auto -q tests/lib/test_file_processing.py
python -m ruff check --no-cache src/openai/lib/_files.py tests/lib/test_file_processing.py
python -m ruff format --no-cache --check src/openai/lib/_files.py tests/lib/test_file_processing.py
```
---------
Co-authored-by: Marcus Wood <marcuswood@openai.com>1 parent 841d929 commit 58aca1d
2 files changed
Lines changed: 16 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
64 | 68 | | |
65 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
66 | 72 | | |
67 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
68 | 76 | | |
69 | 77 | | |
70 | 78 | | |
| |||
0 commit comments