Skip to content

Commit

Permalink
Fixes infinite loop in CredentialProvider (#1705)
Browse files Browse the repository at this point in the history
* Fixes infinite loop in CredentialProvider if InstanceProfileProvider throws

* Updated test name to be more meaningful

* Added changelog document
  • Loading branch information
MattStypa authored and howardlopez committed Jan 9, 2019
1 parent 6570fab commit 311bdfd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/credential_provider_loop_fix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "Credentials",
"description": "Fixes a failure loop if InstanceProfileProvider fails."
}
]
5 changes: 5 additions & 0 deletions src/Credentials/CredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public static function memoize(callable $provider)
}
// Refresh the result and forward the promise.
return $result = $provider();
})
->otherwise(function($reason) use (&$result) {
// Cleanup rejected promise.
$result = null;
return new Promise\RejectedPromise($reason);
});
};
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Credentials/CredentialProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ public function testMemoizes()
$this->assertEquals(1, $called);
}

public function testMemoizesCleansUpOnError()
{
$called = 0;
$f = function () use (&$called) {
$called++;
return Promise\rejection_for('Error');
};
$p = CredentialProvider::memoize($f);
$p()->wait(false);
$p()->wait(false);
$this->assertEquals(2, $called);
}

public function testCallsDefaultsCreds()
{
$k = getenv(CredentialProvider::ENV_KEY);
Expand Down

0 comments on commit 311bdfd

Please sign in to comment.