Co-Routines not dying and resulting in a crash (stack overflow?)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Co-Routines not dying and resulting in a crash (stack overflow?)

Raymond Jacobs
I have noticed a problem when I create a large amount of co-routines from C code.
 
it seems that when i call:  cr=lua_newthread(state);
 
it is pushed onto the stack of 'state' per the documentation.
 
however after I have loaded code and run the co-routine using lua_resume, it's status is still '0', and the thread never gets garbage collected, eventually resulting in a stack overflow.
 
it seems that by manually popping the thread off of the 'state' stack stops this problem, but I was under the impression that after running the co-routine it should be 'dead' and thus garbage collected.
 
do I /really/ need to go back and pop these threads off of the stack after they are done?
 
and if the status always returns 0 how can I know when they are done?
 
Thank you for your time,
 
Raymond Jacobs
Ethereal Darkness Interactive

Reply | Threaded
Open this post in threaded view
|

Re: Co-Routines not dying and resulting in a crash (stack overflow?)

Eric Scouten
Anything that's on an active lua_State cannnot be garbage collected. You'll need to manage the lifetime of this coroutine at least a little bit, by having a reference to it somewhere that goes away upon completion.

-Eric



On 12 Apr 2006, at 10:25, Raymond Jacobs wrote:

I have noticed a problem when I create a large amount of co-routines from C code.
 
it seems that when i call:  cr=lua_newthread(state);
 
it is pushed onto the stack of 'state' per the documentation.
 
however after I have loaded code and run the co-routine using lua_resume, it's status is still '0', and the thread never gets garbage collected, eventually resulting in a stack overflow.
 
it seems that by manually popping the thread off of the 'state' stack stops this problem, but I was under the impression that after running the co-routine it should be 'dead' and thus garbage collected.
 
do I /really/ need to go back and pop these threads off of the stack after they are done?
 
and if the status always returns 0 how can I know when they are done?
 
Thank you for your time,
 
Raymond Jacobs
Ethereal Darkness Interactive


Reply | Threaded
Open this post in threaded view
|

Re: Co-Routines not dying and resulting in a crash (stack overflow?)

Raymond Jacobs
Hi Eric,
 
Thanks for the reply, I am currently solving it by iterating the stack and removing the co-routine when i have decided it is dead, this work but it's a little 'ick', my main issues were that I didint know (and the documentation did not specify) that the co-routines do not die upon completion.
 
it seems the optimal situation would be to increase the reference count of the new thread when i create it, and immediately pop it off the stack (since when i need to get it later the position is not predictable and i need to search) and then later be able to decrement the ref count by simply having the lua_State* that represents the co-routine.
 
any thoughts on that?
 
-Raymond

 
On 4/12/06, Eric Scouten <[hidden email]> wrote:
Anything that's on an active lua_State cannnot be garbage collected. You'll need to manage the lifetime of this coroutine at least a little bit, by having a reference to it somewhere that goes away upon completion.

 
-Eric

 

 

On 12 Apr 2006, at 10:25, Raymond Jacobs wrote:

I have noticed a problem when I create a large amount of co-routines from C code.
 
it seems that when i call:  cr=lua_newthread(state);
 
it is pushed onto the stack of 'state' per the documentation.
 
however after I have loaded code and run the co-routine using lua_resume, it's status is still '0', and the thread never gets garbage collected, eventually resulting in a stack overflow.
 
it seems that by manually popping the thread off of the 'state' stack stops this problem, but I was under the impression that after running the co-routine it should be 'dead' and thus garbage collected.
 
do I /really/ need to go back and pop these threads off of the stack after they are done?
 
and if the status always returns 0 how can I know when they are done?
 
Thank you for your time,
 
Raymond Jacobs
Ethereal Darkness Interactive



Reply | Threaded
Open this post in threaded view
|

Re: Co-Routines not dying and resulting in a crash (stack overflow?)

Eric Scouten
On 12 Apr 2006, at 11:07, Raymond Jacobs wrote:

> Hi Eric,
>
> Thanks for the reply, I am currently solving it by iterating the  
> stack and removing the co-routine when i have decided it is dead,  
> this work but it's a little 'ick', my main issues were that I  
> didint know (and the documentation did not specify) that the co-
> routines do not die upon completion.
>
> it seems the optimal situation would be to increase the reference  
> count of the new thread when i create it, and immediately pop it  
> off the stack (since when i need to get it later the position is  
> not predictable and i need to search) and then later be able to  
> decrement the ref count by simply having the lua_State* that  
> represents the co-routine.
>
> any thoughts on that?


Not sure I follow what you mean by "increase the reference count."  
Lua does not use reference counting anywhere; it's exclusively  
garbage collected.

In our application, we approach this problem by storing a reference  
to the new thread in the global reference list and removing that  
reference when the coroutine is done. I think that's done via lua_ref  
and lua_unref, but I don't have the code handy at the moment.

-Eric

Reply | Threaded
Open this post in threaded view
|

Re: Co-Routines not dying and resulting in a crash (stack overflow?)

Raymond Jacobs
Ah,
 
lua_ref and lua_unref allow you to keep an object in C so it doesn't get GC'd,
I was looking for somthing like that but couldn't find it, that sounds like a great plan.
 
thanks again Eric =)
 
-Raymond

 
On 4/12/06, Eric Scouten <[hidden email]> wrote:
On 12 Apr 2006, at 11:07, Raymond Jacobs wrote:

> Hi Eric,
>
> Thanks for the reply, I am currently solving it by iterating the
> stack and removing the co-routine when i have decided it is dead,
> this work but it's a little 'ick', my main issues were that I
> didint know (and the documentation did not specify) that the co-
> routines do not die upon completion.
>
> it seems the optimal situation would be to increase the reference
> count of the new thread when i create it, and immediately pop it
> off the stack (since when i need to get it later the position is
> not predictable and i need to search) and then later be able to
> decrement the ref count by simply having the lua_State* that
> represents the co-routine.
>
> any thoughts on that?


Not sure I follow what you mean by "increase the reference count."
Lua does not use reference counting anywhere; it's exclusively
garbage collected.

In our application, we approach this problem by storing a reference
to the new thread in the global reference list and removing that
reference when the coroutine is done. I think that's done via lua_ref
and lua_unref, but I don't have the code handy at the moment.

-Eric