]> granicus.if.org Git - libevent/commitdiff
Merge branch 'listener-immediate-close'
authorAzat Khuzhin <a3at.mail@gmail.com>
Thu, 4 Jan 2018 16:28:59 +0000 (19:28 +0300)
committerAzat Khuzhin <azat@libevent.org>
Sat, 2 Feb 2019 12:13:55 +0000 (15:13 +0300)
* listener-immediate-close:
  test/listener: cover immediate-close logic
  Immediately stop trying to accept more connections if listener disabled

(cherry picked from commit 416b48ba7a0b44407b7a053bd21830aa75d7162f)

listener.c
test/regress_listener.c

index 942fa45180f957fd4a6b751c21d8792cc5ce9d43..2835df176114dca164efb02f732cd2d923ada1f6 100644 (file)
@@ -424,6 +424,11 @@ listener_read_cb(evutil_socket_t fd, short what, void *p)
                        return;
                }
                --lev->refcnt;
+               if (!lev->enabled) {
+                       /* the callback could have disabled the listener */
+                       UNLOCK(lev);
+                       return;
+               }
        }
        err = evutil_socket_geterror(fd);
        if (EVUTIL_ERR_ACCEPT_RETRIABLE(err)) {
index 32f2307426e5272daacd14be2f914e5d645c1e05..070e5e34f61858d2828cd7c73384c16359a74650 100644 (file)
@@ -230,6 +230,44 @@ end:
        ;
 }
 
+static void
+regress_listener_immediate_close(void *arg)
+{
+       struct basic_test_data *data = arg;
+       struct event_base *base = data->base;
+       struct evconnlistener *listener = NULL;
+       struct sockaddr_in sin;
+       struct sockaddr_storage ss;
+       ev_socklen_t slen = sizeof(ss);
+       int count = 1;
+       unsigned int flags = LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE;
+       int fd1 = -1, fd2 = -1;
+
+       memset(&sin, 0, sizeof(sin));
+       sin.sin_family = AF_INET;
+       sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
+       sin.sin_port = 0; /* "You pick!" */
+
+       /* Start a listener with a bogus socket. */
+       listener = evconnlistener_new_bind(base, acceptcb, &count,
+           flags, -1, (struct sockaddr *)&sin, sizeof(sin));
+       tt_assert(listener);
+
+       tt_assert(getsockname(evconnlistener_get_fd(listener),
+               (struct sockaddr*)&ss, &slen) == 0);
+
+       evutil_socket_connect_(&fd1, (struct sockaddr*)&ss, slen);
+       evutil_socket_connect_(&fd2, (struct sockaddr*)&ss, slen);
+
+       event_base_dispatch(base);
+
+       tt_int_op(count, ==, 0);
+
+end:
+       if (listener)
+               evconnlistener_free(listener);
+}
+
 #ifdef EVENT__HAVE_SETRLIMIT
 static void
 regress_listener_error_unlock(void *arg)
@@ -290,6 +328,9 @@ struct testcase_t listener_testcases[] = {
        { "close_accepted_fd", regress_listener_close_accepted_fd,
          TT_FORK|TT_NEED_BASE, &basic_setup, NULL, },
 
+       { "immediate_close", regress_listener_immediate_close,
+         TT_FORK|TT_NEED_BASE, &basic_setup, NULL, },
+
        END_OF_TESTCASES,
 };