diff --git a/packages/transaction/lib/chain.ts b/packages/transaction/lib/chain.ts index a73f334536cbf25fd8fef225cec8c902e52e7c91..a3f23bcac1944a6277105378086bdf8730fced3b 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 276cb330304f501293f028c2b733f9192bc4542b..ab260318336e03c7fa6fb8de9b9f2931e50198e3 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 ca2e6787dde68cc56ec9d60cd2efa0be23207d02..8ff15f77062f93397bace5a2991eb311c3cba733 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