Skip to content

fix: Track initialization state more robustly in NodeBridge.init() #137

@bbopen

Description

@bbopen

Problem

As identified in PR #136 code review, there's an edge case where init() can silently succeed after a partial failure.

Scenario:

  1. doInit() spawns workers successfully
  2. refreshBridgeInfo() fails (e.g., network timeout)
  3. On retry, init() sees processPool.length >= minProcesses and returns early
  4. Bridge appears initialized but bridgeInfo is undefined

Current Behavior

async init(): Promise<void> {
  if (this.disposed) {
    throw new BridgeDisposedError('Bridge has been disposed');
  }
  if (this.processPool.length >= this.options.minProcesses) {
    return;  // Early return based only on pool size
  }
  // ...
}

Proposed Fix

Track initialization state explicitly:

private initState: 'pending' | 'initializing' | 'ready' | 'failed' = 'pending';

async init(): Promise<void> {
  if (this.disposed) {
    throw new BridgeDisposedError('Bridge has been disposed');
  }
  if (this.initState === 'ready') {
    return;
  }
  if (this.initState === 'failed') {
    throw new BridgeProtocolError('Bridge initialization previously failed');
  }
  // ...
}

Impact

Low - bridgeInfo is diagnostic-only and workers remain functional even if metadata fetch fails.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions