Conversation
| private async startServerHost(): Promise<number> { | ||
| const port = await this.findAvailablePortHost(); | ||
|
|
||
| if (await this.isServerRunningHost(port)) { | ||
| return port; |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
|
|
||
| this.hostServerProcess.kill(); | ||
| this.hostServerProcess = null; | ||
| throw new Error('Failed to start OpenCode server on host'); |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Enable OpenCode adapter to run directly on the host machine instead of only inside Docker containers. Uses native fetch() for HTTP requests and Bun.spawn() for server management in host mode, while preserving existing container logic via execInContainer. - Reuse host server across sessions (module-level state like container mode) - Properly await process.exited on startup failure to prevent zombies
7d8ec84 to
4412712
Compare
| private async findAvailablePortHost(): Promise<number> { | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| fetch: () => new Response(''), | ||
| }); | ||
| const port = server.port!; | ||
| server.stop(); | ||
| return port; |
There was a problem hiding this comment.
Bug: The findAvailablePortHost function doesn't await server.stop(), creating a race condition where the port may be returned before it's fully released by the OS.
Severity: HIGH
🔍 Detailed Analysis
In findAvailablePortHost, the call to server.stop() is not awaited. Since server.stop() is an asynchronous operation that returns a Promise, the function returns the port number before the temporary server has fully shut down and released the port to the operating system. This creates a race condition. If the subsequent code immediately tries to bind to this port, it may fail with an "address already in use" error, leading to intermittent failures when starting the OpenCode server.
💡 Suggested Fix
The server.stop() call should be awaited to ensure the port is fully released before the function returns. Change server.stop(); to await server.stop(); in the findAvailablePortHost function.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/session-manager/adapters/opencode.ts#L210-L217
Potential issue: In `findAvailablePortHost`, the call to `server.stop()` is not awaited.
Since `server.stop()` is an asynchronous operation that returns a Promise, the function
returns the port number before the temporary server has fully shut down and released the
port to the operating system. This creates a race condition. If the subsequent code
immediately tries to bind to this port, it may fail with an "address already in use"
error, leading to intermittent failures when starting the OpenCode server.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8418176
Summary
fetch()for HTTP requests andBun.spawn()for server management in host modeexecInContainerfor non-host modeChanges
isHostandhostServerProcessproperties to track mode and manage host server lifecyclestartServerHost(),findAvailablePortHost(),isServerRunningHost()createSession(),sendAndStream(),startSSEStream()withif (this.isHost)for native vs container executiondispose()Fixes the "OpenCode adapter does not support host mode" error when connecting through chat to OpenCode on a host machine.