Giter Club home page Giter Club logo

Comments (6)

benoit-germain avatar benoit-germain commented on August 21, 2024

Well, as a matter of fact, as you point out, there is no way for you to obtain the handle of a lane if you did not store it yourself. In fact it relates to an old design decision of Lanes, to make lanes non-transferable, and therefore non-manipulable, from more than one thread. I'm not sure it would be easy to change that while ensuring that no crash will occur if somebody wants to abuse the mechanism.

However, I think that you could achieve the intended result by sending a message through a linda, and have all your lanes terminate gracefully when they catch it.

from lanes.

jjvbsag avatar jjvbsag commented on August 21, 2024

😁 Well, our issue is a bit more complex. I will try to describe.

We have Lua running in an embedded device and using ZeroBraneStudio as IDE with remote debugging. Lua itself is instanced in a persistent Application. Te controlflow is

  • a persistent application which
  • has a thread which
  • executes lua machine

This thread can by remote control be forced to start/stop the Lua machine or to start/stop the machine with remote debugging.

NOW: if the script in the Lua machine creates additional lanes, stopping the Lua machine sometimes refuses to work. Especially when debugging is set to debug a lane and not the main thread. Maybe our approach to stop the luamachine is also wrong.

So the question could be: If you create a lua machine the simple way:

	L=lua_open();
	luaL_loadfile(L,scriptpath);
	lua_pcall(L,0,0,-1)

and the script creates lanes (out of your control) and runs forever, how would you stop the machine?
We do this by inserting a hook

   lua_sethook(L,lstop,LUA_MASKCALL|LUA_MASKRET|LUA_MASKCOUNT,1);

with

static void lstop(lua_State *L, lua_Debug *ar)
{
	luaL_error(L, "!STOPPED!");
}	

but maybe you have some better suggestion?

I also should mentions, this all is with luajit on an ARM system.

from lanes.

benoit-germain avatar benoit-germain commented on August 21, 2024

I think that just issuing an error is not enough to cleanup everything. Did you try forcing a full gc cycle after the pcall? This should cleanup everything created inside your script (besides globals and registry contents of course). Why can't you just close the state? That would gc everything and terminate the lanes.

from lanes.

jjvbsag avatar jjvbsag commented on August 21, 2024

Ok, this was just the short code snip (there are much more resources aside to Lua managed by the application). Full code is

int CLua::Stop(void(*killfunc)(void))
{
	lua_sethook(L,lstop,LUA_MASKCALL|LUA_MASKRET|LUA_MASKCOUNT,1);
	if(killfunc) killfunc();
	_thread->join();
	_thread=0;
	if(L)
	{
		lua_gc(L, LUA_GCCOLLECT, 0);
		lua_close(L);
	}
	L=0;
	return 0;
}

Killfunc normally provides an Event to get Lua-Application out of GetEvent() loop.

from lanes.

benoit-germain avatar benoit-germain commented on August 21, 2024

Since you are closing the state, it should gc all the lanes and forcefully terminate them even if stuck in a blocking linda operation after a suitable timeout. (see shutdown_timeout in the documentation). So I'd say if you wait a little bit, you should see all lane threads disappearing from the process.

from lanes.

benoit-germain avatar benoit-germain commented on August 21, 2024

In fact, I've traced a bit (mainly to remember how it works), and when you lua_close the state, garbage collection invokes selfdestruct_gc, which cleans up everything, gracefully at first, then forcefully. So when lua_close() returns, there should be no lane running. At least that's what I observe with a small test that launches a lane doing a busy-loop, and closing the state before the lane ends (tested with Lua 5.4 on windows 10 64 bits).

from lanes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.