Skip to content

Race condition in SysVCacheItemPool constructor #225

@ericnorris

Description

@ericnorris

I am using the CacheSessionPool in the Google Cloud Spanner PHP library for caching Spanner sessions.

In order to construct the CacheSessionPool, you are required to pass in a SysVCacheItemPool. Although the CacheSessionPool uses locking when interacting with the SysVCacheItemPool, there appears to be a race condition in __construct meaning you probably need to do locking yourself in order to create the object.

loadItems is called in __construct: https://github.com/googleapis/google-auth-library-php/blob/v1.4.0/src/Cache/SysVCacheItemPool.php#L135

loadItems then calls shm_attach: https://github.com/googleapis/google-auth-library-php/blob/v1.4.0/src/Cache/SysVCacheItemPool.php#L88-L92.

shm_attach attempts to 1) get the shared memory segment if it exists 2) create it if not: https://github.com/php/php-src/blob/PHP-7.2/ext/sysvshm/sysvshm.c#L171-L182.

If two processes attempt to construct a SysVCacheItemPool at the same time, see that there is not an existing shared memory segment, and then try to create one with IPC_CREAT | IPC_EXCL the WARNING on L178 (https://github.com/php/php-src/blob/PHP-7.2/ext/sysvshm/sysvshm.c#L177-L180) will occur.

Normally this may be okay, but I am running in an environment where I convert warnings to exceptions.

This could maybe be solved by not calling loadItems in the constructor, or I could file an issue in the https://github.com/googleapis/google-cloud-php repository to see what changes may need to be made there.

Environment details

Steps to reproduce

Not straightforward, since it is a race condition. You could try creating many SysVCacheItemPools in a tight loop in multiple processes.

Metadata

Metadata

Assignees

Labels

type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions