In the apachesolr issue queue we came across a major issue with cron: #1953976: avoid use variable_set() in anything related to indexing content (because of performance)
The issue occurs if variable_set() gets called during any implementation of hook_cron.
What happens?
variable_set() invalidates the variable cache for bootstrap:
<?php
function variable_set($name, $value) {
global $conf;
db_merge('variable')->key(array('name'=> $name))->fields(array('value'=> serialize($value)))->execute();
cache_clear_all('variables', 'cache_bootstrap');
$conf[$name] = $value;
}
?>
The next request rebuilds the cache_bootstrap in DRUPAL_BOOTSTRAP_VARIABLES by calling variable_initialize() and all further requests have to wait until it's finished:
<?php
function variable_initialize($conf = array()) {
// NOTE: caching the variables improves performance by 20% when serving
// cached pages.
if ($cached = cache_get('variables', 'cache_bootstrap')) {
$variables = $cached->data;
}
else {
// Cache miss. Avoid a stampede.
$name = 'variable_init';
if (!lock_acquire($name, 1)) {
// Another request is building the variable cache.
// Wait, then re-run this function.
lock_wait($name);
return variable_initialize($conf);
}
else {
// Proceed with variable rebuild.
$variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
cache_set('variables', $variables, 'cache_bootstrap');
lock_release($name);
}
}
foreach ($conf as $name => $value) {
$variables[$name] = $value;
}
return $variables;
}
?>
That behavior was introduced by #561990: Avoid variable_set() and variable_del() stampedes.
When does it happen?
As soon as you enable core search, statistics or tracker (probably rare used old modules). See these functions:
Is it critical?
Yes, under bad circumstances. If you have enabled one of the modules mentioned above and the cron runs during a high traffic period. A large amount of waiting requests may kill your webserver.
How to fix the issue?
I don't know exactly. Maybe in core the code could be improved. But the number of contrib modules that are doing something similar like calling variable_set in hook_cron or somewhere else during a normal page request is unpredictable. So the best solution seems to be to modfiy variable_set itself, which might not be easy ...