Skip to content

Commit 456d523

Browse files
committed
reset parser in set_retry
1 parent d2951a4 commit 456d523

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/mongocrypt-kms-ctx.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ static void set_retry(mongocrypt_kms_ctx_t *kms) {
518518
kms->should_retry = true;
519519
kms->attempts++;
520520
kms->sleep_usec = backoff_time_usec(kms->attempts);
521+
// Reset intermediate state of parser.
522+
if (kms->parser) {
523+
kms_response_parser_reset(kms->parser);
524+
}
521525
}
522526

523527
/* An AWS KMS context has received full response. Parse out the result or error.
@@ -1167,11 +1171,6 @@ bool mongocrypt_kms_ctx_fail(mongocrypt_kms_ctx_t *kms) {
11671171

11681172
// Mark KMS context as retryable. Return again in `mongocrypt_ctx_next_kms_ctx`.
11691173
set_retry(kms);
1170-
1171-
// Reset intermediate state of parser.
1172-
if (kms->parser) {
1173-
kms_response_parser_reset(kms->parser);
1174-
}
11751174
return true;
11761175
}
11771176

test/test-mongocrypt-datakey.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ static void _test_create_datakey_with_retry(_mongocrypt_tester_t *tester) {
445445
ASSERT_OK(mongocrypt_kms_ctx_feed(kms_ctx, TEST_FILE("./test/data/rmd/kms-decrypt-reply-429.txt")), kms_ctx);
446446
// In-place retry is indicated.
447447
ASSERT(mongocrypt_kms_ctx_should_retry(kms_ctx));
448-
ASSERT(mongocrypt_kms_ctx_fail(kms_ctx));
449448
// Feed a successful response.
450449
ASSERT_OK(mongocrypt_kms_ctx_feed(kms_ctx, TEST_FILE("./test/data/kms-aws/encrypt-response.txt")), kms_ctx);
451450
ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
@@ -454,6 +453,44 @@ static void _test_create_datakey_with_retry(_mongocrypt_tester_t *tester) {
454453
mongocrypt_destroy(crypt);
455454
}
456455

456+
// Test that HTTP error can be retried after two retryable errors:
457+
{
458+
mongocrypt_t *crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
459+
mongocrypt_ctx_t *ctx = mongocrypt_ctx_new(crypt);
460+
ASSERT_OK(
461+
mongocrypt_ctx_setopt_key_encryption_key(ctx,
462+
TEST_BSON("{'provider': 'aws', 'key': 'foo', 'region': 'bar'}")),
463+
ctx);
464+
ASSERT_OK(mongocrypt_ctx_datakey_init(ctx), ctx);
465+
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
466+
mongocrypt_kms_ctx_t *kms = mongocrypt_ctx_next_kms_ctx(ctx);
467+
ASSERT_OK(kms, ctx);
468+
mongocrypt_binary_t *retryable_http = TEST_FILE("./test/data/rmd/kms-decrypt-reply-429.txt");
469+
470+
// Feed a retryable HTTP error:
471+
{
472+
ASSERT_OK(mongocrypt_kms_ctx_feed(kms, retryable_http), kms);
473+
ASSERT(mongocrypt_kms_ctx_should_retry(kms));
474+
}
475+
476+
// Feed a retryable HTTP error again:
477+
{
478+
ASSERT_OK(mongocrypt_kms_ctx_feed(kms, retryable_http), kms);
479+
ASSERT(mongocrypt_kms_ctx_should_retry(kms));
480+
}
481+
482+
// Feed a successful response:
483+
{
484+
ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/kms-aws/encrypt-response.txt")), kms);
485+
ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
486+
_mongocrypt_tester_run_ctx_to(tester, ctx, MONGOCRYPT_CTX_DONE);
487+
}
488+
489+
_mongocrypt_tester_run_ctx_to(tester, ctx, MONGOCRYPT_CTX_DONE);
490+
mongocrypt_ctx_destroy(ctx);
491+
mongocrypt_destroy(crypt);
492+
}
493+
457494
// Test that a network error is retried.
458495
{
459496
mongocrypt_t *crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);

0 commit comments

Comments
 (0)