Problem/Motivation

If there is an exception during a module's installation then error message/exception is not persistent.

Proposed resolution

Store the excpetion in a log to allow for later retrieval.

Remaining tasks

Reroll
Review

User interface changes

None

API changes

None

Original report by matt2000

If hook_install fails, it can leave the system in an invalid state. We need to know when this happens.

This patch is also required to make #793274: Schema is left broken if hook_install fails testable.

Issue fork drupal-793660

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

matt2000’s picture

Status: Active » Needs review
berdir’s picture

Status: Needs review » Needs work

This pretty much eats the original error, doesn't it? This would make it really hard to actually find and solve the bug (think of porting a module to D7 and something as simple as still having the schema install function calls in there...)

I think we should log the exception message too. There are two ways to do it:

- Use _drupal_decode_exception() and extend the watchdog text
- #711108: Richer exception reporting for watchdog() makes it possible to pass exceptions directly to watchdog and it will handle the decoding itself. Note that it would not allow to add a custom addtional text to the watchdog call, so you might want to decode it yourself.

David_Rothstein’s picture

I think that ideally the error would bubble up so that a calling function can use it; e.g. the calling function might want to drupal_set_message() to indicate the module did not install correctly. Also, as I mentioned in the other issue, I think rather than just logging the error we should be calling drupal_uninstall_schema() to restore it back to its uninstalled state.

Since #793274: Schema is left broken if hook_install fails was committed but purposely left open for further work to happen, would it now be better to close this issue in favor of that one?

matt2000’s picture

D7 really suggests that Schema Install and Module Install are two separate and independent things. I think automatically calling durpal_uninstall_schema() is not always the right thing to do, and should we left to the user. As long as we correctly identify whether the schema was installed, we already provide the mechanism for the user to do this. (via uninstall.)

As an example, imagine a module which creates an Entity, and sets up a number of tables for that entity via hook schema, and then imports a base data set (e.g., postal codes for a geo-location module) and sets up some FieldsAPI fields in hook_install. Maybe the data import takes significant time.

If the schema installs correctly, and the data imports successfully, but the fieldsAPI fields are not created because some other modules created a fields with the same name, do we really want to force the user to go back to square one? We've wasted a lot of cycles on perfectly good work if we do.

Or what if the order is different, and we successfully create the Fields before we fail? We would need to also remove the fields automatically, otherwise we'll cause an error when they try to re-install.

So I'm inclined to think the best route is to continue to decouple schema from install, not rejoin them.

(That's also why two separate issues seemed appropriate, although I have no problem with closing this one, if the code here is definitely not useful, and that is probably the case.)

berdir’s picture

You are probably right, not sure :)

That imho doesn't change the fact that we want to move the exception handling out of module_enable() into the calling functions or do you disagree ?(The obvious difference would be additional modules won't be installed when the exception bubbles out of module_enable().)

I think we want to handle this differently based on where we are calling this, according to http://api.drupal.org/api/function/module_enable/7, we are using the function during installation, update functions, installing new modules (and simpletest, which is not listed there).

Not sure how to handle it... Adding an argument to decide between continue and re-throwing of the exception is probably ugly...

matt2000’s picture

I think I get the concern, but I think we need something inside of module_enable in order to make #793274: Schema is left broken if hook_install fails testable.

moshe weitzman’s picture

Priority: Critical » Normal

IMO, this is not critical. Why should hook_install() fail? In your example, developer should have loaded data in separate stage. That stage should be a drush command or if web is required, then use batch api.

superspring’s picture

Version: 7.x-dev » 8.x-dev
Assigned: matt2000 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.15 KB

I like the idea of recording the exception in watchdog. This gives easy access to the error.
Attached is a patch which catches the error, records it and allows the other modules to be installed.

superspring’s picture

StatusFileSize
new2.12 KB

After a review with @chx the exception is thrown again to prevent compounding problems and to ensure the user is notified of the fault.

chx’s picture

I am not quite sure what the point in this after all if hook_install dies, the only way forward is to restore your site from backup as your site in an unknown state. It's not impossible that even after fixing whatever caused the exception and deleting manually the schema version, a second run will choke on something else -- namely the first half of the installer that already ran and might not be re-entrant. It's just easier to restore. And if you restore, your logs are gone, too. So... why?

superspring’s picture

Issue summary: View changes

Using Issue Summary Template.

chx’s picture

Status: Needs review » Reviewed & tested by the community

OK, so the issue summary now contains the motivation: provide more information in the log. Might be useful for developers so why not.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

No tests?

superspring’s picture

Status: Needs work » Needs review
StatusFileSize
new7.5 KB

Same patch as above but with tests.

swentel’s picture

Status: Needs review » Needs work
+++ b/core/includes/errors.incundefined
@@ -94,12 +94,24 @@ function _drupal_decode_exception($exception) {
+          // Oh noes, a closure!

We don't really do urban language in our comments :)

+++ b/core/modules/system/tests/modules/common_test_install_helper/common_test_install_helper.installundefined
@@ -0,0 +1,14 @@
+  // Oh noes, bad code throws an exception!

Same here.

superspring’s picture

Status: Needs work » Needs review
StatusFileSize
new7.53 KB

Same patch with less 'urban language'.

xen’s picture

Assigned: Unassigned » xen

Checking if this is still current....

xen’s picture

Assigned: xen » Unassigned
StatusFileSize
new6.77 KB

Fixed up patch to latest head. module_enable doesn't exist anymore, the test modules info file needed rewriting to yaml, and other bits and pieces.

xen’s picture

Issue summary: View changes

Forgot username.

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll
kerby70’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new5.82 KB
new3.73 KB

Reroll attached.

Some of the comments that were being added to are already in now.

mgifford’s picture

Status: Needs review » Needs work

No longer applies.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
larowlan’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Needs reroll, +Bug Smash Initiative

Is this still relevant?

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

quietone’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs work
Issue tags: +DX

I looked at ModuleInstaller and this change could still be applied so I am setting this to NW.

bhanu951’s picture

Assigned: Unassigned » bhanu951
_utsavsharma’s picture

StatusFileSize
new2.38 KB

did not find these files on 9.2.x
common_test_install_helper.module, common_test_install_helper.install, common_test_install_helper.info.yml , ExceptionTest.php.
Reroll for 9.2.x

bhanu951’s picture

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

bhanu951’s picture

Assigned: bhanu951 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll

Re- Rolled the patch updated Test for 10.1.x branch.

smustgrave’s picture

Status: Needs review » Needs work

Hiding the files as the fix appears in the MR.

MR seems to have failures

Did not review.

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.

andypost’s picture

It's often happening when some config entity is changed in hook_install but $syncing variable is not checked

It fails randomly when install happening from existing config

quietone’s picture

Changing to the DX special tag defined on Issue tags -- special tags.

bhanu951’s picture

StatusFileSize
new5.65 KB

Uploading MR changes as backup patch before changing target branch.

nicxvan’s picture

Component: system.module » extension system
nicxvan’s picture

This needs a recollection but would probably be nice to get in.

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.