Follow-up from #3065283: Don't modify maintenance page registry on DB failure which discovered this bug.

Problem/Motivation

The _template_preprocess_default_variables() helper function provides $variables['db_is_active'], but its meaning has changed over time.

When #688294: Switch from db_is_active() to proper exception catching was committed, it used drupal_is_front_page() to determine whether there was an active DB connection and, if an exception was thrown, would set $variables['db_is_active'] to FALSE accordingly.

I suspect that originally, drupal_is_front_page() used to initiate a DB call somewhere along the chain.

This is, however, no longer the case as I suspect that, over time, these subsequent functions were optimized so avoid DB calls.

Thus, this variable really only means "in maintenance mode", not "is the database active".

Proposed resolution

Actually check whether the DB is accessible.

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

Issue fork drupal-3082948

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

markcarver created an issue. See original summary.

markhalliwell’s picture

Version: 7.x-dev » 8.8.x-dev
Issue tags: +Needs backport to D7

I suppose this (variable name being misleading) is true of HEAD as well:https://git.drupalcode.org/project/drupal/blob/9af697c53bfb03c48b2fb145f...

Seems 8.x simply did away with the try/catch block after entirely, but its value is still just a reference to !defined('MAINTENANCE_MODE').

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

This came up as the daily BSI target.

This one is tricky as I'm not sure it's a bug as it appears to be by design. The key 'db_is_active' could be confusing so maybe that could be argued but the fact it doesn't call the DB anymore seems to be a decision.

Kinda not sure what should happen here..

longwave’s picture

So db_is_active is set in two places:

        'db_is_active' => !defined('MAINTENANCE_MODE'),

then

    // An exception might be thrown.
    try {
      $variables['is_front'] = $this->pathMatcher->isFrontPage();
    }
    catch (\Exception) {
      // If the database is not yet available, set default values for these
      // variables.
      $variables['is_front'] = FALSE;
      $variables['db_is_active'] = FALSE;
    }

I think the second one actually covers the case that the IS talks about? However this is also called without a guard elsewhere, so the try/catch here is likely ineffective on its own as an exception can happen elsewhere.

    // Add a variable for the root path. This can be used to create a class and
    // theme the page depending on the current path (e.g. node, admin, user) as
    // well as more specific data like path-frontpage.
    $is_front_page = $this->pathMatcher->isFrontPage();

db_is_active is in turn used to set an duplicate flag:

    if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
      $variables['db_offline'] = TRUE;
    }

This is then documented in html.html.twig and applies a db-offline class if the database is offline. There is no CSS for the class, however.

I also searched contrib and can find no real usage of any of these:

https://search.tresbien.tech/search?q=db_is_active
https://search.tresbien.tech/search?q=db_offline
https://search.tresbien.tech/search?q=db-offline

This feels somewhat legacy and I think we should consider deprecating it all for removal.

longwave-bot made their first commit to this issue’s fork.

longwave’s picture

Status: Active » Needs review

Full disclosure: trying something new here, using an AI agent to work on issues and tests in a private clone of core. There is always a human in the loop, I manually review and approve all output before pushing back here. Commits and pushes by @longwave-bot are from the agent.

The changes here were written by DeepSeek v4 Flash.

smustgrave’s picture

Status: Needs review » Needs work

Mind tweaking the IS seems like we are still just checking maintenance mode, which I'm fine with but can we document that please