Files
magpie/internal/plugin/testdata/stuck/index.js
T
ggbdpq 3da58c3dfc fix(plugin): end ordinary RPCs' plugin work when their caller leaves (#4678)
A plugin model list's deadline and an ordinary RPC's cancellation released
only the caller. inTime raced the hook without stopping it, and host.go
marked only gatewayRequest cancellable, so a cancelled usage or providers
call never told the host: the plugin's task kept running, the host's
pending count kept it, and repeated reads left task after task alive past
their callers. Reported with an offline host-script reproduction that
shows abort reaching none of three pending usage tasks and five model
tasks outliving their deadlines.

The cancellation path a fetch and a gateway request already take now
carries ordinary RPCs too:

- host.go marks every call but init cancellable, so a caller whose
  context ends has the host told (the existing callAborting write), the
  way it already tells it of a stopped fetch. init is left alone: a start
  that overruns is stopped whole.
- host.js registers each ordinary RPC's AbortController; an aborted
  RPC's answer, nobody's then, is not sent.
- the handler is handed the signal: usage passes it to the plugin's
  auth.usage hook as its own options, and a model list's hook options
  carry it, with the listWait deadline aborting the same controller, so
  vendor I/O that supports stopping stops at the deadline or when the
  caller leaves, whichever comes first.

Sign-in renewal and its persistence are untouched: fresh() runs before
the hooks, and an aborted RPC's handler still finishes what it saves.

Refs #4678

Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and ported, narrowed: an aborted ordinary RPC is no longer
answered "Aborted" at once and dropped from pending. It stays pending
until its handler ends, as on main. host.go's stop() closes the host's
stdin and then gives a renewing host up to stopRenewing to save the
rotated token; with the early done(), host.js's own `pending === 0 &&
closed` exit ended the process at once under a cancelled call whose
handler was still renewing or saving (keepSignedIn, set, signOut, a
usage hook mid-refresh). The Go side needs no answer: an aborted call
is released once its abort is written. The signal to the usage and
models hooks, and the listWait deadline aborting the models hook, are
kept as the patch had them.
TestCancelledCallKeepsTheHostTillItsHookEnds is new: a usage hook that
ends 600ms after its abort, then Settle() closing the host's stdin; the
hook's end must reach its trace.

Run on mote (darwin arm64, Go 1.26.8, Bun via testenv): go test -tags
nogui -count=1 ./internal/plugin/... ok 102.8s; -run Plugin
./internal/gateway/ ok 52.1s and ./internal/provider/ ok 13.3s.
Broken by hand: host.js and host.go at main, tests kept:
TestOrdinaryCallCancelTellsTheHost FAIL "the host was told of the
cancellation didn't happen", TestCancelledUsageReachesThePlugin FAIL
"the plugin's usage hook was handed a signal didn't happen",
TestModelListDeadlineTellsTheHook FAIL "the models hook was handed a
signal didn't happen". host.js as the patch had it (early answer):
TestCancelledCallKeepsTheHostTillItsHookEnds FAIL "the host left
before the hook ended; trace: {"signal":"live"}...".
Not tried: the reporter's replay.py against the built host, and a real
plugin's usage hook that passes the signal to its fetch. The reporter's
"host lives on after stdin closes with a task pending" row is unchanged
by design: that is the renewal safeguard.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 05:53:23 +08:00

65 lines
2.6 KiB
JavaScript

// A plugin whose vendor never lets it through, as one whose sign-in is
// answered 401 and tried again for ever: with $STUCK_LOAD set its plugin
// function never returns, with $STUCK_MODELS set its models hook never
// does, and with $STUCK_USAGE set its usage hook doesn't either — but not
// before saying what its caller handed it, and whether an abort reaches it,
// to the file $STUCK_TRACE names (#4678).
const never = () => new Promise(() => {})
import fs from "node:fs"
// traceUsage says what the host handed the hook: a signal it can listen to,
// and, later, that the abort arrived, one JSON line each.
function traceUsage(options) {
const signal = options?.signal
const live = signal && typeof signal.addEventListener === "function"
fs.appendFileSync(process.env.STUCK_TRACE, JSON.stringify({ signal: live ? (signal.aborted ? "aborted" : "live") : "none" }) + "\n")
if (live && !signal.aborted) {
signal.addEventListener("abort", () => fs.appendFileSync(process.env.STUCK_TRACE, JSON.stringify({ aborted: true }) + "\n"), { once: true })
}
}
export const StuckPlugin = async () => {
if (process.env.STUCK_LOAD) await never()
return {
config: async (cfg) => {
cfg.provider = cfg.provider ?? {}
cfg.provider.stuckco = {
name: "StuckCo",
npm: "@ai-sdk/openai-compatible",
api: "https://stuck.invalid/v1",
models: { "stuck-1": { name: "Stuck One", limit: { context: 1000, output: 100 } } },
}
},
provider: {
id: "stuckco",
async models(provider, options) {
if (process.env.STUCK_MODELS_TRACE) traceUsage(options)
if (process.env.STUCK_MODELS) await never()
return provider.models
},
},
auth: {
provider: "stuckco",
methods: [{ type: "api", label: "API key" }],
loader: async () => ({ baseURL: "https://stuck.invalid/v1" }),
usage: async (_getAuth, _provider, options) => {
if (process.env.STUCK_USAGE) {
traceUsage(options)
// with $STUCK_USAGE_LINGER set, an abort ends the hook that many
// ms later, as a hook still saving a renewed sign-in would, and it
// says so first
const linger = Number(process.env.STUCK_USAGE_LINGER)
if (linger > 0 && options?.signal) {
await new Promise((ok) => options.signal.addEventListener("abort", () => setTimeout(() => {
fs.appendFileSync(process.env.STUCK_TRACE, JSON.stringify({ finished: true }) + "\n")
ok()
}, linger), { once: true }))
} else await never()
}
return { windows: [] }
},
},
}
}