Skip to content

Cosmos - Change Feed Continuation Token Parsing Bug #43018

@martin-css

Description

@martin-css
  • Package Name: azure-cosmos
  • Package Version: 4.9.0
  • Operating System: Debian
  • Python Version: 3.12.11

Describe the bug

Using the ContainerProxy.query_items_change_feed with the continuation fails with a ValueError when provided a valid continuation token returned by Cosmos DB (NoSQL mode). Example error:

ValueError: year 57679 is out of range

To Reproduce
Steps to reproduce the behavior:

  1. Create a Cosmos NoSQL account, database and container
  2. Using the Python SDK, call query_items_change_feed on the container instance with a start_time parameter supplied.
  3. Capture the continuation token from client_connection.last_response_headers["etag"]
  4. Call query_items_change_feed on the container instance with the continuation parameter set to the value from 3.

Expected behavior
No errors should occur, the change feed query should return expected results, allowing a client to persist the continuation token and pull down further change feed information.

Additional context

This is happening due to bad handling of the continuation token. At azure/cosmos/_change_feed/change_feed_start_from.py:198, the SDK attempts to parse the point in time with the following:

point_in_time = datetime.fromtimestamp(point_in_time_ms).astimezone(timezone.utc)

However, the relevant part of the base64 encoded continuation token has the time present as a Unix timestamp in milliseconds, example extracted from a token:

{"Type": "PointInTime", "PointInTimeMs": 1758011093508}

The from_timestamp method expects a Unix timestamp in seconds. Hence passing the timestamp in milliseconds is causing the ValueError. The variable is called point_in_time_ms which is correct but this is not being used correctly. One possible fix is something like:

point_in_time = datetime.fromtimestamp(point_in_time_ms / 1000).astimezone(timezone.utc) + timedelta(milliseconds=point_in_time_ms % 1000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientThis issue points to a problem in the data-plane of the library.CosmosService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions