Skip to content

fix: resolve race conditions in Redis TTL and prevent cache test state leakage#10374

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/cache-tests-race-conditions
Open

fix: resolve race conditions in Redis TTL and prevent cache test state leakage#10374
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/cache-tests-race-conditions

Conversation

@gr8man

@gr8man gr8man commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Description
This PR fixes persistent and random failures when running the cache test suite (tests/system/Cache) repeatedly or in a random order (--order-by=random).

Ref: #9968

Changes included:

  1. Fix TTL Race Condition in Redis/Predis Handlers: Changed the PredisHandler and RedisHandler to use the expire() command directly with a relative TTL (in seconds) instead of calculating an absolute timestamp for expireAt() via PHP's Time::now(). Using a PHP-generated timestamp makes the cache vulnerable to clock drifts between the PHP server and the Redis server. This drift resulted in random test failures (e.g. testGet and testRemember failing to assert null after waiting for 3 seconds).
  2. Prevent State Leakage in Tests: Several test classes (FileHandlerTest, RedisHandlerTest, PredisHandlerTest, ApcuHandlerTest, MemcachedHandlerTest) inherited testDeleteMatching which temporarily created 50-100 cache entries. The tearDown() methods previously only deleted three predefined default keys (key1, key2, key3), leaving dozens of dummy keys behind. This leaked state caused random Directory not empty system errors for FileHandler and false-positive assertion failures in tests like testDeleteMatchingNothing. tearDown() now properly calls $this->handler->clean() to guarantee a perfectly clean state for the next random test.
  3. Proper ApcuHandlerTest Skip Logic: Added an explicit check in ApcuHandlerTest's setUp() to verify apcu_enabled(). This correctly skips the test when APCu CLI mode is disabled instead of falling back to the DummyHandler and producing cascaded test failures.

Checklist:

  • Securely signed commits (if configured in your environment)
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@gr8man

gr8man commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

IMPORTANT

Note to reviewers and developers: Please ensure that tests are run regularly with the --order-by=random flag during development. Identifying test pollution and global state leakage bugs at a later stage is extremely difficult and time-consuming, as these failures only manifest under very specific test suite execution orders.

@gr8man gr8man changed the title Fix cache tests race conditions and teardown memory leaks fix: resolve race conditions in Redis TTL and prevent cache test state leakage Jul 4, 2026
@gr8man gr8man force-pushed the fix/cache-tests-race-conditions branch from 7827c6e to 260ac10 Compare July 4, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant