Skip to content

Commit

Permalink
fixed 2 coroutine bugs:
Browse files Browse the repository at this point in the history
- abandoned coroutines do not properly free stack frames
- abandoned coroutines trigger recursive state freeing
  • Loading branch information
archo5 committed Jun 13, 2015
1 parent dd03d82 commit 3c4bb2b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ext/sgsapitest.c
Expand Up @@ -797,6 +797,17 @@ DEFINE_TEST( yield_resume )
destroy_context( C );
}

DEFINE_TEST( yield_abandon )
{
SGS_CTX = get_context(), *CF;

CF = sgs_ForkState( C, 0 );
const char* str = "yield();";
atf_assert( sgs_ExecString( CF, str ) == SGS_SUCCESS );

destroy_context( C );
}

int sm_tick_id = 0;
int sm_resume_id = 0;
static int sm_wait( SGS_CTX )
Expand Down Expand Up @@ -869,6 +880,7 @@ test_t all_tests[] =
TST( native_obj_meta ),
TST( fork_state ),
TST( yield_resume ),
TST( yield_abandon ),
TST( state_machine_core ),
};
int all_tests_count(){ return sizeof(all_tests)/sizeof(test_t); }
Expand Down
7 changes: 7 additions & 0 deletions src/sgs_ctx.c
Expand Up @@ -134,6 +134,12 @@ static void ctx_destroy( SGS_CTX )
{
SGS_SHCTX_USE;

if( C->state & SGS_STATE_DESTROYING )
{
return;
}
C->state |= SGS_STATE_DESTROYING;

/* clear the stack */
while( C->stack_base != C->stack_top )
{
Expand All @@ -160,6 +166,7 @@ static void ctx_destroy( SGS_CTX )
sgs_StackFrame* sf = C->sf_cached, *sfn;
while( sf )
{
sgs_Release( C, &sf->func );
sfn = sf->cached;
sgs_Dealloc( sf );
sf = sfn;
Expand Down
1 change: 1 addition & 0 deletions src/sgscript.h
Expand Up @@ -152,6 +152,7 @@ typedef SGSRESULT (*sgs_ScriptFSFunc) (
#define SGS_MUST_STOP (0x00020000 | SGS_HAS_ERRORS)
#define SGS_SERIALIZE_MODE2 0x0004
#define SGS_STATE_PAUSED 0x0008
#define SGS_STATE_DESTROYING 0x0010


/* Statistics / debugging */
Expand Down
7 changes: 7 additions & 0 deletions tests/bug00028.sgs
@@ -0,0 +1,7 @@
x = {};
co = co_create(function(x)
{
yield();
});
x.co = co;
co_resume( co, x );

0 comments on commit 3c4bb2b

Please sign in to comment.