Avoid time domain casting exception during request cancellation (#2645)

Context::Cancel() sets the deadline to DateTime::min(), which throws a range
exception when cast to system_clock::time_point.  Instead, cast
system_clock::now() to DateTime.
This commit is contained in:
John Heffner 2021-07-22 01:10:37 -04:00 committed by GitHub
parent 7da78d4cc9
commit 1839061e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -69,4 +69,18 @@ namespace Azure { namespace Storage { namespace Test {
EXPECT_LE(timeout.Value(), 301);
}
TEST(StoragetimeoutTest, Cancelled)
{
Blobs::BlobClientOptions clientOptions;
auto containerClient = Azure::Storage::Blobs::BlobContainerClient::CreateFromConnectionString(
StandardStorageConnectionString(), LowercaseRandomString(), clientOptions);
Azure::Core::Context context;
context.Cancel();
// Should not throw an error on time point casting.
EXPECT_THROW(
containerClient.DeleteIfExists(Storage::Blobs::DeleteBlobContainerOptions(), context),
Core::OperationCancelledException);
}
}}} // namespace Azure::Storage::Test

View File

@ -37,11 +37,11 @@ namespace Azure { namespace Storage { namespace _internal {
}
else
{
auto currentTimepoint = std::chrono::system_clock::now();
int64_t numSeconds
= std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::time_point(cancelTimepoint) - currentTimepoint)
.count();
auto currentTimepoint = DateTime(std::chrono::system_clock::now());
int64_t numSeconds = (cancelTimepoint > currentTimepoint)
? std::chrono::duration_cast<std::chrono::seconds>(cancelTimepoint - currentTimepoint)
.count()
: -1;
request.GetUrl().AppendQueryParameter(
HttpHeaderTimeout, std::to_string(std::max(numSeconds, int64_t(1))));
}