From 7363eae13cb8febd85923957de19e2de7c186cea Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 14 Nov 2011 17:46:43 -0500 Subject: [PATCH 1/3] Use the EVENT_BASE_FLAG_NOLOCK flag to prevent socketpair() invocation In Tor 0.2.2, we never need the event base to be notifiable, since we don't call it from other threads. This is a workaround for bug 4457, which is not actually a Tor bug IMO. --- changes/bug4457 | 8 ++++++++ src/common/compat_libevent.c | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changes/bug4457 diff --git a/changes/bug4457 b/changes/bug4457 new file mode 100644 index 0000000000..d3d9eb3b40 --- /dev/null +++ b/changes/bug4457 @@ -0,0 +1,8 @@ + o Minor bugfixes: + - Initialize Libevent with the EVENT_BASE_FLAG_NOLOCK flag enabled, so + that it doesn't attempt to allocate a socketpair. This could cause + some problems on windows systems with overzealous firewalls. Fix for + bug 4457; workaround for Libevent versions 2.0.1-alpha through + 2.0.15-stable. + + diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index 6d89be804b..3e35e093e4 100644 --- a/src/common/compat_libevent.c +++ b/src/common/compat_libevent.c @@ -177,7 +177,18 @@ tor_libevent_initialize(void) #endif #ifdef HAVE_EVENT2_EVENT_H - the_event_base = event_base_new(); + { + struct event_config *cfg = event_config_new(); + + /* In 0.2.2, we don't use locking at all. Telling Libevent not to try to + * turn it on can avoid a needless socketpair() attempt. + */ + event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK); + + the_event_base = event_base_new_with_config(cfg); + + event_config_free(cfg); + } #else the_event_base = event_init(); #endif From 0f6c02161793fa1022fe721ed9c1aac3538294b3 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 14 Nov 2011 17:53:45 -0500 Subject: [PATCH 2/3] Detect failure from event_init() or event_base_new_with_config() --- changes/bug4457 | 1 + src/common/compat_libevent.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/changes/bug4457 b/changes/bug4457 index d3d9eb3b40..fe7c95ff80 100644 --- a/changes/bug4457 +++ b/changes/bug4457 @@ -5,4 +5,5 @@ bug 4457; workaround for Libevent versions 2.0.1-alpha through 2.0.15-stable. + - Detect failure to initialize Libevent. Better detection for bug 4457. diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index 3e35e093e4..ddb2da68aa 100644 --- a/src/common/compat_libevent.c +++ b/src/common/compat_libevent.c @@ -179,6 +179,7 @@ tor_libevent_initialize(void) #ifdef HAVE_EVENT2_EVENT_H { struct event_config *cfg = event_config_new(); + tor_assert(cfg); /* In 0.2.2, we don't use locking at all. Telling Libevent not to try to * turn it on can avoid a needless socketpair() attempt. @@ -193,6 +194,11 @@ tor_libevent_initialize(void) the_event_base = event_init(); #endif + if (!the_event_base) { + log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue."); + exit(1); + } + #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD) /* Making this a NOTICE for now so we can link bugs to a libevent versions * or methods better. */ From 7be50c26e8b2ec0bd0121d5e8b2dc55de977cf5a Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 14 Nov 2011 18:12:29 -0500 Subject: [PATCH 3/3] 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. --- changes/bug4457_master | 6 ++++++ src/common/compat_libevent.c | 37 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 changes/bug4457_master diff --git a/changes/bug4457_master b/changes/bug4457_master new file mode 100644 index 0000000000..d394643b6d --- /dev/null +++ b/changes/bug4457_master @@ -0,0 +1,6 @@ + o Minor features: + - When we fail to initialize Libevent, retry with IOCP disabled so we + don't need to turn on multi-threading support in Libevent, which in + turn requires a working socketpair(). This is a workaround for bug + 4457, which affects Libevent versions from 2.0.1-alpha through + 2.0.15-stable. diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index c53346118f..7a28c9bc9b 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();