chapter-three / next-drupal

Next.js for Drupal has everything you need to build a next-generation front-end for your Drupal site: SSG, SSR, and ISR, Multi-site, Authentication, Webforms, Search API, I18n and Preview mode (works with JSON:API and GraphQL).
https://next-drupal.org
MIT License
624 stars 172 forks source link

getPathsFromContext() with multiple types prone to deadlocks #93

Open lauriii opened 2 years ago

lauriii commented 2 years ago

I'm getting following exception fairly consistently when visiting any of the node pages:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 5 database is locked: INSERT INTO "oauth2_token" ("bundle", "uuid", "auth_user_id", "client", "value", "created", "changed", "expire", "status") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); Array ( [0] => access_token [1] => ? [2] => ? [3] => ? [4] => ? [5] => ? [6] => ? [7] => ? [8] => ? ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems() (line 940 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

This seems to be caused by how getPathsFromContext() is calling getResourceCollection() multiple times when multiple types are provided, leading into multiple calls for buildHeaders(). I haven't been able to reproduce this when calling getPathsFromContext() with a single type.

Reporting here instead of Simple OAuth since I haven't had a chance to research the root cause for this.

shadcn commented 2 years ago

Is this resulting into multiple tokens creating when running yarn dev?

I've been looking for a solution for a while now. We implemented an in-memory cache + a larger value for the token expiry. This seems to be working for production.

On dev/build however, Next runs getStaticPaths and getStaticProps on different workers. The memory cache solution does not work here.

I'll continue looking into this and see if we can refactor the getPathsFromContext/getResourceCollection calls.

shadcn commented 2 years ago

Just adding some thoughts here...

I'm wondering if we even need authenticated requests for getStaticPaths? Static pages are always non-authenticated.

Authenticated calls are used for rendering unpublished content and revisions in the preview which is a server-side call in getStaticProps with fallback: blocking.

So do we need to make auth'd call for getStaticPaths? 🤔

\cc @robdecker ^^

harumijang commented 2 years ago

Is this resulting into multiple tokens creating when running yarn dev?

I've been running into this error when running yarn build as well

shadcn commented 2 years ago

@harumijang Are you using the DrupalClient? How big is your site (as in how many content types or pages?)

harumijang commented 2 years ago

Using DrupalClient and very small demo site with 5 pages. But I was able to get around the error by reinstalling my backend with mySQL instead of SQlite.

shadcn commented 2 years ago

OK. Good to know. Let me know if you hit this issue again.