From f93b58baad396fa0899fab4ce3bda84b8a3bccb1 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 5 Aug 2022 01:17:13 +0000 Subject: [PATCH] [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. --- ts/test/tests.mts | 18 ++++++++++++++++++ typescript_strings.py | 7 +++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ts/test/tests.mts b/ts/test/tests.mts index 7df10c7d..a8edeacc 100644 --- a/ts/test/tests.mts +++ b/ts/test/tests.mts @@ -275,6 +275,24 @@ tests.push(async () => { return true; }); +tests.push(async () => { + // Test that we can do basic locking of a NetworkGraph + 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]); + const logger = ldk.Logger.new_impl({ + log(record: ldk.Record): void { + if (record.get_level() != ldk.Level.LDKLevel_Gossip) + console.log(record.get_module_path() + ": " + record.get_args()); + } + } as ldk.LoggerInterface); + const network_graph = ldk.NetworkGraph.constructor_new(genesis_hash, logger); + const graph_lock_1 = network_graph.read_only(); + graph_lock_1.free(); + const graph_lock_2 = network_graph.read_only(); + graph_lock_2.free(); + + return true; +}); + async function run_tests(check_leaks: boolean) { var test_runs = []; for (const test of tests) { diff --git a/typescript_strings.py b/typescript_strings.py index 6eb5a10a..fc2b7dde 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -332,7 +332,7 @@ export class CommonBase { protected constructor(ptr: number, free_fn: (ptr: number) => void) { this.ptr = ptr; if (Number.isFinite(ptr) && ptr != 0){ - finalizer.register(this, get_freeer(ptr, free_fn)); + finalizer.register(this, get_freeer(ptr, free_fn), this); } } // In Java, protected means "any subclass can access fields on any other subclass'" @@ -346,7 +346,10 @@ export class CommonBase { } protected static set_null_skip_free(o: CommonBase) { o.ptr = 0; - finalizer.unregister(o); + // @ts-ignore TypeScript is wrong about the returnvalue of unregister here! + const did_unregister: boolean = finalizer.unregister(o); + if (!did_unregister) + throw new Error("FinalizationRegistry unregister should always unregister unless you double-free'd"); } } -- 2.30.2