Skip to content

server.close() is unawaitable - this makes graceful shutdown/restart more difficult #3841

@matthewoates

Description

@matthewoates

Description

Ideally server.close() returns a promise that resolves once the port is resumed.

The starter code when running via tsx --watch <script> or node --watch <script> (basically anything that restarts quickly) will attempt to create an opencode server before the opencode server created by the previous invocation has fully shut down.

Code:

import { createOpencode } from '@opencode-ai/sdk';


async function go() {
  const { client } = await createOpencode();

  const sessionResponse = await client.session.list();
  const sessions = sessionResponse.data;

  console.log(sessions);

  const projectsResponse = await client.project.list();
  console.log(projectsResponse.data);
}

go();
tsx --watch index.ts

Repro steps:

  • run tsx --watch index.ts
  • observe the server is created successfully
  • make a change and save within index.ts, causing the script to restart quickly
  • observe a error: Failed to start server. Is port 4096 in use? error

This feels like a good first issue.

OpenCode version

@opencode-ai/sdk 1.0.16

opencode --version
0.15.31

Steps to reproduce

See above

Screenshot and/or share link

No response

Operating System

Mac OS

Terminal

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions