aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat_libevent.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-11-14 18:12:29 -0500
committerNick Mathewson <nickm@torproject.org>2011-11-14 18:12:29 -0500
commit7be50c26e8b2ec0bd0121d5e8b2dc55de977cf5a (patch)
treed6f8502a81e0bd4b43791112d0e40a6fcd80a878 /src/common/compat_libevent.c
parentcf8117136c1b206e727d582a4c378ce53e058414 (diff)
downloadtor-7be50c26e8b2ec0bd0121d5e8b2dc55de977cf5a.tar
tor-7be50c26e8b2ec0bd0121d5e8b2dc55de977cf5a.tar.gz
Disable IOCP and retry event_base_new_with_config once on failure
This is a fancier bug4457 workaround for 0.2.3. In 0.2.2, we could just tell Libevent "Don't enable locking!" so it wouldn't try to make the event_base notifiable. But for IOCP, we need a notifiable base. (Eventually, we'll want a notifiable base for other stuff, like multithreaded crypto.) So the solution is to try a full-featured initialization, and then retry with all the options turned off if that fails.
Diffstat (limited to 'src/common/compat_libevent.c')
-rw-r--r--src/common/compat_libevent.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c
index c53346118..7a28c9bc9 100644
--- a/src/common/compat_libevent.c
+++ b/src/common/compat_libevent.c
@@ -195,7 +195,14 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
#ifdef HAVE_EVENT2_EVENT_H
{
- struct event_config *cfg = event_config_new();
+ int attempts = 0;
+ int using_threads;
+ struct event_config *cfg;
+
+ retry:
+ ++attempts;
+ using_threads = 0;
+ cfg = event_config_new();
tor_assert(cfg);
#if defined(MS_WINDOWS) && defined(USE_BUFFEREVENTS)
@@ -203,9 +210,18 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
evthread_use_windows_threads();
event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
using_iocp_bufferevents = 1;
+ using_threads = 1;
+ } else {
+ using_iocp_bufferevents = 0;
}
#endif
+ if (!using_threads) {
+ /* Telling Libevent not to try to turn locking on can avoid a needless
+ * socketpair() attempt. */
+ event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK);
+ }
+
#if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,0,7)
if (torcfg->num_cpus > 0)
event_config_set_num_cpus_hint(cfg, torcfg->num_cpus);
@@ -220,6 +236,25 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
the_event_base = event_base_new_with_config(cfg);
event_config_free(cfg);
+
+ if (using_threads && the_event_base == NULL && attempts < 2) {
+ /* This could be a socketpair() failure, which can happen sometimes on
+ * windows boxes with obnoxious firewall rules. Downgrade and try
+ * again. */
+#if defined(MS_WINDOWS) && defined(USE_BUFFEREVENTS)
+ if (torcfg->disable_iocp == 0) {
+ log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again with "
+ "IOCP disabled.");
+ } else
+#endif
+ {
+ log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again.");
+ }
+
+ torcfg->disable_iocp = 1;
+ goto retry;
+ }
+
}
#else
the_event_base = event_init();