hook_exit() documentation states that this hook is run at the end of each page request, even for cached page views. This is actually not true. There are several places in core where exit() was used, but none of them were preceded with a hook_exit() trigger (except page caching handling in bootstrap.inc). For example:

Some contributed modules (imagecache, image_captcha) don't invoke hook_exit() either.

Are there any guidelines when hook_exit() should be invoked and when not? Shouldn't module_invoke_all('exit') be always called for clean termination?

I think it's good idea to make invoking hook_exit() mandatory for each page request.

CommentFileSizeAuthor
#12 Drupal-1305814-12.patch986 byteselachlan
#10 1305814-10.patch592 bytesgiammi
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mdupont’s picture

Title: hook_exit() usage in core » hook_exit() description is misleading
Component: base system » documentation

hook_exit() is triggered in the context of "regular" page requests, but not during AJAX requests, file download, etc. The documentation is not very clear about that and could be improved.

jhodgdon’s picture

Version: 6.x-dev » 8.x-dev
Category: support » bug

The comments in #1 seem to apply to the Drupal 7/8 documentation for hook_exit() as well. So let's get it all documented right there first, then tackle D6.

jhodgdon’s picture

The comments in #1 seem to apply to the Drupal 7/8 documentation for hook_exit() as well. So let's get it all documented right there first, then tackle D6.

TonyK’s picture

Revisiting this issue after a long time.

hook_exit() is triggered in the context of "regular" page requests, but not during AJAX requests, file download, etc. The documentation is not very clear about that and could be improved.

@mdupont. Shouldn't it be a good practice to call hook_exit() on AJAX pages either? The documentation states that

It is often used for page logging and specialized cleanup.

These sort of tasks usually need to be performed on literary every page request.

Also,

This hook MUST NOT print anything because by the time it runs the response is already sent to the browser.

Therefore, there shouldn't be any undesirable consequences for downloading files etc.

jhodgdon’s picture

RE #4 - it is too late to change the behavior for Drupal 7 or 6 of when hook_exit() is being invoked.

If you'd like to get it changed in Drupal 8. then we should split this off into two issues:
1) change the behavior in Drupal 8
2) document the current behavior in Drupal 6/7/8 (for 8.x, if the behavior is changed, the docs would be updated then).

We can use the present issue for either (1) or (2) but not both... currently its title is appropriate for (2) and this issue was originally filed against Drupal 6.x, where only (2) is appropriate, so I'd suggest filing a separate issue for (1).

dawehner’s picture

Just to document it: hook_exit() will be removed: #1911178: Remove hook_exit()

jhodgdon’s picture

RE #6 - that doesn't look all that certain, judging by the last few comments on the issue. Maybe... Anyway if it goes away we'll move this to D7. Meanwhile we could use a patch.

elachlan’s picture

Version: 8.x-dev » 7.x-dev

hook_exit() has been removed from drupal 8, this is a documentation issue and as such I am moving it back to drupal 7.

See #1911178: Remove hook_exit() for more details.

jhodgdon’s picture

Thanks! OK, it looks like all we need to do is change the documentation to say "most regular" page requests instead of "each" page request. That seems like a good Novice project.

giammi’s picture

Status: Active » Needs review
FileSize
592 bytes

trying...

jhodgdon’s picture

Status: Needs review » Needs work

Thanks! The text looks good, but we have a coding standard that all comments (including doc blocks) need to wrap at 80 characters or less. So we need a new patch that re-wraps that paragraph, because with the additional words it goes over 80 characters now.

See
https://drupal.org/coding-standards/docs#drupal

elachlan’s picture

Status: Needs work » Needs review
FileSize
986 bytes

re-rolled with wraps.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks! I'll get it committed soon.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Thanks again, all! I committed this to 7.x, and I also went into the 6.x documentation project repository and made the same change there. Normally I would ask for a formal backport, but in 6.x the hooks are not even in the Drupal Core repo and issues in the Documentation project tend to get lost forever, so I just took care of it.

Automatically closed -- issue fixed for 2 weeks with no activity.