fix(javascript): match resource release methods to the acquired handle (#7773)

* fix(javascript): match resource release methods to the acquired handle

Every acquisition family admitted every release method, so `h.close()`,
`h.destroy()` and `h.end()` each resolved as a complete release of whatever the
binding held. Node exposes a different subset per acquisition, and the rejected
pairs throw at the call site instead.

Measured on Node 24.22.1:

| acquisition | value | release members |
| --- | --- | --- |
| `fs.openSync` | number | none |
| `fs.open` (callback) | undefined | none |
| `fs.createReadStream` | ReadStream | `close`, `destroy` (`end` is the byte-offset field) |
| `fs.createWriteStream` | WriteStream | `close`, `destroy`, `end` |
| `fs/promises.open` | FileHandle | `close` |
| `net.connect` | Socket | `destroy`, `end` |
| `tls.connect` | TLSSocket | `destroy`, `end` |

So `fs.openSync(...)` + `fd.close()`, `createReadStream(...)` + `.end()`,
`net.connect(...)` + `.close()` and the FileHandle cases emitted a `releases` /
`resolved` fact for a call that throws.

Carry the specifier with each acquisition candidate, list the release methods
each pair actually has, and emit no release when the pair is not one of them. A
release stays `complete` only when the binding held exactly one acquisition and
that acquisition admitted the method.

`tls.createConnection` is not a Node export at all: `'createConnection' in
require('node:tls')` is false and the ESM import does not link. Drop it from the
table so a `tls.createConnection` call yields no acquisition.

* chore: retrigger title and readiness checks after the title was corrected

* fix(javascript): cover a mixed-family binding before completing a release

greptile asked for the mixed-family case: a binding that can hold either a
FileHandle or a socket, then calls close(). The FileHandle is the only linked
acquisition and the release stays partial.

That case also showed the `candidates.length === 1` guard was dead: an
acquisition of another family is not in `linked`, so `holdsOnlyAcquisitions`
already returns false for it and the release already stayed partial without the
guard. Removing the guard and keeping the test means the behaviour is now pinned
by a test rather than by two overlapping conditions.
This commit is contained in:
吴佳浩Alben
2026-10-29 08:23:53 +08:00
committed by GitHub
parent a965cbab06
commit b7442c1958
3 changed files with 196 additions and 8 deletions
@@ -3,9 +3,32 @@ export const BUILTIN_RESOURCE_METHODS = {
fs: ["open", "openSync", "createReadStream", "createWriteStream"],
"fs/promises": ["open"],
net: ["connect", "createConnection"],
tls: ["connect", "createConnection"],
tls: ["connect"],
} as const;
/**
* Release methods the value Node returns from each acquisition actually has.
*
* `fs.openSync` returns a numeric descriptor and `fs.open` in its callback form
* returns `undefined`, so neither has a release method at all; the descriptor is
* released through `fs.closeSync`/`fs.close`, which is not a member call. A
* `fs.ReadStream` exposes `end` as its byte-offset field, not a method. A
* `FileHandle` and a `net.Socket`/`TLSSocket` each expose only their own subset,
* so a family-blind release list claims a teardown that throws instead.
*/
export const BUILTIN_RESOURCE_RELEASE_METHODS = {
"fs:open": [],
"fs:openSync": [],
"fs:createReadStream": ["close", "destroy"],
"fs:createWriteStream": ["close", "destroy", "end"],
"fs/promises:open": ["close"],
"net:connect": ["destroy", "end"],
"net:createConnection": ["destroy", "end"],
"tls:connect": ["destroy", "end"],
} as const satisfies Readonly<
Record<string, readonly ("close" | "destroy" | "end")[]>
>;
export const BUILTIN_RESOURCE_SPECIFIERS = Object.keys(
BUILTIN_RESOURCE_METHODS,
);