2

I have a problem with drupal_register_shutdown_function(): my callback is not being called =| Here's example code:

<?php
/**
 * Implements hook_node_update().
 */
function example_node_update($node) {
  if ($node->type == 'test') {

    drupal_register_shutdown_function('_example_shutdown');
    dpm(drupal_register_shutdown_function()); // This outputs callbacks array showing that '_example_shutdown' really gets registered.
  }
}


function _example_shutdown() {
  dpm('Shutdown callback executed'); // This is not shown
}

I use php-fpm, and php.net register_shutdown_function discussion suggests to use fastcgi_finish_request() instead of register_shutdown_function() with php-fpm. Also, see this D8 issue: "Ensure register_shutdown_function() works with php-fpm". Is there any D7 workaround?

I think there could be two different approaches:

  1. Global: Fix drupal_register_shutdown_function() to check for php-fpm environment and use fastcgi_finish_request() in that case.

  2. Local: Use some hook to force _drupal_shutdown_function(). Maybe hook_exit() would be ok for some cases, but in my case it is too late (as my callback updates nodes, and by the time hook_exit() runs the response is already sent to the browser, so user would get an "old" node, I'm afraid).

Maybe there is something simpler that I'm missing?

1 Answer 1

5

Example shutdown is getting called ; you can't use dpm in a shutdown function though, because at that point the session already got written to the database and there's just no way to add a new message to it.

In PHP-FPM, the connection also could have already been closed, so print or var_dump is not really a good test to check if you're shutdown function is getting called in general ; it should work in D7 though if you're not using a module that's specifically closing the request, so you should be able to use print "Hello world"; and get it to show on screen (that's what the core "Shutdown functions tests" is doing in D7).

If you want something realiable to use in a shutdown function to check if it got invoked, try something like file_put_contents('/tmp/drupal-debug.txt', 'Shutdown function called');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.