[BUG] Polling timeouts can misfire after wall-clock adjustments

Open Beginner friendly
#203 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start with PollTimer in xai_sdk/poll_timer.py, identified by the reproduction, and inspect how it records the start time and computes elapsed duration. Add a focused regression test covering an independent wall-clock adjustment while monotonic time advances normally. Done means both sync and async polling paths retain the requested timeout behavior without public API changes.

Written by the indexing model from the issue text.

Description

Confirmation of Issue Source
  • I confirm that this issue is with the xAI SDK Python library (e.g., client behavior, SDK methods, or documentation) and not with the xAI API itself (e.g., model output, API errors, or quota issues).
Describe the Bug

PollTimer measures elapsed duration with time.time(), which is a wall clock and can move forward or backward after NTP corrections, manual clock changes, or VM suspend/restore. A forward adjustment can make an otherwise healthy deferred chat, collection indexing, or video-generation operation time out immediately. A backward adjustment can extend polling beyond the caller's requested timeout.

Both sync and async polling paths share this helper, so the behavior affects both clients.

Steps to Reproduce

On current main (4dab6a4), this deterministic reproduction simulates a forward wall-clock adjustment:

import datetime
from unittest.mock import patch

from xai_sdk.poll_timer import PollTimer

with patch("xai_sdk.poll_timer.time.time", side_effect=[100.0, 10_000.0]):
    timer = PollTimer(
        timeout=datetime.timedelta(seconds=10),
        interval=datetime.timedelta(seconds=20),
    )
    print(timer.sleep_interval_or_raise())

Actual result:

TimeoutError: Polling timed out after 9900.0s

Expected result: elapsed timeout accounting should be unaffected by wall-clock changes. Python's time.monotonic() is intended for measuring durations and cannot go backward.

Proposed Fix

Use time.monotonic() for the start timestamp and elapsed-time calculation, with a focused regression test that changes the wall clock independently of monotonic elapsed time. This is an internal implementation change with no public API or normal-case timing change.

Dominant language
Python
Stars
578
Forks
191
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xai-org/xai-sdk-python

All issues in xai-org/xai-sdk-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.