Check that each fd that had been added with some event do has O_NOBLOCK
after event_enable_debug_mode()
Rebased and do not check signals (EV_SIGNAL) by azat.
Refs: nmathewson/Libevent#90
Refs: #96
Image next situation:
T1: T2:
event_del_()
lock the event.ev_base.th_base_lock
event_del_nolock_() event_set_base()
unlock the event.ev_base.th_base_lock
In this case we will unlock the wrong base after event_del_nolock_()
returns, and deadlock is likely to happens, since event_base_set() do
not check any mutexes (due to it is possible to do this only if event is
not inserted anywhere).
So event_del_() has to cache the base before removing the event, and
cached base.th_base_lock after.
Detected using ThreadSanitizer, resolved by capturing the value
of ev_res in a local variable while the event is locked, then
passing that captured variable to the callback.
TSAN report:
I0728 14:35:09.822118 WARNING: ThreadSanitizer: data race (pid=815501)
I0728 14:35:09.822186 Write of size 2 at 0x7b2c00001bf2 by thread T80 (mutexes: write M1110835549570434736):
I0728 14:35:09.822248 #0 event_active_nolock_ libevent/event.c:2893:14 (0a2b90577e830d775300664df77d0b91+0x1fdab28)
I0728 14:35:09.822316 #1 event_active libevent/event.c:2858:2 (0a2b90577e830d775300664df77d0b91+0x1fdd10e)
I0728 14:35:09.822379 #2 Envoy::Event::TimerImpl::enableTimer(std::chrono::duration<long, std::ratio<1l, 1000l> > const&) envoy/source/common/event/timer_impl.cc:24:5 (0a2b90577e830d775300664df77d0b91+0x459fa0)
...
I0728 14:35:09.824146 Previous read of size 2 at 0x7b2c00001bf2 by main thread:
I0728 14:35:09.824232 #0 event_process_active_single_queue libevent/event.c:1646:33 (0a2b90577e830d775300664df77d0b91+0x1fdf83d)
I0728 14:35:09.824350 #1 event_process_active libevent/event.c (0a2b90577e830d775300664df77d0b91+0x1fd9ad8)
I0728 14:35:09.824445 #2 event_base_loop libevent/event.c:1961 (0a2b90577e830d775300664df77d0b91+0x1fd9ad8)
I0728 14:35:09.824550 #3 Envoy::Event::DispatcherImpl::run(Envoy::Event::Dispatcher::RunType) envoy/source/common/event/dispatcher_impl.cc:166:3 (0a2b90577e830d775300664df77d0b91+0x4576d9)
...
Fixes: #543 (pull-request)
Delete the event from the queue before blocking for the current
event callback termination.
Ensures that no callback is being executed when event_del() returns,
hence making this function a secure mechanism to access data which is
handled in the event callack.
Fixes: #236Fixes: #225
Refs: 6b4b77a
Fixes: del_wait
event_reinit() removes the event, but only evsig_add puts it back. So any
signals set up before event_reinit will be ignored until another signal is
added.
Fixes: #307
Before this patch event_reinit() only closes the signal socketpair fds and
recreates them if signals have been added, but this is wrong, since socketpair
fds created on backend init, and if we will not re-create them bad things in
child/parent signal handling will happens (and indeed this is what happens for
non-reinit backends like select).
Fixes: #307
For finalizers we can register yet another finalizer out from finalizer, and
iff finalizer will be in active_later_queue we can add finalizer to
activequeues, and we will have events in activequeues after event_base_free()
returns, which is not what we want (we even have an assertion for this).
A simple case is bufferevent with underlying (i.e. filters) in inactive queue.
Fixes: regress bufferevent/bufferevent_socket_filter_inactive
The following command failed before:
$ ./configure --disable-debug-mode
Fixes: dcfb19a27b7760299bc9e7291c9abd88c59fd91a ("Debug mode option to error on
evthread init AFTER other event calls.")
- A handy event_enable_debug_mode() feature which will error and abort the
application if any thread-aware libevent functions are called BEFORE the
evthread API has been initialized (manually, or through
evthread_use_windows_threads() / evthread_use_pthreads()
- This is done by setting the global debug variable
'event_debug_created_threadable_ctx_' whenever the following functions
are called:
evthreadimpl_lock_alloc_()
evthreadimpl_cond_alloc_()
event_base_new_with_config() <- this checks to see if the thread
callbacks are enabled first, so we
have to manually set the variable.
- Example:
int main(int argc, char ** argv) {
struct event_base * base;
event_enable_debug_mode();
base = event_base_new();
evthread_use_pthreads();
return 0;
}
When executed, the program will throw an error and exit:
[err] evthread initialization must be called BEFORE anything else!
This will avoid leaking of event_debug_map_HT_GROW
I buildin it into libevent_glboal_shutdown() because
event_disable_debug_mode() -> event_free_debug_globals() ->
event_free_debug_globals_locks() will clean event_debug_map_lock_ that
used in event_disable_debug_mode().
There is a race between manual event_active and natural event activation. If both happen at the same time on the same FD, they would both be protected by the same event base lock except for 1 LoC where the fields of struct event are read without any kind of lock. This commit does those reads into local variables inside the lock and then invokes the callback with those local arguments outside the lock. In 2.0-stable, none of this is inside the lock; in HEAD, only the callback is read inside the lock. This gets the callback and all 3 arguments inside the lock before calling it outside the lock.
After running the callback, we were checking evcb->evcb_closure to
decide whether to call mm_free(ev). But the callback itself might
have freed ev, so we need to grab that field first
Found with AddressSanitizer
This commit provides an interface for accessing and resetting the maximum
number of events in a given period. This information provides better insight
into event queue pressure.
In event_process_active_single_queue, EVLIST_INIT must be set on any
event that uses one of the event-only closures, and so "ev" will be
set in those cases. But coverity's worried here (CIDs numerous). So
instead, just add the assertions that should make it happy.