unreleased-resource
A resource a function acquires (a file handle, an interval or a timeout, a listening server, a worker, an observer, an AbortController wired to listeners, an object URL, a stream reader's lock, a temporary directory) and doesn't release on some way out of the function: a return, the end of the body, an await in between that rejects, or a call in between that throws. On by default, as a warning.
Why: a resource held past its function keeps working for no one: the interval keeps firing, the listeners stay registered, the port stays taken, the file descriptor stays open, and most of them keep the process alive. The path that forgets is rarely the one you test: an early return, or the await that rejects when the disk or the network fails. The fix is the shape Rust's Drop gives for free: release it in a finally (or declare it with await using where it is disposable), so every way out runs the release.
❌ Rejected: when fileHandleStat fails, q returns the error and the handle stays open
✕ Rejectedunreleased-resource
ts
import { close, fileHandleStat, open } from "@evobug_com/safe-ts-feature-node/fs/promises";
import { q, type Result } from "@evobug_com/safe-ts-std/result";
export async function size(path: string): Promise<Result<number, Error>> {
const handle = q(await open(path, "r"));
const stat = q(await fileHandleStat(handle));
q(await close(handle));
return stat.size;
}ts
import { close, fileHandleStat, open } from "@evobug_com/safe-ts-feature-node/fs/promises";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { q, type Result } from "@evobug_com/safe-ts-std/result";
export async function size(path: string): Promise<Result<number, Error>> {
const __q1 = await open(path, "r"); if (__q1 instanceof __qErr) return __q1; const handle = __q1;
const __q2 = await fileHandleStat(handle); if (__q2 instanceof __qErr) return __q2; const stat = __q2;
{ const __q3 = await close(handle); if (__q3 instanceof __qErr) return __q3; __q3; }
return stat.size;
}✅ Accepted: await using closes it on every way out
✓ Accepted0 diagnostics
ts
import { fileHandleStat, open } from "@evobug_com/safe-ts-feature-node/fs/promises";
import { q, type Result } from "@evobug_com/safe-ts-std/result";
export async function size(path: string): Promise<Result<number, Error>> {
await using handle = q(await open(path, "r"));
const stat = q(await fileHandleStat(handle));
return stat.size;
}ts
import { fileHandleStat, open } from "@evobug_com/safe-ts-feature-node/fs/promises";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { q, type Result } from "@evobug_com/safe-ts-std/result";
export async function size(path: string): Promise<Result<number, Error>> {
const __q1 = await open(path, "r"); if (__q1 instanceof __qErr) return __q1; await using handle = __q1;
const __q2 = await fileHandleStat(handle); if (__q2 instanceof __qErr) return __q2; const stat = __q2;
return stat.size;
}The packs' safe versions stand for what they wrap: open from the Node pack acquires a file handle, close(handle) releases it, fileHandleStat(handle) is a method call on it. q(...) is an early return (the transform writes it as one), and so is if (isErr(stat)) return stat;.
using and await using pass tsc strict at the ES2024 target the adoption guide sets (TypeScript writes them out as try / finally where the target lacks them), the SAFE-TS transform leaves them as written, and Node runs them. Only a value with [Symbol.dispose]() or [Symbol.asyncDispose]() can be declared so: a Node FileHandle can (the Node pack's map says so, and its test closes one that way), a browser's interval id (a number) can't. Elsewhere, release it in a finally:
❌ Rejected: the early return leaves the interval running
✕ Rejectedunreleased-resource
ts
export function poll(tick: () => void, done: () => boolean): void {
const id = setInterval(tick, 100);
if (done()) return;
tick();
clearInterval(id);
}✓ Accepted0 diagnostics
ts
export function poll(tick: () => void, done: () => boolean): void {
const id = setInterval(tick, 100);
try {
if (done()) return;
tick();
} finally {
clearInterval(id);
}
}The quick fix Release it in a finally writes this when the release is a statement of the same block as the declaration and nothing declared between them is used after it.
A call that throws between the two is a way out too, also where a caller catches it: the caller goes on, and the interval keeps running.
❌ Rejected: checkRange throws a PropagateError, run catches it, the interval is never cleared
✕ Rejectedunreleased-resource
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class OutOfRange extends PropagateError {}
function checkRange(n: number): void {
if (n < 0) throw new OutOfRange("negative");
}
function sample(n: number): void {
const id = setInterval(() => console.log("sampling"), 1000);
checkRange(n);
clearInterval(id);
}
export function run(n: number): number {
try {
sample(n);
return 1;
} catch (error) {
if (error instanceof OutOfRange) return 0;
throw error;
}
}✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class OutOfRange extends PropagateError {}
function checkRange(n: number): void {
if (n < 0) throw new OutOfRange("negative");
}
function sample(n: number): void {
const id = setInterval(() => console.log("sampling"), 1000);
try {
checkRange(n);
} finally {
clearInterval(id);
}
}
export function run(n: number): number {
try {
sample(n);
return 1;
} catch (error) {
if (error instanceof OutOfRange) return 0;
throw error;
}
}What counts
Acquired is a value bound with
constorletin the function from an operation the API maps list:setInterval,setTimeout,new AbortController()once itssignalis in the options of anaddEventListener,new BroadcastChannel,URL.createObjectURL,stream.getReader(),new WebSocket, and in the browsernew EventSource,new Worker, and the observers once theyobserve(); in Nodeawait open(...),await mkdtemp(...),createServer()once itlisten()s,new Workerofnode:worker_threads,createInterfaceofnode:readline. Which operations acquire, arm and release is data in the packs' maps (format), each tested on the running engine, so a pack of your own can declare its library's.Every way out: each
return, the end of the body, aq(...)that returns the error (std'squnder whatever name the file imports it,q as ask), and an exception:- from an
awaitoutside atrywhen what it awaits can fail: a function of the project that propagates a failure, a library's member a pack (or automatic mode) marks as failing, or something the checker can't look up (a promise held in a property). Anawaitof a function that returns aResult, of one that propagates nothing, of a pack's safe version, of std's helpers or ofPromise.resolve(...)can't reject, and isn't a way out; - from a call (or a
new) when it can throw where it is: a function of the project that throws or propagates aPropagateError(itself, a function it calls, aThrows/Throwingslot its type declares), a built-in that throws (JSON.parse(text),new URL(text), unless the types written show it can't), a library's member a pack marks as throwing. The callee is found as every rule finds it: in another file, through a namespace import, a re-export or a default export, a method through its receiver (a parameter, a local made withnewor made in place, a field, a field of a parameter, an element, what a function returns), a static method. A call that runs a callback right away (list.forEach((x) => check(x)),list.map(parse)) lets out what the callback throws. Whether a caller catches it doesn't matter: the function leaves without the release. A call that can't fail, a call of std's, and a call the checker can't resolve (a function value with no written type, a built-in method on a receiver of no known class) aren't ways out. Inside atry, such a call goes to thecatch: acatchthat returns before the release is reported, naming the call.
Whether a call can fail is a whole-program fact: the file's own analysis (the editor's too) lists the
awaits and calls between the acquisition and the release, and the whole-program pass keeps the finding when one of them can fail, naming the first.- from an
Released is the release the maps name (
clearInterval(id),handle.close(),observer.disconnect(),controller.abort(), ...), or the value leaving the function's hands: returned, stored (a field, a property, a list, another binding), handed to any call, or used by a function written inside this one (a callback, a cleanup): that function holds it now.Meant to live as long as the program: an interval started by a worker's main function, a server, a page's
AbortControllerare often never released on purpose. Those (every resource the maps marklifelong) are reported only in a function that releases them on some path: the function shows it means to, and misses a way out.
How it relates to the other lifetime rules
listener-cleanupreports a listener that can't be removed and an interval whose id is thrown away; this rule starts where the id or the controller is kept, and asks whether every way out releases it.unbounded-pollis about a loop that waits for state with no way out,fetch-signalabout a request nothing can stop, andpending-without-closeabout resolvers nobody settles when a channel closes: none of them reports a resource this rule reports.- The Vue and React cleanup rules are about a component's lifetime: a timer set up in
onMountedand cleared inonUnmounted, or returned from an effect's cleanup, is used by a function written inside, which this rule takes as handing it on.
What the rule can't see
- Ownership handed on: a value returned, stored, passed on or captured by a callback is taken to be released by whoever holds it. A callback that never releases it (an
exithandler that settles a promise but doesn't clear the interval polling for the same thing) isn't followed. - A resource the maps don't name, one reached other than through a binding of the function (
fs.promises.open(...),this.timer = setInterval(...)), one acquired at module level (it lives as long as the program), or assigned again. - A promise it can't look up. An
awaitof a value held in a property or a parameter (await job.done) is taken to reject; one kept in aconstof the function first is judged by the call that made it.