Skip to content

pending-without-close ​

A class, or a function outside any class, keeps the resolve / reject of pending promises in a collection (pending.set(id, { resolve, reject }), waiting[id] = entry, push, add, also from Promise.withResolvers()) and listens for message (or data) to settle them, but nothing in it handles close, error, exit, end, disconnect or abort.

Why: a reply settles a pending call only if a reply comes. When the other side goes away (the worker thread dies, the socket drops, the port is closed), no reply ever comes, and every caller awaiting one waits forever: a request that never returns, a shutdown that waits for a flush that can't happen, a test that hangs until the runner kills it. An adopter's RPC peer over a worker MessagePort had a close(error) method that rejected every pending call, but nothing called it when the port closed.

ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";

export class PortClosed extends PropagateError {}

type Port = {
  readonly postMessage: (value: unknown) => void;
  readonly on: (event: "message", listener: (value: unknown) => void) => unknown;
};
type Pending = { resolve: (value: unknown) => void; close: () => void };
type Reply = { id: number; value: unknown };

export class RpcPeer {
  private nextId = 1;
  private readonly pending = new Map<number, Pending>();

  constructor(private readonly port: Port, read: (message: unknown) => Reply) {
    port.on("message", (message) => {
      const reply = read(message);
      this.pending.get(reply.id)?.resolve(reply.value);
      this.pending.delete(reply.id);
    });
  }

  call(method: string): Promise<unknown> {
    const id = this.nextId++;
    return new Promise((resolve, reject) => {
      this.pending.set(id, { resolve, close: () => reject(new PortClosed(method)) });
      this.port.postMessage({ id, method });
    });
  }
}

✅ Accepted: the end of the port rejects what is still waiting

✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";

export class PortClosed extends PropagateError {}

type Port = {
  readonly postMessage: (value: unknown) => void;
  readonly on: {
    (event: "message", listener: (value: unknown) => void): unknown;
    (event: "close", listener: () => void): unknown;
  };
};
type Pending = { resolve: (value: unknown) => void; close: () => void };
type Reply = { id: number; value: unknown };

export class RpcPeer {
  private nextId = 1;
  private readonly pending = new Map<number, Pending>();

  constructor(private readonly port: Port, read: (message: unknown) => Reply) {
    port.on("message", (message) => {
      const reply = read(message);
      this.pending.get(reply.id)?.resolve(reply.value);
      this.pending.delete(reply.id);
    });
    port.on("close", () => {
      for (const p of this.pending.values()) p.close();
      this.pending.clear();
    });
  }

  call(method: string): Promise<unknown> {
    const id = this.nextId++;
    return new Promise((resolve, reject) => {
      this.pending.set(id, { resolve, close: () => reject(new PortClosed(method)) });
      this.port.postMessage({ id, method });
    });
  }
}

The calls wait for a PortClosed, a PropagateError: storing the bare reject instead ({ resolve, reject }) lets whoever calls it reject with anything, which no-throw reports.

Which event ends the channel depends on it: close on a MessagePort (Node) and a WebSocket, exit (and error) on a Worker, close / end / error on a socket. A caller-owned AbortSignal works too: reject the pending calls in its abort listener.

What counts ​

  • The store: set, push, add, unshift, or an assignment to x[key], whose value holds a promise executor's resolve or reject, a Promise.withResolvers() result, or a binding made from one (const entry = { resolve, reject }).
  • The replies: x.on("message", ...), once, addListener, addEventListener, prependListener, or x.onmessage = ...; the same with data.
  • The end: any listener for close, error, exit, end, disconnect or abort in the same class (or the same function, outside classes; or at module level), including once(emitter, "close") from node:events and x.onclose = ....

A new Promise whose executor neither settles it nor hands its resolvers on, on some path, is promise-never-settles's: that rule stops where the resolvers are kept, which is where this one starts.

What the rule can't see: it doesn't check that the end listener settles the pending entries, or that it listens on the same object as message; any end listener in the class counts. A store in one class and the message listener in another, a cleanup method that code elsewhere calls on exit (it is still reported: subscribe in the class, or pass a // safe-ts-ignore pending-without-close: <who calls it>), and pending calls kept as callbacks other than a promise's resolvers (callbacks.set(id, done)) aren't followed. A timeout per call is another way out that it doesn't recognise.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra