Aborting a running vm all friendly like

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
gz
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aborting a running vm all friendly like

gz
Hi,

I have lua 5.3 embedded in a server application, basically responding to requests from other programs. There are multiple threads running lua vm's, and each thread handles one request until it is done. All requests run the same code. I have implemented this by loading the code when the per thread vm is created via lua_loadfile(), and execute it by lua_pushvalue()ing the function created by lua_loadfile() and then executing it using lua_pcall().

Now, the program being serviced by my server may have a request aborted on demand, and for that I need a way to abort a running vm in such a way that afterwards it is still functional, and the function created by lua_loadfile() is still on the stack for the next round of processing. My idea for this was to install a hook, probably a count hook, that checks for a flag to see whether the thread has been aborted, and if it has, just raises an error. This would abort the vm at the earliest convenient opportunity, and also offer the ability to handle te abort from within lua (to do some cleanup or whatever). But I was wondering, is there maybe a better solution for this kind of thing?

Regards,
Gunnar

Es ist keine Schande, vor Angst zu verblöden.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aborting a running vm all friendly like

Oliver Kroth
Hi

I have the same challenge in my scenario, and solved it by using a hook
that raises a "stopped" error on the main thread.
But if there is a global function named "stop", this is called instead
to allow the applet to stop smoothly.

--
Oliver

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aborting a running vm all friendly like

Thomas Jericke
In reply to this post by gz
On 31.07.2017 19:29, Gunnar Zötl wrote:
My idea for this was to install a hook, probably a count hook, that checks for a flag to see whether the thread has been aborted, and if it has, just raises an error. This would abort the vm at the earliest convenient opportunity, and also offer the ability to handle te abort from within lua (to do some cleanup or whatever).

I use exactly this mechanism in my Lua environment. I would say it is a quite good mechanism.
What you might have to think about, do you want errors from your hook to be caught by pcall within a script or not.
Both is possible, you should just use one approach an stick to it. Probably in your scenario you don't want pcall to catch a error from the hook.
--
Thomas
Loading...