Skip to content

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

Closed as not planned

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

Activity

  1. matthewoates commented on Nov 3, 2025

    @matthewoates
    Author

    More on this -

    It appears that promises aren't needed in the SIGTERM case (node will wait for all resources to close), but it would still be useful in the case where we want to orchestrate multiple / restart instances of opencode.

  2. Br1an67 commented on Mar 1, 2026

    @Br1an67

    I'd like to work on this. The fix makes close() return a Promise that resolves when the child process has exited, enabling proper graceful shutdown.

    PR: #15597

  3. github-actions commented on May 1, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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