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

Open
opened 2026-02-16 17:36:08 -05:00 by yindo · 1 comment
Owner

Originally created by @matthewoates on GitHub (Nov 3, 2025).

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

Originally created by @matthewoates on GitHub (Nov 3, 2025). ### 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: ```ts 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(); ``` ```bash 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_
yindo added the bug label 2026-02-16 17:36:08 -05:00
Author
Owner

@matthewoates commented on GitHub (Nov 3, 2025):

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.

@matthewoates commented on GitHub (Nov 3, 2025): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#2539