How to execute request-specific tasks after page response is returned. Does hook_exit() actaully do this? I'm trying to figure out if it's possible to "hide" the perceived execution time of some end-of-request logging tasks that are page specific and depend on the availability of the Drupal DB API. Essentially I'd like to run these tasks after the page response is returned in full to the browser. I've always just assumed that hook_exit() was the right choice for this kind of thing but after looking at its invocation within drupal_page_footer() in some detail, I'm not so sure. Here's the notable snippet from drupal_page_footer():

module_invoke_all('exit');

// Commit the user session, if needed.
drupal_session_commit();

if (variable_get('cache', 0) &amp;&amp; ($cache = drupal_page_set_cache())) {
drupal_serve_page_from_cache($cache);
}
else {
  ob_flush();
}


So it looks like both cached and un-cached responses are not sent to the browser until after all hook_exit() implementations run (flushing the buffer and executing drupal_serve_page_from_cache() both happen after module_invoke_all('exit')). This seems a bit contrary to the hook_exit() documentation which states that, "This hook MUST NOT print anything because by the time it runs the response is already sent to the browser."

Am I perhaps misinterpreting the way hook_exit() implementations work in relation to the response delivery? If not, is there some alternative way to trigger custom logging logic later in the response (while DB access is still available)? Perhaps by registering a shutdown function or something to that effect?
You're looking for fastcgi_finish_request(). Recommend using it inside of a shutdown function. If you need something else the HTTPRL module should be able to do what you need with threading.