-
Notifications
You must be signed in to change notification settings - Fork 392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node process does not exit after subscribing to a Kafka topic #222
Comments
The subscription itself shouldn't be the thing keeping the process alive, as it doesn't actually open any worker threads to do its work. It is likely the act of consuming that does. What method of consuming are you using? Could you show me a sample and potentially your shutdown handler so I could see the sequence of events? |
I ran into this issue today: For example: Consumer
producer
The consumer in this scenario throws the error but does not kill the process. However, in the stream form of the consumer:
It does! I've been digging into it and I can't see a reason for why this is happening at the library level. The only difference is that the stream calls the binding method "consume" while the standard api calls "consumeloop". I need to go pick up my kid but I'll try and dig a little more. My hunch is that this is from the bindings somewhere. |
Been doing some deep diving. Addons are fairly new to me but from running this in debugger, It looks like the while loop in the C++ libraries for the non-stream consumer keeps executing although the main JS thread failed. |
So, I've been digging around all day out of curiosity. The issue is definitely that the KafkaConsumerConsumeLoop keeps executing even after the main thread throws an error. This is because it is not listening for any errors from JS. I toyed around with this:
And it does indeed catch from main JS thread. However, I have no idea what to do after. Ideally I would somehow let the execute method know that there was an error and to stop the while loop. But I'm not sure how to do that. Another way is to force another tick of the event loop, but again, that is a bit outside my comfort zone. These guys for example created a helper function that forces the event loop to tick: This is a thread I found this at: nodejs/nan#668 It might not be too useful because the new version of nan fixes the issue they refer to in here but its similar in that the worker thread does not deal with JS exceptions. The reason why the stream consumer exits is because in the JS side the event loop keeps ticking. For the moment, I'm doing this:
Which is not really optimal as it will require management if an app uses a lot of clients. Anyways, I hope you find this useful and I hope it leads you the right way and are able to figure out the best way to fix this and I look forward to learning from it! |
Stuff like this makes me want to remove the consume loop entirely :( But thanks for the thorough investigation! I will play around with this and try to figure out an optimal solution :) |
@webmakersteve Thanks for your response. Yes, it is a bit of a headache. My C++ experience is limited and I am as green as they come to the Node Addon API but I learned quite a bit by debugging this. I'm thinking for the short-term to just use the streams API. From the playing around I did, it looks like the streams API kind of does the same thing that the flow-mode API does (never ending loop) but makes those loops in v8 rather than outside of it. I Googled for hours but I couldn't find a solution. I just need SOMETHING to tell me if the v8 thread is still alive and well in the execute method. Thats all I want but doesn't seem to exist since execute method only deals with the current thread(the loop) and can't peek into v8 threads from there. |
This issue so painful :( |
This should be fixed with |
Hello,
I am trying to troubleshoot an issue around our node.js applications not exiting gracefully.
I have an app which executes some code then terminates, but I find that the node process continues to stay alive after subscribing to a Kafka topic with node-rdkafka.
I notice that this is also the case (node process continues to live) when there is an uncaught error, where usually the node process terminates.
Is there a way to subscribe to a topic without this behaviour?
Thanks in advance.
The text was updated successfully, but these errors were encountered: