Hello again,

I'm currently working with libevent 2.02-alpha and I encountered a weird 
problem when I try to exit the base loop from another thread.

When I developed my application and tested it as a single-thread 
application I had no problems shutting down the base loop with 
'event_base_loopexit(base, NULL);' when using the following setup:

void close_stuff(int dummy)
{
event_base_loopexit(base, NULL);
}

int main(void)
{
signal(SIGHUP, close_stuff);

base = event_base_new();
// ..snip.. socket creation
event_base_dispatch(base);

// ..snip.. cleanup
event_base_free(base);

return 0;
}

If I would then send a SIGHUP signal to the program, the loop would exit 
almost immediately and it would clean up all memory, etc.

I then copied the code to a module for one of my programs. This code 
looks like this:

void module_destroy()
{
if (base != NULL)
{
printf("Exiting event loop..\n");
event_base_loopexit(base, NULL);
}

printf("Unloading module\n");
}

void *module_loop(void* ptr)
{
(void)ptr;

base = event_base_new();
// ..snip.. socket creation
event_base_dispatch(base);

// ..snip.. cleanup
event_base_free(base);
return 0;
}

The only difference with the previous code is that the 'module_loop' 
code runs in a (pthread) thread. In both cases 'base' is global. The 
code itself works fine, just as in the single-thread application, so no 
problems there.

The problems arise however when then module gets notified that it needs 
to be unloaded. The 'module_destroy' function is then called in the main 
thread (e.g. not the same thread as the libevent code).

When I then call the 'event_base_loopexit(base, NULL);' nothing happens. 
The libevent loop keeps running and does not appear to end. All 
connections made also stay alive long after the module has been 
unloaded. The main thread then crashes after a few minutes, probably 
because libevent is still accessing things.

I already tried adding a 'sleep(3);' after the loopexit call to give the 
loop some time to close, but that doesn't help either - it just freezes 
the main thread.

Am I doing something wrong here? According to the "libevent book" this 
was not supported before 2.0:

"Because event_base did not support locking before Libevent 2.0, these 
functions weren’t completely threadsafe: it was not permissible to call 
the _loopbreak() or _loopexit() functions from a thread other than the 
one executing the event loop.".

But, I gather from that note that it should be possible now?


_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to