Skip to content

Commit f93b58b

Browse files
committed
[TS] Call FinalizationRegistry.register with a unregister token
We were assuming we could use `unregister` but weren't actually passing an "unregister token", which causes unregister to not function properly.
1 parent 217905a commit f93b58b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

ts/test/tests.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ tests.push(async () => {
275275
return true;
276276
});
277277

278+
tests.push(async () => {
279+
// Test that we can do basic locking of a NetworkGraph
280+
const genesis_hash = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xd6, 0x68, 0x9c, 0x08, 0x5a, 0xe1, 0x65, 0x83, 0x1e, 0x93, 0x4f, 0xf7, 0x63, 0xae, 0x46, 0xa2, 0xa6, 0xc1, 0x72, 0xb3, 0xf1, 0xb6, 0x0a, 0x8c, 0xe2, 0x6f]);
281+
const logger = ldk.Logger.new_impl({
282+
log(record: ldk.Record): void {
283+
if (record.get_level() != ldk.Level.LDKLevel_Gossip)
284+
console.log(record.get_module_path() + ": " + record.get_args());
285+
}
286+
} as ldk.LoggerInterface);
287+
const network_graph = ldk.NetworkGraph.constructor_new(genesis_hash, logger);
288+
const graph_lock_1 = network_graph.read_only();
289+
graph_lock_1.free();
290+
const graph_lock_2 = network_graph.read_only();
291+
graph_lock_2.free();
292+
293+
return true;
294+
});
295+
278296
async function run_tests(check_leaks: boolean) {
279297
var test_runs = [];
280298
for (const test of tests) {

typescript_strings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
332332
protected constructor(ptr: number, free_fn: (ptr: number) => void) {
333333
this.ptr = ptr;
334334
if (Number.isFinite(ptr) && ptr != 0){
335-
finalizer.register(this, get_freeer(ptr, free_fn));
335+
finalizer.register(this, get_freeer(ptr, free_fn), this);
336336
}
337337
}
338338
// In Java, protected means "any subclass can access fields on any other subclass'"
@@ -346,7 +346,10 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
346346
}
347347
protected static set_null_skip_free(o: CommonBase) {
348348
o.ptr = 0;
349-
finalizer.unregister(o);
349+
// @ts-ignore TypeScript is wrong about the returnvalue of unregister here!
350+
const did_unregister: boolean = finalizer.unregister(o);
351+
if (!did_unregister)
352+
throw new Error("FinalizationRegistry unregister should always unregister unless you double-free'd");
350353
}
351354
}
352355

0 commit comments

Comments
 (0)