Clearing sessionPool before closing db connection generates broken cache file.
Here is the php code for reproduction.
<?php
require_once __DIR__.'/../../vendor/autoload.php';
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Google\Cloud\Spanner\Session\CacheSessionPool;
use Google\Cloud\Spanner\SpannerClient;
$projectId = '...';
$instanceId = '...';
$databaseId = '...';
$cacheDir = sys_get_temp_dir() . '/spanner-cache';
echo 'cacheDir: ' . $cacheDir . PHP_EOL;
$cacheItemPool = new FilesystemAdapter('', 0, $cacheDir);
$sessionPool = new CacheSessionPool($cacheItemPool);
$spanner = new SpannerClient(['projectId' => $projectId]);
$db = $spanner->connect($instanceId, $databaseId, ['sessionPool' => $sessionPool]);
$sessionPool->warmup();
for($i = 0; $i < 5; $i++) {
$result = $db->execute('SELECT ' . random_int(0, 10000))->rows()->current();
printf('result %d: %s'.PHP_EOL, $i, array_values($result)[0]);
}
$sessionPool->clear();
// Cache file broken
$db->close();
Executing this php script causes following warnings (and create broken cache file).
cacheDir: /.../spanner-cache
result 0: 1013
result 1: 2345
result 2: 764
result 3: 3854
result 4: 3992
PHP Warning: array_push() expects parameter 1 to be array, null given in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 306
PHP Stack trace:
PHP 1. {main}() /.../public/spanner-test/session-not-found.php:0
PHP 2. Google\Cloud\Spanner\Database->close() /.../session-not-found.php:36
PHP 3. Google\Cloud\Spanner\Session\CacheSessionPool->release() /.../vendor/google/cloud-spanner/src/Database.php:1380
PHP 4. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:309
PHP 5. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
PHP 6. array_push() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:306
Warning: array_push() expects parameter 1 to be array, null given in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 306
Call Stack:
0.0004 369744 1. {main}() /.../public/spanner-test/session-not-found.php:0
1.2530 6222616 2. Google\Cloud\Spanner\Database->close() /.../public/spanner-test/session-not-found.php:36
1.2530 6222616 3. Google\Cloud\Spanner\Session\CacheSessionPool->release() /.../vendor/google/cloud-spanner/src/Database.php:1380
1.2530 6223312 4. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:309
1.2530 6223360 5. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
1.2579 6413192 6. array_push() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:306
Then again, executing the same script causes fatal error because of broken cache file.
cacheDir: /.../spanner-cache
PHP Notice: Undefined index: inUse in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 588
PHP Stack trace:
PHP 1. {main}() /.../public/spanner-test/session-not-found.php:0
PHP 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
PHP 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
PHP 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
PHP 5. Google\Cloud\Spanner\Session\CacheSessionPool->getSessionCount() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:397
Notice: Undefined index: inUse in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 588
Call Stack:
0.0003 369744 1. {main}() /.../public/spanner-test/session-not-found.php:0
0.0770 4491728 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
0.0770 4492048 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
0.0771 4492096 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
0.0821 4493104 5. Google\Cloud\Spanner\Session\CacheSessionPool->getSessionCount() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:397
PHP Notice: Undefined index: toCreate in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 589
PHP Stack trace:
PHP 1. {main}() /.../public/spanner-test/session-not-found.php:0
PHP 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
PHP 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
PHP 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
PHP 5. Google\Cloud\Spanner\Session\CacheSessionPool->getSessionCount() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:397
Notice: Undefined index: toCreate in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 589
Call Stack:
0.0003 369744 1. {main}() /.../public/spanner-test/session-not-found.php:0
0.0770 4491728 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
0.0770 4492048 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
0.0771 4492096 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
0.0821 4493104 5. Google\Cloud\Spanner\Session\CacheSessionPool->getSessionCount() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:397
PHP Notice: Undefined index: toCreate in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 402
PHP Stack trace:
PHP 1. {main}() /.../public/spanner-test/session-not-found.php:0
PHP 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
PHP 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
PHP 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
Notice: Undefined index: toCreate in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 402
Call Stack:
0.0003 369744 1. {main}() /.../public/spanner-test/session-not-found.php:0
0.0770 4491728 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
0.0770 4492048 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
0.0771 4492096 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() /.../vendor/google/cloud-core/src/Lock/LockTrait.php:67
PHP Fatal error: Uncaught Error: Unsupported operand types in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:402
Stack trace:
#0 /.../vendor/google/cloud-core/src/Lock/LockTrait.php(67): Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}()
#1 /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php(407): Google\Cloud\Core\Lock\SemaphoreLock->synchronize(Object(Closure))
#2 /.../public/spanner-test/session-not-found.php(20): Google\Cloud\Spanner\Session\CacheSessionPool->warmup()
#3 {main}
thrown in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 402
Fatal error: Uncaught Error: Unsupported operand types in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 402
Error: Unsupported operand types in /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php on line 402
Call Stack:
0.0003 369744 1. {main}() /.../public/spanner-test/session-not-found.php:0
0.0770 4491728 2. Google\Cloud\Spanner\Session\CacheSessionPool->warmup() /.../public/spanner-test/session-not-found.php:20
0.0770 4492048 3. Google\Cloud\Core\Lock\SemaphoreLock->synchronize() /.../vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:407
0.0771 4492096 4. Google\Cloud\Spanner\Session\CacheSessionPool->Google\Cloud\Spanner\Session\{closure}() #####################yyyworkspace/laravel-extension-template/vendor/google/cloud-core/src/Lock/LockTrait.php:67
Cache should be able to be cleared whenever needed. However, this session pool creates broken cache and prevent us from making the next connection.
Thank you very much for the bug report @yuichiro12! I opened #1071 which should resolve this issue. Would you be able to check out the change and confirms it works, please?
@yuichiro12 We've verified the patch and will get this out in a bugfix release today. thanks again!
Most helpful comment
@yuichiro12 We've verified the patch and will get this out in a bugfix release today. thanks again!