alexbosworth / ln-service

Node.js interface to LND
MIT License
318 stars 61 forks source link

Some subscribeTo methods seem to keep a node process from shutting down #174

Closed andreashuber69 closed 1 year ago

andreashuber69 commented 1 year ago
import { subscribeToBlocks } from "lightning";
import { connectLnd } from "./test/connectLnd.js";

const eventName = "block";
const waitTime = 5000;

const authenticatedLnd = await connectLnd();
const emitter = subscribeToBlocks(authenticatedLnd);
const handler = (e) => console.log(e);

emitter.on(eventName, handler);
console.log(`Waiting for ${waitTime / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, waitTime));
console.log("Shutting down...");
emitter.off(eventName, handler);

Playing with lightning 9.8.1, the above code works as expected. That is, I get the following output and the process ends.

Waiting for 5 seconds...
{
  height: 798407,
  id: '00000000000000000004cf4fb091ef41647bd2d2e7657e2bd82a9158b8e165b6'
}
Shutting down...

However, when I use subscribeToForwards instead of subscribeToBlocks and set eventName to forward instead of block, the process hangs after logging Shutting down.... Why?

andreashuber69 commented 1 year ago

Sorry, I meant to open this in https://github.com/alexbosworth/lightning, but I guess it applies to this project too.

alexbosworth commented 1 year ago

i think this is a node.js question? if you have open listeners node.js wouldn't exit right?

andreashuber69 commented 1 year ago

i think this is a node.js question? if you have open listeners node.js wouldn't exit right?

I would agree with you if the behavior was equivalent for subscribeToBlocks and subscribeToForwards. It clearly isn't, everything works as expected for the former but not for the latter.

Also, note that I remove the handler on the last line emitter.off(eventName, handler);

alexbosworth commented 1 year ago

Did you try using emitter.removeAllListeners() ?

andreashuber69 commented 1 year ago

Did you try using emitter.removeAllListeners() ?

Just tried that, the result is the same. Works with blocks, doesn't work with forwards. Here's the CTRL-C, CTRL-V of the code:

import { subscribeToForwards } from "lightning";
import { connectLnd } from "./test/connectLnd.js";

const eventName = "forward";
const waitTime = 5000;

const authenticatedLnd = await connectLnd();
const emitter = subscribeToForwards(authenticatedLnd);
const handler = (e: unknown) => console.log(e);

emitter.on(eventName, handler);
console.log(`Waiting for ${waitTime / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, waitTime));
console.log("Shutting down...");
emitter.removeAllListeners();
alexbosworth commented 1 year ago

Hmm ok I guess it has an issue, can take a look

alexbosworth commented 1 year ago

Should be fixed now, can you try on the latest version?

andreashuber69 commented 1 year ago

import { subscribeToForwards } from "lightning"; import { connectLnd } from "./test/connectLnd.js";

const eventName = "forward"; const waitTime = 5000;

const authenticatedLnd = await connectLnd(); const emitter = subscribeToForwards(authenticatedLnd); const handler = (e: unknown) => console.log(e);

emitter.on(eventName, handler); console.log(Waiting for ${waitTime / 1000} seconds...); await new Promise((resolve) => setTimeout(resolve, waitTime)); console.log("Shutting down..."); emitter.removeAllListeners();

I've tried with subscribeToForwards, subscribeToPayments, subscribeToChannels and they all now work as expected. Thank you!