question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IfMatchEtag on ReadItemAsync not honoured

See original GitHub issue

Describe the bug IfMatchEtag does not appear to be working as described.

The documentation states:

Most commonly used with the Delete* and Replace* methods of Container such as ReplaceItemAsync<T>(T, String, Nullable<PartitionKey>, ItemRequestOptions, CancellationToken) but can be used with other methods like ReadItemAsync<T>(String, PartitionKey, ItemRequestOptions, CancellationToken) for caching scenarios.

I have witnessed this working previously on an UpsertItem call as well as a Replace call.

However, it does not appear to work on ReadItemAsync. This can be verified relatively simply.

To Reproduce

var etag = "....";
var result = await container.ReadItemAsync<T>(id, new PartitionKey(partitionKey), new ItemRequestOptions {
    IfMatchEtag = etag
});
if (result.ETag != etag) {
    throw new Exception("Cosmos returned document with mismatched ETag");
}

Expected behavior I would expect a CosmosException with either a PreconditionFailed or Conflict (although the latter is not so semantic) status.

Actual behavior The item is returned without issue, with a different ETag than what was presented. Status Code is OK.

Environment summary SDK Version: .NET 3.35.1 OS Version: Mac OSX, also verified on Linux (App Service), and Windows (another dev machine)

Additional context This can be reproduced in the Javascript SDK.

Issue Analytics

  • State:open
  • Created 2 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ealsurcommented, Jul 12, 2023

From your scenario list, scenario 4 is what is broken and what I have been trying to demonstrate. The 412 result is not returned, instead, it reads the document correctly.

Thanks for confirming. I’ll see if I can get confirmation from the service team and if Reads are not expected to work, we’ll update the documentation.

1reaction
rudivcommented, Jul 12, 2023

I have attempted this with a real (previous) ETag from a document and with a fake as per my reproduction (...) and received the same result. The document was read successfully from Cosmos.

From your scenario list, scenario 4 is what is broken and what I have been trying to demonstrate. The 412 result is not returned, instead, it reads the document correctly.

Again this can be verified very simply using the reproduction code. The key bit is here, which throws:

if (result.ETag != etag) {
    throw new Exception("Cosmos returned document with mismatched ETag");
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to obtain ETag for individual document when ...
In this sample we get FeedResponse<SalesOrder> which has ETag, but this is the tag associated with last transaction, not document.
Read more >
Optimistic Concurrency Control
In this exercise, we will view the ETag property of a resource that is requested using the SDK. If this is your first...
Read more >
RequestOptions.IfMatchEtag Property - Cosmos DB
Gets or sets the If-Match (ETag) associated with the request in the Azure Cosmos ... methods like ReadItemAsync<T>(String, PartitionKey, ItemRequestOptions, ...
Read more >
Peter Gerritsen's blog
For a project I'm working on I needed to specify a outgoing proxy for accessing Azure Table Storage in a .NET console application....
Read more >
ETags in ASP.NET Core - Peter Ritchie's Blog
Optimistic Concurrency Control is a means to avoid distributed locking in situations when two or more potentially concurrent operations rarely ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found