CacheBase::deleteMultiple() throws ServiceNotFoundException for "database" during container compilation
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3575945. -->
Reported by: [foxtrotcharlie](https://www.drupal.org/user/27895)
Related to !91 !90
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>We found that ExistingSite tests (weitzman/drupal-test-traits), were failing with this error:</p>
<pre> Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:<br> You have requested a non-existent service "database"</pre><p>Redis 1.11 introduced a hasContainer() guard in CacheBase::deleteMultiple() to allow cache deletes during container rebuild:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br>$in_transaction </span><span style="color: #007700">= \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">hasContainer</span><span style="color: #007700">() && \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">database</span><span style="color: #007700">()-></span><span style="color: #0000BB">inTransaction</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>On Drupal 11.3, HookCollectorKeyValueWritePass (a new compiler pass) triggers a cache delete during container compilation. At that point hasContainer() returns true (the container builder is set), but the database service definition seems to not yet been registered causing the error.</p>
<p>This breaks ExistingSite tests (weitzman/drupal-test-traits) during setUp() and <em>may</em> affect other scenarios where Redis is active during container compilation.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<p>1. Use drupal/redis 1.11 with Drupal 11.3.3<br>
2. Enable Redis as a cache backend (e.g. REDIS_HOST set in environment)<br>
3. Run ExistingSite tests (weitzman/drupal-test-traits)</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Add \Drupal::hasService('database') to the guard in CacheBase::deleteMultiple():</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br>$in_transaction </span><span style="color: #007700">= \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">hasContainer</span><span style="color: #007700">() && \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">hasService</span><span style="color: #007700">(</span><span style="color: #DD0000">'database'</span><span style="color: #007700">) && \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">database</span><span style="color: #007700">()-></span><span style="color: #0000BB">inTransaction</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span></pre></div>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>Create MR.</p>
<h3 id="summary-ui-changes">User interface changes</h3>
<p>None.</p>
<h3 id="summary-api-changes">API changes</h3>
<p>None.</p>
<h3 id="summary-data-model-changes">Data model changes</h3>
<p>None.</p>
issue
GitLab AI Context
Project: project/redis
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/redis/-/raw/2.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/redis
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD