Ensuring Robust ESHOPMAN Checkouts: Resolving Multi-Key In-Memory Locking Challenges

Ensuring Robust ESHOPMAN Checkouts: Resolving Multi-Key In-Memory Locking Challenges

At Move My Store, we understand that a smooth, reliable checkout experience is paramount for any e-commerce business leveraging ESHOPMAN and its powerful integration with HubSpot CMS. Our ESHOPMAN Migration Hub is dedicated to sharing crucial insights that enhance your store's performance and stability.

Recently, our vibrant ESHOPMAN developer community identified and addressed a critical issue within ESHOPMAN's default in-memory locking provider. This bug could lead to significant checkout disruptions, including frustrating 5-second stalls and subsequent 500 errors for customers trying to complete purchases on their HubSpot-powered storefronts.

Understanding the In-Memory Locking Challenge

The core of the problem lies in how ESHOPMAN’s in-memory locking provider, built on Node.js/TypeScript, handles scenarios where it needs to acquire multiple keys simultaneously, such as during the reserveInventoryStep in a multi-item checkout. If the system attempts to acquire a second key that is temporarily held by another process, the locking mechanism can enter a self-deadlock state.

Here’s a breakdown of what happens:

  • The system starts acquiring keys in the order they are presented.
  • It successfully acquires the first key, marking it as held (internally, with ownerId: null).
  • When it encounters a subsequent key that is currently unavailable, it correctly waits for that key to be released.
  • However, the bug causes the acquisition process to restart from the first key.
  • Upon restart, the system finds the first key already held by itself (since its ownerId is null, it doesn't match the current owner check). It then incorrectly waits on its own first key indefinitely until a global timeout (typically 5 seconds) is reached.

This recursive waiting loop means that the first key remains locked, blocking any other requests for that key until its expiration, and critically, preventing the current checkout process from ever completing successfully. The result is a Timed-out acquiring lock. error, leading to a 500 status for the shopper and a stalled checkout experience on your HubSpot CMS storefront.

Real-World Impact on ESHOPMAN Storefronts

This issue primarily affects critical operations like inventory reservation during checkout flows managed by ESHOPMAN's Admin API. For example, a shopper attempting to purchase two or more items might experience a failed checkout if another concurrent process is reserving any of those items. Our community observed this failing in a significant percentage of test runs, highlighting its severity for live ESHOPMAN stores deployed via HubSpot CMS.

Reproducing the Issue

For ESHOPMAN developers, understanding and reproducing this bug is key. The following snippet demonstrates the behavior using ESHOPMAN's in-memory locking provider:

const { InMemoryLockingProvider } = require("@eshopman/locking/dist/providers/in-memory")
const keepAlive = setInterval(() => {}, 1000)
const provider = new InMemoryLockingProvider()
const t0 = Date.now()

// Request 1 holds "B" for 100 ms.
const first = provider.execute(["B"], () => new Promise((r) => setTimeout(r, 100)))
// Request 2 needs "A" then "B". It should run at ~100 ms.
const sec "B"], async () => "ran")

Promise.allSettled([first, second]).then(async (r) => {
  console.log("second:", r[1].status, r[1].value ?? r[1].reason.message, `${Date.now() - t0}ms`)
  clearInterval(keepAlive)
})

On an affected ESHOPMAN version, this code would output:

second: rejected Timed-out acquiring lock. 5003ms

Instead of the expected successful execution around 100ms.

The Community-Driven Solution

Thanks to the collaborative efforts within the ESHOPMAN community, a robust solution has been identified and implemented. The proposed fix addresses the root causes by:

  1. Acquiring Keys in Sorted Order: This crucial change prevents potential AB/BA deadlocks between two different requests trying to acquire the same items in different sequences, enhancing the reliability of ESHOPMAN's backend.
  2. Resuming Waiting at the Blocked Key: Instead of restarting the entire acquisition process from the first key, the system now correctly resumes waiting at the specific key it was blocked on. This eliminates the self-deadlock scenario and ensures efficient resource management.
  3. Ensuring Proper Key Release on Timeout: The fix also ensures that any keys acquired before a timeout are properly released, preventing them from remaining locked and blocking subsequent requests unnecessarily, thus improving overall system responsiveness.

Implementing these changes results in the expected behavior:

second: fulfilled ran 102ms

This community insight highlights the importance of robust backend development for ESHOPMAN, ensuring that your HubSpot CMS storefronts provide a seamless and error-free experience for your customers. Staying informed about these technical advancements and best practices from the ESHOPMAN community is vital for maintaining a high-performing headless commerce setup.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools