Skip to content

fix(sf-fin): load Salesforce modules once for parallel calls - #984

Merged
Jaganpro merged 1 commit into
mainfrom
fix/sf-fin-parallel-lazy-import
Nov 23, 2026
Merged

Jaganpro merged 1 commit into
mainfrom
fix/sf-fin-parallel-lazy-import

Conversation

@Jaganpro

Copy link
Copy Markdown
Contributor

When two sf_fin salesforce calls run in one parallel batch (for example readiness and observe), the second one fails:

❌ Salesforce observe failed: Cannot access 'DEFAULT_CONNECTION_TIMEOUT_MS' before initialization

Cause

SF Fin imports the Salesforce connection module lazily so Pi's boot stays free of the Salesforce SDK. Pi loads extensions through jiti. Under jiti, a second import() of a module that is still being evaluated returns the module part-way through, so the second caller's first call read a constant that was not initialized yet. Native Node ESM does not behave this way, so this happens only under Pi's loader.

Fix

lib/lazy-imports.ts: each lazy loader shares one import promise across callers and drops it after a failed load, so the next call retries. service.ts uses these loaders for the connection, org, and token-transfer modules. The boot path is unchanged (check-boot-path passes).

Proof

  • lazy-imports.test.ts loads the module through jiti with Pi's loader options and calls the loaders concurrently. It reproduced the exact error before the fix and passes after it. It uses an org alias that does not exist, so it never touches an org.
  • Live, against a developer org: readiness, observe, and handoff_queue_create run in parallel failed 3/3 before the fix and passed 3/3 after.
  • npm run validate:ci passes.

Two sf_fin salesforce calls in one parallel batch (readiness and
observe) made the second fail with "Cannot access
'DEFAULT_CONNECTION_TIMEOUT_MS' before initialization". SF Fin imports
the Salesforce connection module lazily so boot stays free of the SDK.
Pi loads extensions through jiti, where a second import() of a module
still being evaluated returns it part-way through, so its first call
read a constant that was not initialized yet.

Each lazy loader now shares one import promise across callers and drops
it after a failed load so the next call retries. The test loads the
module through jiti with Pi's loader options and calls the loaders
concurrently; it reproduced the exact error before the fix.
@Jaganpro
Jaganpro merged commit ee7e4ca into main Nov 23, 2026
17 checks passed
@Jaganpro
Jaganpro deleted the fix/sf-fin-parallel-lazy-import branch November 23, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant