Unhandled rejection when a call fails before delivery and an argument is an imported promise

オープン 初心者向け
#293 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
81/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript

調査の方向性

activePull が作成される ImportTableEntry のコンストラクターから src/rpc.ts を確認し、RpcPayload.dispose() がインポートされた promise 引数をどのように処理するかを読んでください。レポートでは activePull.promise に no-op の catch を付けることが提案されています。まず、同梱された Vitest の再現コードで確認してください。失敗した呼び出しの両方のケースが予想どおり reject され、未処理の rejection が発生しなければ完了です。

索引モデルが issue の本文から書いたものです。

説明

This issue was found using AI tooling and confirmed on capnweb 0.12.0.

ImportTableEntry's constructor creates this.activePull = Promise.withResolvers(), and nothing awaits it. If the call then fails before it is delivered, RpcPayload.dispose() disposes the argument, which calls activePull.reject(...). The result is an unhandled rejection ("RPC was canceled because the RpcPromise was disposed."), which kills a Node server. Repro: start sessions A and B over MessagePorts. On A's client, call stubFromA.nope(sessionB.someCall()). Suggested fix: attach a no-op .catch to activePull.promise when it is created.

Repro:

import { newMessagePortRpcSession, RpcTarget } from "capnweb";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

// A call that fails before reaching the target (not a function, missing
// method) disposes its arguments. When an argument is a promise the caller
// is still resolving, that dispose must not leave an unhandled rejection
// behind: in a server process it would crash every session.

class HostObject extends RpcTarget {
	hello() {
		return "hi";
	}
}

class Root extends RpcTarget {
	object() {
		return new HostObject();
	}
}

function connect() {
	const { port1, port2 } = new MessageChannel();
	newMessagePortRpcSession(port1, new Root());
	return newMessagePortRpcSession<Root>(port2);
}

describe("capnweb failed calls with promise arguments", () => {
	let unhandled: unknown[];
	const onUnhandled = (reason: unknown) => unhandled.push(reason);

	beforeEach(() => {
		unhandled = [];
		process.on("unhandledRejection", onUnhandled);
	});
	afterEach(() => {
		process.off("unhandledRejection", onUnhandled);
	});

	async function settle() {
		await new Promise((resolve) => setTimeout(resolve, 50));
	}

	it("rejects calling a host object as a function", async () => {
		const client = connect();
		const other = connect();
		const object = client.object() as unknown as (arg: unknown) => Promise<unknown>;
		await expect(object(other.object())).rejects.toThrow("is not a function");
		await settle();
		expect(unhandled).toEqual([]);
	});

	it("rejects calling a missing method", async () => {
		const client = connect();
		const other = connect();
		const object = client.object() as unknown as { nope(arg: unknown): Promise<unknown> };
		await expect(object.nope(other.object())).rejects.toThrow("'nope' is not a function");
		await settle();
		expect(unhandled).toEqual([]);
	});
});
主要言語
TypeScript
スター
4k
フォーク
151
平均マージ
16時間 46分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudflare/capnweb のほかの issue

cloudflare/capnweb の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。