Skip to content

fix: prevent concurrent gateway start calls (EADDRINUSE race condition)#1

Open
nyrosveil wants to merge 1 commit intoopenguardrails:mainfrom
nyrosveil:fix/gateway-race-condition
Open

fix: prevent concurrent gateway start calls (EADDRINUSE race condition)#1
nyrosveil wants to merge 1 commit intoopenguardrails:mainfrom
nyrosveil:fix/gateway-race-condition

Conversation

@nyrosveil
Copy link

Summary

This PR fixes a race condition in the GatewayManager that causes EADDRINUSE errors when the MoltGuard plugin is initialized multiple times in quick succession.

Problem

When the plugin is loaded, it sometimes attempts to start the gateway twice simultaneously:

  1. First call: start() begins spawning the process
  2. Second call: start() is called again before the first call sets this.process, causing it to spawn another gateway instance
  3. Result: Error: listen EADDRINUSE: address already in use 127.0.0.1:8900

Solution

Add an isStarting flag to prevent concurrent start() calls:

  • Check isStarting at the beginning of start() and return early if already starting
  • Set isStarting = true before spawning the process
  • Set isStarting = false in the finally block to ensure it resets even on error

Changes

  • Added private isStarting = false to GatewayManager class
  • Added early return check in start() method
  • Added this.isStarting = true before the try block
  • Added this.isStarting = false in the finally block

Testing

The fix has been tested locally and prevents the EADDRINUSE error when the plugin is initialized multiple times.

- Add isStarting flag to prevent multiple start() calls simultaneously
- This fixes EADDRINUSE error when plugin is initialized multiple times
- The gateway was being started twice in quick succession, causing port conflicts
@ThomasLWang
Copy link
Contributor

@nyrosveil Please update moltguard plugin to 6.6.x. The code repo has been moved to https://github.com/openguardrails/openguardrails

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.

2 participants