Skip to content

Instantly share code, notes, and snippets.

@kzk
Created July 12, 2011 04:38
Show Gist options
  • Save kzk/1077403 to your computer and use it in GitHub Desktop.
Save kzk/1077403 to your computer and use it in GitHub Desktop.
memcached-fix-accept-new-conns-race.patch
--- memcached-1.4.5/memcached.c 2010-04-04 04:51:29.000000000 +0900
+++ memcached-1.4.5-modified/memcached.c 2010-08-19 02:34:10.000000000 +0900
@@ -111,6 +111,10 @@
static conn *listen_conn = NULL;
static struct event_base *main_base;
+static pthread_mutex_t allow_new_conns_lock = PTHREAD_MUTEX_INITIALIZER;
+static volatile bool allow_new_conns = true;
+static volatile bool allow_new_conns_cur = true;
+
enum transmit_result {
TRANSMIT_COMPLETE, /** All done writing. */
TRANSMIT_INCOMPLETE, /** More data remaining to write. */
@@ -508,7 +512,9 @@
MEMCACHED_CONN_RELEASE(c->sfd);
close(c->sfd);
- accept_new_conns(true);
+ pthread_mutex_lock(&allow_new_conns_lock);
+ allow_new_conns = true;
+ pthread_mutex_unlock(&allow_new_conns_lock);
conn_cleanup(c);
/* if the connection has big buffers, just free it */
@@ -3436,7 +3442,9 @@
} else if (errno == EMFILE) {
if (settings.verbose > 0)
fprintf(stderr, "Too many open connections\n");
- accept_new_conns(false);
+ pthread_mutex_lock(&allow_new_conns_lock);
+ allow_new_conns = false;
+ pthread_mutex_unlock(&allow_new_conns_lock);
stop = true;
} else {
perror("accept()");
@@ -4033,6 +4041,16 @@
initialized = true;
}
+ bool do_accept_changed = false;
+ pthread_mutex_lock(&allow_new_conns_lock);
+ if (allow_new_conns != allow_new_conns_cur) {
+ allow_new_conns_cur = allow_new_conns;
+ do_accept_changed = true;
+ }
+ pthread_mutex_unlock(&allow_new_conns_lock);
+ if (do_accept_changed)
+ accept_new_conns(allow_new_conns_cur);
+
evtimer_set(&clockevent, clock_handler, 0);
event_base_set(main_base, &clockevent);
evtimer_add(&clockevent, &t);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment