456 Commits

Author SHA1 Message Date
hunterx008
0428771acb Remove redundant condition in function:event_base_once 2024-05-04 12:39:38 +03:00
Andy Pan
6074d55822 Avoid calling read(2) on eventfd on each event-loop wakeup
Register the eventfd with EPOLLET to enable edge-triggered notification
where we don't need to read the data from the eventfd for every wakeup
event.

When the eventfd counter reaches the maximum value of the unsigned 64-bit,
we rewind the counter and retry again. This optimization saves one system
call on each event-loop wakeup, which eliminates the extra latency for epoll
as the EVFILT_USER filter does for the kqueue.
2024-04-20 13:16:17 +03:00
Azat Khuzhin
2e31277207 Disable lazy gettimeofday/clock_gettime comparison
The benefit of this optimization is questionable for the following
reasons:
- libevent uses CLOCK_MONOTONIC_COARSE which is fast enough (on my
  desktop CLOCK_MONOTONIC/CLOCK_MONOTONIC_RAW works 40/50 millions of
  ops per second, and CLOCK_MONOTONIC_COARSE is faster)
- libevent has caching of time (EVENT_BASE_FLAG_NO_CACHE_TIME)

So I don't see any reason for using one more caching - lazy comparsion
(whatever you call it).
2023-06-21 09:47:07 +02:00
Krzysztof Dynowski
4f993028be Synchronize clock before tv_timeout calculation. 2023-06-21 08:58:58 +02:00
Dmitry Antipov
d19690980d Silence weird GCC warning about an uninitialized variable
For the CMake release build with -DEVENT__ENABLE_GCC_WARNINGS=ON
and -DEVENT__DISABLE_MM_REPLACEMENT=ON, GCC version 11 and 12 may
complain about possibly uninitialized variable:

In function ‘event_debug_assert_not_added_’,
    inlined from ‘event_assign’ at event.c:2184:2,
    inlined from ‘event_new’ at event.c:2276:6:
cc1: error: ‘ev’ may be used uninitialized [-Werror=maybe-uninitialized]
event.c: In function ‘event_new’:
event.c:361:13: note: by argument 1 of type ‘const struct event *’ to  event_debug_assert_not_added_.part.0’ declared here
  361 | static void event_debug_assert_not_added_(const struct event *ev)
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This warning is most likely a false positive and can be silenced
by explicitly disabling inlining for 'event_assign()'.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2023-05-14 20:42:19 +02:00
liaotonglang
d06e573c60 style fix: use tab for indent 2023-05-05 22:42:43 +02:00
Dmitry Antipov
1af745d033 signal: new signal handling backend based on signalfd
Linux-specific signal handling backend based on signalfd(2)
system call, and public function event_base_get_signal_method()
to obtain an underlying kernel signal handling mechanism.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2022-11-12 21:14:48 +01:00
Azat Khuzhin
2dfad6c339 Suppress -Wdangling-pointer in event_signal_closure()
gcc 12 complains:

    [34/46] Building C object CMakeFiles/event_static.dir/event.c.o
    /src/le/libevent/event.c: In function ‘event_signal_closure’:
    /src/le/libevent/event.c:1384:32: warning: storing the address of local variable ‘ncalls’ in ‘*ev.ev_.ev_signal.ev_pncalls’ [-Wdangling-pointer=]
     1384 |                 ev->ev_pncalls = &ncalls;
          |                 ~~~~~~~~~~~~~~~^~~~~~~~~
    /src/le/libevent/event.c:1378:15: note: ‘ncalls’ declared here
     1378 |         short ncalls;
          |               ^~~~~~
    /src/le/libevent/event.c:1378:15: note: ‘ev’ declared here
2022-11-12 18:06:09 +01:00
mareksm
b19af675c7
Fix non-std printf %p arguments (#1327)
* Fix non-std printf %p arguments when running with -Werror -pedantic-errors

Co-authored-by: Mareks Malnacs <mareks.malnacs>
2022-08-28 15:27:04 +03:00
Yong Wu
990bcfc28e ev_io_timeout consistency 2021-09-12 00:39:46 +03:00
Yong Wu
7459407701
modify typo - timeeouts 2021-09-05 22:42:04 +08:00
Nick Grifka
83ef3216e4 Add wepoll support to light up the epoll backend on Windows
libevent is lacking a scalable backend on Windows. Let's leverage the wepoll
library until Windows comes up with an epoll/kqueue compete user mode API.

- All regress tests pass for standard wepoll
- These 2 tests fail intermittently for changelist wepoll, so disabling
  changelist wepoll for now
     http/cancel_inactive_server
     http/stream_in
- verify target on Windows runs tests for both wepoll and win32 backends
- wepoll backend preferred over win32 backend
- wepoll version 1.5.6

v2: cleaner backend abstraction. Disallow wepoll on MinGW/Cygwin.
v3: Add wepoll.h to dist
v4: Make sure wepoll source files are excluded from cygwin/mingw builds
v5: Keep win32 as default backend on windows.
v6: Include wepoll in mingw builds. Verified that regress tests pass w/ WEPOLL backend.
v7: Enable wepoll on mingw when building with cmake
v8: Add wepoll testrunner for autotools test target
2020-05-08 23:27:04 +03:00
chenguolong
968bbd5c91 event_base_once: fix potential null pointer threat
supposing if base is null, EVBASE_ACQUIRE_LOCK and EVBASE_RELEASE_LOCK
would get a coredump, so we add a guard for protection.

Signed-off-by: chenguolong <cgl.chenguolong@huawei.com>
2020-02-16 02:54:15 +03:00
Azat Khuzhin
ddacaef59a Revert "Warn if forked from the event loop during event_reinit()"
Thinking about this more and realizing that this was a mistake, so
should be reverted.

In a nut shell I guess most of the apps calls event_reinit() from the
loop (see [1] for example), and this should be totally fine (the bit
with the signals [2] handled in event_reinit() gracefully)

  [1]: https://archives.seul.org/libevent/users/Aug-2019/msg00009.html
  [2]: https://github.com/libevent/libevent/pull/833#issuecomment-501834453

This reverts commit 497ef904d544ac51de43934549dbeccce8e6e8f8.

Reported-by: mikulas@twibright.com
Backport-to: 2.1
2019-09-02 22:14:22 +03:00
Jan Kasiak
445027a5dc Fix memory corruption in EV_CLOSURE_EVENT_FINALIZE with debug enabled
Call event_debug_note_teardown_ before evcb_evfinalize to avoid possible
UAF (if finalizer free's event).
2019-08-28 01:15:39 +03:00
Azat Khuzhin
497ef904d5
Warn if forked from the event loop during event_reinit()
Calling fork() from the event loop is not a great idea, since at least
it shares some internal pipes (for handling signals) before
event_reinit() call

Closes: #833
2019-07-01 23:55:29 +03:00
Tobias Stoeckmann
176fd56655
Protect min_heap_push_ against integer overflow.
Converting unsigned to size_t for size of memory objects allows
proper handling of very large heaps on 64 bit systems.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Closes: #799 (cherry-picked)
2019-04-22 23:51:25 +03:00
Dan Rosen
1cd8830de2 evwatch: fix race condition
There was a race between event_base_loop and evwatch_new (adding a
prepare/check watcher while iterating over the watcher list). Only
release the mutex immediately before invoking each watcher callback,
and reacquire it immediately afterwards (same as is done for normal
event handlers).
2019-04-17 15:52:05 -04:00
Dan Rosen
2f184f8bbf evwatch: Add "prepare" and "check" watchers.
Adds two new callbacks: "prepare" watchers, which fire immediately
before we poll for I/O, and "check" watchers, which fire immediately
after we finish polling and before we process events. This allows other
event loops to be embedded into libevent's, and enables certain
performance monitoring.

Closes: #710
2019-04-03 12:44:50 -04:00
John Ohl
62df1301ca
Add support for EV_TIMEOUT to event_base_active_by_fd
Closes: #194 (cherry-pick)
2019-03-03 23:42:03 +03:00
Azat Khuzhin
9d3a415a99
Do not check O_NONBLOCK for invalid fds
Fixes: 6f988ee1 ("Merge branch 'check-O_NONBLOCK-in-debug'")
2018-12-09 14:48:44 +03:00
Azat Khuzhin
f2da619840
event: add some debug information into loop for event_base_free_queues_()
Refs: 7c8d0152dda18ecc52d3099fea235b04ddb850d9 ("Free event queues even
for recursive finalizers")
2018-11-13 22:24:55 +03:00
Greg Hazel
965ed54c7a
assert that fds are nonblocking in debug mode
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
2018-11-04 01:15:24 +03:00
Azat Khuzhin
aa07cbac64
Add ET flag into event_base_dump_events() 2018-10-31 01:21:07 +03:00
Azat Khuzhin
23c2914f6b Notify event base if there are no more events, so it can exit without delay
Fixes: #623
2018-04-26 01:14:49 +03:00
Azat Khuzhin
08a0d36607 Fix base unlocking in event_del() if event_base_set() runned in another thread
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.
2018-02-28 07:26:11 +03:00
Azat Khuzhin
f0fd92f2c4 Convert event_debug_*() helpers from macros to static functions 2018-02-27 21:08:35 +03:00
yongqing.jiao
6cce7458d0 If precise_time is false, we should not set EVENT_BASE_FLAG_PRECISE_TIMER
Fixes: 630f077c296de61c7b99ed83bf30de11e75e2740 ("Simple unit tests for
monotonic timers")
2017-12-11 00:10:23 +03:00
James Synge
27934f0b39 Fix race in access to ev_res from event loop with event_active()
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)
2017-08-15 00:09:04 +03:00
José Luis Millán
0b4b0efdb8 Return from event_del() after the last event callback termination
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: #236
Fixes: #225
Refs: 6b4b77a
Fixes: del_wait
2017-04-30 01:40:42 +03:00
Azat Khuzhin
177e2171cb Make event_count macros cleaner
Fixes: #489
2017-03-26 21:42:19 +03:00
Azat Khuzhin
9081b66c9c Export symbols for -fvisibility=hidden (under cmake)
Fixes: #442
2017-03-13 12:57:22 +03:00
Azat Khuzhin
94e7dcebc3 Fix -Werror=implicit-fallthrough (fixes gcc-7)
Fixes: #447
2017-01-29 17:58:31 +03:00
Nicholas Marriott
88640aa1ca event_reinit: make signals works after fork() without evsig_add()
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
2015-12-27 02:51:43 +03:00
Nicholas Marriott
ad0c237bc0 event_reinit: always re-init signal's socketpair
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
2015-12-27 02:51:24 +03:00
Azat Khuzhin
7c8d0152dd Free event queues even for recursive finalizers
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
2015-10-30 14:48:14 +03:00
Azat Khuzhin
f337296a5c Fix checking for make_base_notifiable()
Fixes: a068f2e5 ("event_debug_created_threadable_ctx_: fix compilation without
debug mode")
Found-after: 3e56da23 ("travis: add builds without debug mode into matrix")
2015-10-04 03:37:26 +03:00
Azat Khuzhin
a068f2e594 event_debug_created_threadable_ctx_: fix compilation without debug mode
The following command failed before:
$ ./configure --disable-debug-mode

Fixes: dcfb19a27b7760299bc9e7291c9abd88c59fd91a ("Debug mode option to error on
evthread init AFTER other event calls.")
2015-09-29 20:42:45 +03:00
Mark Ellzey
dcfb19a27b 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!
2015-05-15 02:58:14 -07:00
Greg Hazel
6e7a580c15 tab 2015-03-24 17:45:52 -07:00
Greg Hazel
38cef641c4 fix the return value of event_deferred_cb_schedule_ 2015-03-24 17:29:40 -07:00
Nick Mathewson
a77a82a03f Merge remote-tracking branch 'azat/be-pair-fix-freeing-shared-lock-v5' 2015-02-04 08:37:32 -05:00
Azat Khuzhin
e5c87d18b0 event_free_debug_globals_locks(): disable lock debugging
This will allow to use library event after
event_free_debug_globals_locks()/libevent_global_shutdown() without
invalid read/write's.
2015-01-26 00:40:09 +03:00
Azat Khuzhin
941faaed39 event: call event_disable_debug_mode() in libevent_global_shutdown()
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().
2015-01-08 04:49:28 +03:00
Andrea Shepard
f2645f80c1 Implement new/free for struct evutil_monotonic_timer and export monotonic time functions 2014-12-04 09:30:20 -05:00
Nick Mathewson
37145c5659 Merge remote-tracking branch 'public/patches-2.0'
Conflicts:
	ChangeLog
	event.c
2014-11-30 21:07:55 -05:00
vjpai
3c7d6fcaff Fix race caused by event_active
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.
2014-11-30 19:24:15 -05:00
John Ohl
3cc0eace2e Fix use-after-free error in EV_CLOSURE_EVENT callback 2014-09-18 11:37:28 -04:00
Nick Mathewson
ec99dd82e4 Fix a use-after-free error on EV_CLOSURE_EVENT_FINALIZE callbacks
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
2014-03-18 11:27:08 -04:00
Nick Mathewson
980597215d Move assert(ev) to before we use ev in EV_CLOSURE_EVENT_FINALIZE case
Based on a patch from Harlan Stenn.
2014-03-06 10:09:03 -05:00