From ac774bcd70d117b00f40cfb2e36cf93ef8c4ab6a Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 23 Nov 2026 07:09:57 -0500 Subject: [PATCH] refactor(process): share the abortable command-turn wait between GDB and Rizin (#14042) --- src/gdb/GdbSessionManager.ts | 16 ++-------------- src/process/AbortablePromise.ts | 18 +++++++++++++++++- src/rizin/RizinDebugFrames.ts | 12 ------------ src/rizin/RizinDebugSessionManager.ts | 4 ++-- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/gdb/GdbSessionManager.ts b/src/gdb/GdbSessionManager.ts index 29307b15d4d..cde39fbe9f1 100644 --- a/src/gdb/GdbSessionManager.ts +++ b/src/gdb/GdbSessionManager.ts @@ -28,7 +28,7 @@ import { } from "./GdbProviderIdentity.js"; import { persistentDebuggerHostSupported } from "../process/PersistentDebuggerHost.js"; import { HOST_PLATFORM } from "../platform/HostPlatform.js"; -import { AbortedWait, waitForAbortable } from "../process/AbortablePromise.js"; +import { waitForCommandTurn } from "../process/AbortablePromise.js"; import { describeLaunchFailure, launcherFailureOf, @@ -1521,7 +1521,7 @@ export class GdbSessionManager { release = resolve; }); try { - await waitForGdbCommandTurn(previous, signal); + await waitForCommandTurn(previous, signal, "gdb_console"); acquired = true; return await operation(); } finally { @@ -2243,15 +2243,3 @@ export class GdbSessionManager { } } } - -/** Await a serialized command turn, preserving the caller's cancellation reason. */ -const waitForGdbCommandTurn = ( - previous: Promise, - signal?: AbortSignal, -): Promise => { - if (signal === undefined) return previous; - return waitForAbortable(previous, signal).then((outcome) => { - if (outcome instanceof AbortedWait) - throw cancelledBy("gdb_console", outcome); - }); -}; diff --git a/src/process/AbortablePromise.ts b/src/process/AbortablePromise.ts index 960e2d034ad..07e79ce289e 100644 --- a/src/process/AbortablePromise.ts +++ b/src/process/AbortablePromise.ts @@ -1,4 +1,7 @@ -import type { CarriedCancellationReason } from "../domain/analysisErrorCore.js"; +import { + cancelledBy, + type CarriedCancellationReason, +} from "../domain/analysisErrorCore.js"; /** * An abortable wait that the signal won. It carries the signal's reason as @@ -35,3 +38,16 @@ export const waitForAbortable = async ( if (onAbort !== undefined) signal.removeEventListener("abort", onAbort); } }; + +/** + * Await the previous holder of a serialized command turn. A caller abort that + * wins the wait becomes `operation`'s cancellation with the caller's reason. + */ +export const waitForCommandTurn = async ( + previous: Promise, + signal: AbortSignal | undefined, + operation: string, +): Promise => { + const outcome = await waitForAbortable(previous, signal); + if (outcome instanceof AbortedWait) throw cancelledBy(operation, outcome); +}; diff --git a/src/rizin/RizinDebugFrames.ts b/src/rizin/RizinDebugFrames.ts index b01ef1d13c4..2fcceaa80e7 100644 --- a/src/rizin/RizinDebugFrames.ts +++ b/src/rizin/RizinDebugFrames.ts @@ -15,7 +15,6 @@ import type { DebuggerSessionStop } from "../domain/debuggerSessionErrors.js"; import { err, ok, type Result } from "../domain/result.js"; import { utf8HeadExcerpt } from "../domain/utf8HeadExcerpt.js"; import type { FifoQueue } from "../kernel/FifoQueue.js"; -import { AbortedWait, waitForAbortable } from "../process/AbortablePromise.js"; import { debuggerAdmissionRecovery } from "../process/ownedDebuggerSession.js"; import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js"; import { @@ -298,17 +297,6 @@ export const waitForFrame = ( }); }; -export const waitForTurn = ( - previous: Promise, - signal?: AbortSignal, -): Promise => { - if (signal === undefined) return previous; - return waitForAbortable(previous, signal).then((outcome) => { - if (outcome instanceof AbortedWait) - throw cancelledBy("rizin_debug_command", outcome); - }); -}; - /** The caller's reason when it aborted, else the reason an earlier failure carried. */ export const rizinCancellationReason = ( signal: AbortSignal | undefined, diff --git a/src/rizin/RizinDebugSessionManager.ts b/src/rizin/RizinDebugSessionManager.ts index ffd9c79ebde..2919a521eb1 100644 --- a/src/rizin/RizinDebugSessionManager.ts +++ b/src/rizin/RizinDebugSessionManager.ts @@ -28,9 +28,9 @@ import { RizinProcessExitError, waitForCommandFrames, waitForFrame, - waitForTurn, type RizinFrame, } from "./RizinDebugFrames.js"; +import { waitForCommandTurn } from "../process/AbortablePromise.js"; import { persistentDebuggerHostSupported } from "../process/PersistentDebuggerHost.js"; import { randomUUID } from "node:crypto"; import { open, readlink, realpath, stat } from "node:fs/promises"; @@ -724,7 +724,7 @@ export class RizinDebugSessionManager { release = resolve; }); try { - await waitForTurn(previous, signal); + await waitForCommandTurn(previous, signal, "rizin_debug_command"); acquired = true; if (signal?.aborted) return err(cancelledBy("rizin_debug_command", signal));