[TS] Call FinalizationRegistry.register with a unregister token
authorMatt Corallo <git@bluematt.me>
Fri, 5 Aug 2022 01:17:13 +0000 (01:17 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 5 Aug 2022 01:56:50 +0000 (01:56 +0000)
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
typescript_strings.py

index 7df10c7dd588790854cb30d424ec2ac5fbf0768c..a8edeaccc4f0592494bf3ef258634c67da2a4c22 100644 (file)
@@ -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) {
index 6eb5a10a186f9fb7cde4d2d3933363d97ccc69ed..fc2b7dde1261bb8cd08f882ea94859cd5d6f3599 100644 (file)
@@ -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");
        }
 }