Nick Mathewson [Tue, 17 Jul 2012 15:08:25 +0000 (11:08 -0400)]
Fix a memory leak on error in evhttp_uriencode
If we ran out of memory in evhttp_uriencode when allocating the
output buffer, we would neglect to call evbuffer_free() on our
temporary buffer. Now we always free the temporary buffer.
Fixes sourceforge issue 3539887. Thanks to Andrew Cox for reporting
this one.
Nick Mathewson [Thu, 28 Jun 2012 16:00:57 +0000 (12:00 -0400)]
Make th_base_lock nonrecursive
This is necessary for making some thread libraries work with
event.c, and might get better performance with others.
The biggest change required here was that we needed to make some
internal code that had previously called event_add and event_del
call the nolock variants.
Greg Hazel [Tue, 29 May 2012 19:39:12 +0000 (12:39 -0700)]
check for arc4random_buf at runtime, on OS X
(Tweaked by nickm: Fix up the arcr4andom_buf OSX hack so that the
fallback case isn't compiled into the code when we have
arc4random_buf() and we are not on OSX. Also add a comment
explaining what's up.)
Nick Mathewson [Wed, 9 May 2012 15:06:06 +0000 (11:06 -0400)]
Restore our priority-inversion-prevention code with deferreds
Back when deferred_cb stuff had its own queue, the queue was always
executed, but we never ran more than 16 callbacks per iteration.
That made for two problems:
1: Because deferred_cb stuff would always run, and had no priority,
it could cause priority inversion.
2: It doesn't respect the max_dispatch_interval code.
Then, when I refactored deferred_cb to be a special case of
event_callback, that solved the above issues, but made for two more
issues:
3: Because deferred_cb stuff would always get the default priority,
it could could low-priority bufferevents to get too much priority.
4: With code like bufferevent_pair, it's easy to get into a
situation where two deferreds keep adding one another, preventing
the event loop from ever actually scanning for more events.
This commit fixes the above by giving deferreds a better notion of
priorities, and by limiting the number of deferreds that can be
added to the _current_ loop iteration's active queues. (Extra
deferreds are put into the active_later state.)
That isn't an all-purpose priority inversion solution, of course: for
that, you may need to mess around with max_dispatch_interval.
Nick Mathewson [Fri, 6 Apr 2012 07:00:40 +0000 (03:00 -0400)]
Add "active later" event_callbacks to supersede deferred
An event or event callback can now be in an additional state: "active
later". When an event is in this state, it will become active the
next time we run through the event loop. This lets us do what we
wanted to with deferred callbacks: make a type of active thing that
avoids infinite circular regress in a way that starves other events or
exhausts the stack. It improves on deferred callbacks by respecting
priorities, and by having a non-kludgy way to avoid event starvation.
Nick Mathewson [Thu, 5 Apr 2012 16:38:18 +0000 (12:38 -0400)]
Refactor the callback part of an event into its own event_callback type
This shouldn't have any visible effect, but it's necessary or
advisible for a few changes and cleanups I would like to make,
including:
* Replacing the deferred queue with a type that works more as if it
were an event.
* Introducing a useful "activate this on the next round through the
event loop" state for events and deferreds.
* Adding an "on until further notice" status for events, to allow a
saner win32-hybrid approach.
* Eventually, making all user callbacks first-class things with
event-like semantics.
Nick Mathewson [Tue, 1 May 2012 17:03:33 +0000 (13:03 -0400)]
Correctly handle running on a system where accept4 doesn't work.
Previously, we treated EINVAL as the only errno that indicated a
broken accept4. But EINVAL only appears when one of the SOCK_*
options isn't supported. If the accept4 syscall itself isn't there,
we'll get an ENOSYS.
Mark Ellzey [Fri, 30 Mar 2012 19:08:40 +0000 (15:08 -0400)]
Fixed potential double-readcb execution with openssl bufferevents.
the function do_read() will call SSL_read(), and if successful, will
call _bufferevent_run_readcb() before returning to consider_reading().
consider_reading() will then check SSL_pending() to make sure all
pending data has also been read. If it does not, do_read() is called
again.
The issue with this is the possibility that the function that is
executed by _bufferevent_run_readcb() called
bufferevent_disable(ssl_bev, EV_READ) before the second do_read(); In
this case, the users read callback is executed a second time. This is
potentially bad for applications that expect the bufferevent to stay
disabled until further notice. (this is why running openssl bufferevents
without DEFER_CALLBACKS has always been troublesome).
Nick Mathewson [Thu, 26 Apr 2012 20:22:03 +0000 (16:22 -0400)]
When PRECISE_TIMERS is set with epoll, use timerfd for microsecond precision
The epoll interface ordinarily gives us one-millisecond
precision, so on Linux it makes perfect sense to use the
CLOCK_MONOTONIC_COARSE timer. But when the user has set the new
PRECISE_TIMER flag for an event_base (either by the
EVENT_BASE_FLAG_PRECISE_TIMER flag, or by the EVENT_PRECISE_TIMER
environment variable), they presumably want finer granularity.
On not-too-old Linuxes, we can achieve this using the Timerfd
mechanism, which accepts nanosecond granularity and understands
posix clocks. It's a little more expensive than just calling
epoll_wait(), so we won't do it by default.
Nick Mathewson [Thu, 19 Apr 2012 04:25:12 +0000 (00:25 -0400)]
If time has jumped so we'd reschedule a periodic event in the past, schedule it for the future instead
Fixes an issue reported on libevent-users in the thread "a dead
looping bug when changing system time backward". Previously, if time
jumped forward 1 hour[*] and we had a one-second periodic timer event,
that event would get invoked 3600 times. That's almost certainly not
what anybody wants.
In a future version of Libevent, we should expose the amount of time
that the callbac kwould have been invoked somehow.
[*] Forward time jumps can happen with nonmonotonic clocks, or with
clocks that jump on suspend/resume. It can also happen from
Libevent's point of view if the user exits from event_base_loop() and
doesn't call it again for a while.
Nick Mathewson [Tue, 17 Apr 2012 17:04:02 +0000 (13:04 -0400)]
EVENT_BASE_FLAG_PRECISE_TIMER indicates we want fine timer precision
There are a bunch of backends that can give us a reasonably good
monotonic timer quickly, or a very precise monotonic timer less
quickly. For example, Linux has CLOCK_MONOTONIC_COARSE (1msec
precision), which is over twice as fast as CLOCK_MONOTONIC. Since
epoll only lets you wait with 1msec precision,
CLOCK_MONOTONIC_COARSE is a clear win.
On Windows, you've got GetTickCount{,64}() which is fast, and
QueryPerformanceCounter, which is precise but slow.
Note that even in the cases where the underlying timer claims to
have nanosecond resolution, we're still not exposing that via
Libevent.
Note also that "Precision" isn't the same as "Resolution" or
"Accuracy". A timer's precision is the smallest change that the
clock will register; a timer's resolution is the fineness of its
underlying representation; a timer's accuracy is how much it drifts
with respect to "Real Time", whatever that means. (Terms and
definitions from PEP 418.)
Nick Mathewson [Tue, 17 Apr 2012 16:44:39 +0000 (12:44 -0400)]
Move use_monotonic and friends into event_base
The use_monotonic field used to be a static field set up at library
setup. Unfortunately, this makes it hard to give the user a way to
make speed/accuracy tradeoffs about time. Moving it into event_base
should let the clock implementation become configurable.
Nick Mathewson [Fri, 17 Sep 2010 04:34:13 +0000 (00:34 -0400)]
Replace pipe-based notification with EVFILT_USER where possible
Sufficiently recent kqueue implementations have an EVFILT_USER filter
that we can use to wake up an event base from another thread. When
it's supported, we now use this mechanism rather than our old
(pipe-based) mechanism. This should save some time and complications
on newer OSX and freebsds.