From 583ea5ba9ece025fedf18fe25fcddd3fcead395c Mon Sep 17 00:00:00 2001 From: Gandalf Date: Sun, 7 Dec 2025 21:23:40 +0100 Subject: [PATCH] Add p-limit to serialize WASM chain access Prevent WASM memory corruption from concurrent calls to the shared wax chain instance. WASM modules are not thread-safe, and concurrent access from Next.js Server Components can cause "memory access out of bounds" errors. Uses p-limit(1) to serialize all getChain() calls, ensuring only one WASM operation runs at a time. --- packages/transaction/lib/chain.ts | 13 +++++++++++-- packages/transaction/package.json | 1 + pnpm-lock.yaml | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/transaction/lib/chain.ts b/packages/transaction/lib/chain.ts index a73f33453..a3f23bcac 100644 --- a/packages/transaction/lib/chain.ts +++ b/packages/transaction/lib/chain.ts @@ -1,10 +1,19 @@ import type { ExtendedNodeApi, ExtendedRestApi } from './extended-hive.chain'; import { hiveChainService } from './hive-chain-service'; import { TWaxExtended, TWaxRestExtended } from '@hiveio/wax'; +import pLimit from 'p-limit'; let chain: TWaxExtended> | undefined = undefined; +// Serialize access to wax chain instance to prevent WASM memory corruption +// from concurrent calls. WASM modules are not thread-safe, and concurrent +// access (e.g., from Next.js Server Components) can cause "memory access +// out of bounds" errors. +const wasmLock = pLimit(1); + export async function getChain() { - if (!chain) chain = await hiveChainService.getHiveChain(); - return chain; + return wasmLock(async () => { + if (!chain) chain = await hiveChainService.getHiveChain(); + return chain; + }); } diff --git a/packages/transaction/package.json b/packages/transaction/package.json index 276cb3303..ab2603183 100644 --- a/packages/transaction/package.json +++ b/packages/transaction/package.json @@ -10,6 +10,7 @@ "@hive/smart-signer": "workspace:*", "@hiveio/wax": "catalog:hiveio-toolset", "@hiveio/workerbee": "catalog:hiveio-toolset", + "p-limit": "^3.1.0", "secure-random": "^1.1.2", "set-interval-async": "^3.0.3", "speakingurl": "^14.0.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca2e6787d..8ff15f770 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -789,6 +789,9 @@ importers: '@hiveio/workerbee': specifier: catalog:hiveio-toolset version: 1.28.4-rc0 + p-limit: + specifier: ^3.1.0 + version: 3.1.0 secure-random: specifier: ^1.1.2 version: 1.1.2 -- GitLab