summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2018-04-26 06:29:40 +0200
committerQuentin Young <qlyoung@cumulusnetworks.com>2018-05-29 21:06:16 +0200
commitae6670d0138beac5ad8166a694d112b64a4d48ca (patch)
tree0fbe697fd8cad1b4b0cf0210b5ce03af60d53fa2
parentzebra: fix zserv_read rescheduling (diff)
downloadfrr-ae6670d0138beac5ad8166a694d112b64a4d48ca.tar.xz
frr-ae6670d0138beac5ad8166a694d112b64a4d48ca.zip
zebra: don't send data after closing connection
Cancelling threads is nice but they can potentially be scheduled again after cancellation without an explicit check. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
-rw-r--r--zebra/zserv.c23
-rw-r--r--zebra/zserv.h3
2 files changed, 21 insertions, 5 deletions
diff --git a/zebra/zserv.c b/zebra/zserv.c
index 1effb2b2b..548e0b32c 100644
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -53,6 +53,7 @@
#include "lib/zassert.h" /* for assert */
#include "lib/zclient.h" /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
#include "lib/frr_pthread.h" /* for frr_pthread_new, frr_pthread_stop... */
+#include "lib/frratomic.h" /* for atomic_load_explicit, atomic_stor... */
#include "zebra/debug.h" /* for various debugging macros */
#include "zebra/rib.h" /* for rib_score_proto */
@@ -166,6 +167,7 @@ static void zserv_log_message(const char *errmsg, struct stream *msg,
*/
static void zserv_client_close(struct zserv *client)
{
+ atomic_store_explicit(&client->dead, true, memory_order_seq_cst);
THREAD_OFF(client->t_read);
THREAD_OFF(client->t_write);
zserv_event(client, ZSERV_HANDLE_CLOSE);
@@ -199,6 +201,9 @@ static int zserv_write(struct thread *thread)
int writerv;
struct stream_fifo *cache;
+ if (atomic_load_explicit(&client->dead, memory_order_seq_cst))
+ return 0;
+
/* If we have any data pending, try to flush it first */
switch (buffer_flush_available(client->wb, client->sock)) {
case BUFFER_ERROR:
@@ -286,18 +291,23 @@ zwrite_fail:
*/
static int zserv_read(struct thread *thread)
{
+ struct zserv *client = THREAD_ARG(thread);
int sock;
- struct zserv *client;
size_t already;
- struct stream_fifo *cache = stream_fifo_new();
- uint32_t p2p_orig = atomic_load_explicit(&zebrad.packets_to_process,
- memory_order_relaxed);
+ struct stream_fifo *cache;
+ uint32_t p2p_orig;
+
uint32_t p2p;
struct zmsghdr hdr;
+ if (atomic_load_explicit(&client->dead, memory_order_seq_cst))
+ return 0;
+
+ p2p_orig = atomic_load_explicit(&zebrad.packets_to_process,
+ memory_order_relaxed);
+ cache = stream_fifo_new();
p2p = p2p_orig;
sock = THREAD_FD(thread);
- client = THREAD_ARG(thread);
while (p2p) {
ssize_t nb;
@@ -435,6 +445,9 @@ zread_fail:
static void zserv_client_event(struct zserv *client,
enum zserv_client_event event)
{
+ if (atomic_load_explicit(&client->dead, memory_order_seq_cst))
+ return;
+
switch (event) {
case ZSERV_CLIENT_READ:
thread_add_read(client->pthread->master, zserv_read, client,
diff --git a/zebra/zserv.h b/zebra/zserv.h
index a1b55bf8e..c8b65b6a4 100644
--- a/zebra/zserv.h
+++ b/zebra/zserv.h
@@ -53,6 +53,9 @@ struct zserv {
/* Client pthread */
struct frr_pthread *pthread;
+ /* Whether the thread is waiting to be killed */
+ _Atomic bool dead;
+
/* Client file descriptor. */
int sock;