简体   繁体   中英

Node kill thread but not process

I was wondering if you could terminate just the thread on which the node application is executing but NOT the process.

Now, I know this sounds strange, because node IS single-threaded, BUT I'm working on an .NET application that hosts node in-process. And when I terminate node with process.exit() the whole application gets closed, which is a behavior I don't want. I just want the node thread to get terminated.

Honestly I'm so desperate I even tried, getting a list of all application threads prior to creating the thread on which node is executing, and then another list after it's created, then removing all threads that were present prior to starting node, keeping a reference to the remaining thread, thinking it was the node thread. As you could expect this did not turn out so well.

I'm using EdgeJS to host node, if that makes any difference. It does not have a built in functionality to terminate itself, unfortunately.

Oh and server.close() doesn't work for me, for some reason.

I'm hosting a video streaming server in my node code, if that info can help you in any way.

Thanks for all the help in advance!

The node thread needs to co-operate by clearing everything that is running on the event loop and then it terminates naturally by returning. So just stop listening for requests, close handles etc. You can use process._getActiveRequests and process._getActiveHandles to see what is keeping the event loop alive.

You can also abruptly interrupt the node thread just by calling OS apis but this will leak a lot of garbage until you exit the process so you cannot start/restart node a lot of times before you need to exit the process anyway to free the leaked resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM