Problem/Motivation

Editing views on 11.2.0 gets the following error

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep()

Note that any object that uses the StringTranslationTrait and is serialized could hit this issue.

Steps to reproduce

  1. Install standard Drupal in French
  2. Edit the admin/content view
  3. Change a field name for example the actions field

Proposed resolution

Do not serialize the \Drupal\locale\LocaleTranslation::$translations property. It contains LocaleLookup objects that should not be serialized.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Issue fork drupal-2987548

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

pratip.ghosh created an issue. See original summary.

cilefen’s picture

I added a core issue that links to some others. In addition there have been reports of this being caused by some contributed modules.

Also: Please upgrade to 8.5.5 at your earliest convenience. We are not supporting 8.4.x releases at this time.

pratip.ghosh’s picture

Version: 8.4.3 » 8.5.5

Yes, already updated to 8.5.5. Still facing the issue.

aleksip’s picture

I get this exception in AJAX calls if I use object methods as callbacks, and don't get the exception if I use traditional callbacks.

john.oltman’s picture

If you are using the Locale module this may be relevant.

Version: 8.5.5 » 8.5.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.5.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. Branches prior to 8.8.x are not supported, and 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.

HbtTundar’s picture

StatusFileSize
new2.78 KB
HbtTundar’s picture

cilefen’s picture

Status: Active » Needs review
a.milkovsky’s picture

Try using the Drupal\Core\DependencyInjection\DependencySerializationTrait in your class, as it was done in #2893029: EntityType objects cannot be reliably serialized without DependencySerializationTrait.

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
euphoric_mv’s picture

euphoric_mv’s picture

The last submitted patch, 14: local-lookup-fix-serialization-2987548-14.patch, failed testing. View results

4kant’s picture

#15 works for me on Drupal 9.3.5

Thanks

a.dmitriiev’s picture

Status: Needs review » Reviewed & tested by the community

#15 works for me too.

The problem itself exists when you have more than 1 language installed and use the translation inside ajax form. The ajax form is cached and then the translated strings are serialized with everything that translation object has, including the locale lookup that has database connection as dependency.

keoal’s picture

I am also receiving the error when trying to upload images as part of a webform submission, this is only happening for anonymous users however and the images upload when I am logged in as admin.

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1993 of /web/core/lib/Drupal/Core/Database/Connection.php).

I have tried the patch at #15 but this did not resolve my issue.

I have noticed a jquery error in the console when I try to upload images:

en/results/data-upload?element_parents=elements/collection/group_image&ajax_form=1&_wrapper_format=drupal_ajax 500 (500 Service unavailable (with message))

**edit**

The patch appears to have worked, there must have been a caching issue also.

klemendev’s picture

Our website only has one language and is hit by this error. File upload forms stopped working and the website is essentially broken.

Patch #15 does not seem to work, even after clearing all caches.

Changing priority to major as this bug is breaking websites from being usable in some cases

EDIT: In our case, the issue is combination of this one + https://www.drupal.org/node/3279371

klemendev’s picture

Priority: Normal » Major
alexpott’s picture

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

Can we get a test here - plus some steps to reproduce? I'm not sure that the explanation in #18 is correct. TranslatableMarkup don't serialise the stringTranslation property - see \Drupal\Core\StringTranslation\TranslatableMarkup::__sleep().

pmunch’s picture

Patch #15 does not seem to work, even after clearing all caches.

In our case, deactivating honeypot for the form (in /admin/config/content/honeypot) solved the issue immediately.

cilefen’s picture

Honeypot is causing a version of this.

markie’s picture

FWIW: I ran into this issue and it turned out to be that Honeypot was enabled on all forms which is not best practice. Once I disabled that (or more to the point, had the editor role bypass honeypot) the issue went away without the patch. Something to look at if a certain role is having the issue.

EDIT: wow in the last couple of hours both pmunch and cilefen were able to capture this as well. Great minds I guess :)

cilefen’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Needs steps to reproduce

I be am postponing this as it seems the causes may differ.

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.

randalv’s picture

This has also plagued one of our projects (which has about 180 subsites, by the way).

We've implemented the patch in #15 and this seems to work!

The error occurred when attempting to open the media library from an "Add media" button on a block form.
This happened only with translations, the block form on the original node was fine.

jukka792’s picture

I am getting this same error on D9.3.16 on my local dev and on my testing site.
But not on the production which is little bit behind with module and core versions.

It comes when saving a existing image style. (/admin/config/media/image-styles/manage/style-name/

None of these helped:

1. Uninstalled Honeypot (it was protecting only few forms) NO help
2. patched with #15 No help
3. restarted DB and APP servers and cleared cache with drush cr, No help
4. Tried to switch back to php 7.4 from 8.1 No help

The site seems weirdly slow also.

Error message is:
LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1993 of /var/www/html/site/web/core/lib/Drupal/Core/Database/Connection.php).

I will start to compare now the working production site to my local dev, what is different.

4kant’s picture

On Drupal 9.48 (to which I updated today from 9.35), this error seems to be gone.
I don't have to patch anymore (had used #15 sucsessfully on 9.35)

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

marieascomedia’s picture

Got the same problem on a Drupal 9.5.9 / 9.5.10, migrated from a 8.x, when tring to add a file in a file field in a node.
No custom code in this content type.

Patch #15 works for me.
Thanks.

aleksip’s picture

Five years later I come across this again, on Drupal 10.1.4. This time it happens if I set a translated error message in my form validation code. And does not happen with an untranslated error message. And the fix in #15 does seem to work.

Considering the nature of the exception and reading all the comments it seems likely that there are different causes for the exception, probably requiring different fixes.

I also wonder about the fix in #15, since the exception message states that DependencySerializationTrait should be looked as a temporary solution.

samlerner’s picture

I'm seeing a similar issue on a Drupal 10.1.4 site, and I've narrowed it down to the Agreement module. Based on the comments that refer to the problem being with translating form errors, I think this code is the problem:

  public function validateForm(array &$form, FormStateInterface $form_state) {
    $storage = $form_state->getStorage();
    $agreement = $storage['agreement'];
    if (!$agreement->allowsRejecting() && !$form_state->getValue('agree') && !$storage['agreed']) {
      $settings = $agreement->getSettings();
      $form_state->setErrorByName('agree', $this->t('@agree_error', ['@agree_error' => $settings['failure']]));
    }
  }

If the form is getting serialized, and the error message is using t(), then it's possible this is where the database connection is getting serialized as well. I can't reproduce the issue locally, so I'm applying the patch in #15 and will verify when the change gets to production.

I am also curious about what should be done long-term if indeed DependencySerializationTrait is meant to be temporary.

johnv’s picture

samlerner’s picture

Adding the DependencySerializationTrait didn't seem to solve my issue, I either didn't put it in the right place, or it's not the same cause of the error. I'll keep looking and see if I can figure out where it's coming from.

johnv’s picture

Indeed, the problem is in an apparently unrelated class/object.

In #35, if you replace $this->t('@agree_error' with global t('@agree_error', I expect the problem to be gone.

johnv’s picture

Version: 9.5.x-dev » 11.x-dev
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new4.74 KB

Let me re-open this issue and post a proposal patch.
In the OP, I added some comments that makes the problem reproducable.
Please check #3400483: [meta] LogicException: The database connection is not serializable (for string translations in Ajax callback), which I created with many references to other reported issues of (apparently) the same problem.
IMO, adding DependencySerializationTrait to so many other, unrelated core/contrib classes is not the way to go. (Therefore, it is euphemistically a 'temporary' solution.)

Attached patch interferes in the serialization of the TranslationManager, which contains a list of Translators (TranslatorInterface).
By NOT serializing the listed Translators, the problem disappears.
Unlike other objects, those Translators are re-created after the unserialization of the form.

In my test case, 2 Translators were listed:
- Drupal\Core\StringTranslation\Translator\CustomStrings (serializable)
- Drupal\locale\LocaleTranslation (NOT serializable)
As per comments in other topics, the second is not serializable due to an open DatabaseConnection. The object contains DependencySerializationTrait itself, but apparently that does not work.

Hence, proposed solution is in TranslationManager, not in TranslatorInterface instances.
My test case: #3399054-23: Ajax error on [Add exception] button if "seasons" is activated

Let me first upload this patch, to check the tests.
Then, let me add another patch, removing DependencySerializationTrait from some core classes.

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

artem_kondra’s picture

I have the same issue in Drupal core 10.1.6 with nl translation node, confirm that #15 solves problems, it will be great to see that in core

alexpott’s picture

@johnv I' ve tried the steps to reproduce and it does not happen for me with the address module. I've got a some questions about the steps to reproduce:

  • Is it with the Standard install profile?
  • What is the contrib field module you are using?
  • Are there any other contrib modules installed?
johnv’s picture

@alexpott, please check attached patch for some test instructions.

Test by:
- Install with normal install profile.
- Enable locale module 'User interface translation' with +1 languages.
- Enable office_hours module and add field to a Content type.
- In Field settings, Allow both Exceptions and Seasons.
- Select widget type 'Office Hours (week) with exceptions and seasons'.
- Edit with non-English page, e.g., /nl/node/8/edit .
- Enable both Exceptions and Season in widget settings.
- Create/Edit node with non-English page, e.g., /nl/node/8/edit .
- Click 'Add Exception' button. (You may need to click twice.)
- Check if an empty exception is added, or error occurs.
Set breakpoint at OfficeHoursSeason~label() and OfficeHoursSeasonHeader~process().
@see https://www.drupal.org/project/office_hours/issues/3399054

You can provoke the error by switching between the following return values in OfficeHoursSeason~label():
- return $this->t($this->name);
- return t($this->name);
- return $this->name;
The first one will generate the error.

(IMO in previous Drupal versions, bootstrap::t() and $this->t() return different classes. I might be mistaken, because in my current D10.2 dev version, both return TranslatableMarkup, only created with different parameters. I never understood why there was/is a difference. )

johnv’s picture

StatusFileSize
new4.35 KB

@alexpott, please ignore the file in #43. It is too long. See newly attached file.

alfthecat’s picture

Having the same issue on 10.3.9, on image fields in my case.

I tried the patch from #39 but it doesn't apply:

sudo -u www-data patch -p1 < core_2987548-39_serialize.patch
patch unexpectedly ends in middle of line
patch: **** Only garbage was found in the patch input.
pearls’s picture

Tested.
Interestingly enough, the 3-year patch (#15) worked for Drupal11 (v 11.1.5)
#39 and #44 didn't apply.

This error is now gone and after patch #15 I can delete referenced entities without errors

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1297 of /home/pearls/public_html/d11site/web/core/lib/Drupal/Core/Database/Connection.php)
hommesreponse’s picture

@samlerner Did you ever get this figured out? I have the Agreement module installed also and have tried #15 with no success. #39 also gave me this when trying to apply:

sudo -u www-data patch -p1 < core_2987548-39_serialize.patch
patch unexpectedly ends in middle of line
patch: **** Only garbage was found in the patch input. 
samlerner’s picture

@hommesreponse I'm not entirely sure how, but the problem seems to be gone. I can't find those errors in our logs for the last 3 months, and I applied the patch in #15 back in 2023. We're on Drupal 10.4.6, maybe the problem was fixed elsewhere?

rakesh.gectcr’s picture

StatusFileSize
new738 bytes

I'm still encountering the following error on the 11.x branch:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1309 of /var/www/html/core/lib/Drupal/Core/Database/Connection.php).

I've also re-rolled the patch from #15 to apply cleanly against the 11.x branch.

lazzyvn’s picture

@rakesh.gectcr thanks you save my day.
I confirm patch #49 fixed this issue

orkutmuratyilmaz’s picture

Status: Needs work » Reviewed & tested by the community

@rakesh.gectcr, thanks for the patch. I can confirm that, patch #49 can fix this issue too.

jrochate’s picture

I'm having the same problem on Drupal 11.2 and #49 solved it.

Anyway, the solution doesn't look definitive.

alexpott’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs steps to reproduce

I've managed to reproduce this issue reliably with Drupal 11.2.0.

  1. Install standard Drupal in French
  2. Edit the admin/content view
  3. Change a field name for example the actions field

You will get an ajax error and if you check the logs you will see:

 46   21/Jun 20:34   php      Erreur        LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database
                                             connection. Adjust your code s

alexpott’s picture

Priority: Major » Critical

Now that core itself can trigger this bug it is a critical.

We just need a test of serializing \Drupal\locale\LocaleTranslation ... will work on adding that.

alexpott’s picture

Issue tags: +11.2.0 update

Tagging because this now covers a regression in 11.2.0

alexpott’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

I've added a test - well updated the existing \Drupal\Tests\locale\Kernel\LocaleTranslationTest::testSerializable() to test this problem. We're unlucky #3128389: LocaleTranslation is not serializable did not catch this.

alexpott’s picture

xjm’s picture

Test-only pipeline indeed throws the exception folks have been reporting all this time:

There was 1 error:
1) Drupal\Tests\locale\Kernel\LocaleTranslationTest::testSerializable
LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution.
/builds/issue/drupal-2987548/core/lib/Drupal/Core/Database/Connection.php:1309
/builds/issue/drupal-2987548/core/modules/locale/tests/src/Kernel/LocaleTranslationTest.php:49
ERRORS!

alexpott credited alezu.

alexpott’s picture

Crediting @alezu for #3531210: "The database connection is not serializable" error on views configuration save as this duplicate help find a way to reproduce this issue.

xjm’s picture

It's really great to have identified the actual root cause of this after so much time -- thanks @alexpott!

Could some of the folks encountering the latest version of this in production on 11.2.0 manually test and confirm that the above fixes the issue? If it's helpful, here's a patch version of the merge request:
https://git.drupalcode.org/project/drupal/-/merge_requests/12440.diff

xjm’s picture

Hiding outdated patch files for clarity.

jrochate’s picture

Hi @xjm.

Like reported above, I had this error appearing and needed to patch with #49.

But now I have removed the patch and applied the MR!12440 and it works as expected.
I'm not having the error on the operations I was having before.

Thanks you @all for the effort is solving this.

xjm’s picture

Status: Needs review » Reviewed & tested by the community

OK great, thanks @jrochate for testing!

nicxvan’s picture

Ran test only to confirm and it fails with a relevant error.

gillesbailleux’s picture

Do core maintainers plan a D11.2.1 release in a near future?

alexpott’s picture

@gillesbailleux yes we are planning to do a release once this makes it to 11.2.x and a couple of other critical regressions we've found.

  • larowlan committed 72642535 on 11.2.x
    Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm...

  • larowlan committed 743f5aa2 on 11.x
    Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm...
larowlan’s picture

Component: forms system » locale.module
Status: Reviewed & tested by the community » Fixed

Committed to 11.x and backported to 11.2.x

Thanks folks.

alexpott’s picture

Version: 11.x-dev » 10.5.x-dev
Status: Fixed » Patch (to be ported)

Given this is an old bug and can easily affect contrib and is not that often tested for (ie. test coverage of module not in the default language is probably pretty thing on the ground) - I think we should backport this fix to 10.5.x and 10.6.x

alexpott’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

The cherry pick to 10.5.x is clean and the test passes.

  • catch committed efd43372 on 10.5.x authored by larowlan
    Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm...

  • catch committed 8978c893 on 10.6.x authored by larowlan
    Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Yeah makes sense to backport this one to 10.5.x too. Cherry-picked to both 10.x branches.

arturopanetta’s picture

Issue summary: View changes

After upgrading to Drupal 11.2.0 Views returns the following error in the browser console:

"\nSi è verificato un errore HTTP in AJAX.\nCodice HTTP di risposta: 500\nDebugging information follows.\nPercorso: /admin/structure/views/ajax/display/tassonomia_ambito/block_1/title\nStatusText: error\nResponseText: The website encountered an unexpected error. Try again later."

Also, in the php logs I see this error:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1309 of /opt/drupal/web/core/lib/Drupal/Core/Database/Connection.php).

I have tested several patches, the #72 (https://git.drupalcode.org/project/drupal/-/merge_requests/12440.patch) works, views does not return any more error.

alexpott’s picture

For some reason #78 made changes to the issue summary so I reverted it back to the state before that comment.

Status: Fixed » Closed (fixed)

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