google-auth-library-php icon indicating copy to clipboard operation
google-auth-library-php copied to clipboard

Add locking to FetchAuthTokenCache

Open ericnorris opened this issue 6 years ago • 1 comments

Per #225, filing an additional ticket for this feature request. Thanks again!

Is your feature request related to a problem? Please describe.

The SysVCacheItemPool class points out that calling code should handle race conditions, but the FetchAuthTokenCache class, which is used by the google-cloud-php library when the authCache parameter is passed in (see here), does not do any locking.

This means that users that follow code examples such as the following (from here):

$authCache = new SysVCacheItemPool();
$sessionCache = new SysVCacheItemPool([
    // Use a different project identifier for ftok than the default
    'proj' => 'B'
]);

$spanner = new SpannerClient([
    'authCache' => $authCache
]);

...may run into race condition issues unknowingly.

Describe the solution you'd like

Either of the two:

  • FetchAuthTokenCache adds a locking mechanism to safely use the passed inCacheItemPoolInterface
  • I file an issue in the google-cloud-php repository to request it use a cache with locking built in.

ericnorris avatar Apr 22 '19 21:04 ericnorris

I am trying to fix this for v2.0 but I can't seem to recreate the error. I can add some locking to ftok, but I'd like to confirm the fix. I've done the following:

> while [ true ]; do php -r "ftok('/tmp/foo', 'A');"; done &
> while [ true ]; do php -r "ftok('/tmp/foo', 'B');"; done

I was hoping this would eventually throw the error but it hasn't happened yet. I've also tried this:

use Google\Cache\SysVCacheItemPool;

while (true) {
    $rand = rand() % 256;
    $key = chr($rand);
    $var = 'varname' . $key;
    $$var = new Google\Cache\SysVCacheItemPool(['proj' => $key]);
}

bshaffer avatar Feb 22 '21 17:02 bshaffer