From 1eaf9e86a8f54d77d6f392829d1b859c94965329 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 5 Jan 2020 11:16:03 -0800 Subject: fixes #1112 POSIX pollq finalizers could be simpler We reap the connections when closing, to ensure that the clean up is done outside the pollq thread. This also reduces pressure on the pollq, we think. But more importantly it eliminates some complex code that was meant to avoid deadlocks, but ultimately created other use-after-free challenges. This work is an enabler for further simplifications in the aio/task logic. While here we converted some potentially racy locking of the dialers and reference counts to simpler lock-free reference counting. --- src/supplemental/tcp/tcp.c | 7 +++-- src/supplemental/tls/mbedtls/tls.c | 58 +++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 28 deletions(-) (limited to 'src/supplemental') diff --git a/src/supplemental/tcp/tcp.c b/src/supplemental/tcp/tcp.c index 02a3351f..02d5d6ce 100644 --- a/src/supplemental/tcp/tcp.c +++ b/src/supplemental/tcp/tcp.c @@ -155,12 +155,15 @@ tcp_dialer_free(void *arg) return; } + nni_aio_stop(d->resaio); + nni_aio_stop(d->conaio); + nni_aio_fini(d->resaio); + nni_aio_fini(d->conaio); + if (d->d != NULL) { nni_tcp_dialer_close(d->d); nni_tcp_dialer_fini(d->d); } - nni_aio_fini(d->resaio); - nni_aio_fini(d->conaio); nni_mtx_fini(&d->mtx); nni_strfree(d->host); nni_strfree(d->port); diff --git a/src/supplemental/tls/mbedtls/tls.c b/src/supplemental/tls/mbedtls/tls.c index b7ed0575..d49c9de5 100644 --- a/src/supplemental/tls/mbedtls/tls.c +++ b/src/supplemental/tls/mbedtls/tls.c @@ -1,5 +1,5 @@ // -// Copyright 2019 Staysail Systems, Inc. +// Copyright 2020 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // Copyright 2019 Devolutions // @@ -81,6 +81,7 @@ typedef struct { nni_list sends; // upper side sends nni_list recvs; // upper recv aios nni_aio * handshake; // handshake aio (upper) + nni_reap_item reap; } tls; struct nng_tls_config { @@ -279,36 +280,41 @@ tls_mkerr(int err) // The common code should call this only after it has released // it's upper layer stuff. static void -tls_free(void *arg) +tls_reap(void *arg) { tls *tls = arg; // Shut it all down first. - if (tls != NULL) { - if (tls->tcp != NULL) { - nng_stream_close(tls->tcp); - } - nni_aio_stop(tls->tcp_send); - nni_aio_stop(tls->tcp_recv); - nni_aio_fini(tls->com.aio); - - // And finalize / free everything. - nng_stream_free(tls->tcp); - nni_aio_fini(tls->tcp_send); - nni_aio_fini(tls->tcp_recv); - mbedtls_ssl_free(&tls->ctx); - nng_tls_config_free(tls->com.cfg); - - if (tls->recvbuf != NULL) { - nni_free(tls->recvbuf, NNG_TLS_MAX_RECV_SIZE); - } - if (tls->sendbuf != NULL) { - nni_free(tls->sendbuf, NNG_TLS_MAX_RECV_SIZE); - } - nni_mtx_fini(&tls->lk); - memset(tls, 0xff, sizeof(*tls)); - NNI_FREE_STRUCT(tls); + if (tls->tcp != NULL) { + nng_stream_close(tls->tcp); + } + nni_aio_stop(tls->tcp_send); + nni_aio_stop(tls->tcp_recv); + nni_aio_fini(tls->com.aio); + + // And finalize / free everything. + nng_stream_free(tls->tcp); + nni_aio_fini(tls->tcp_send); + nni_aio_fini(tls->tcp_recv); + mbedtls_ssl_free(&tls->ctx); + nng_tls_config_free(tls->com.cfg); + + if (tls->recvbuf != NULL) { + nni_free(tls->recvbuf, NNG_TLS_MAX_RECV_SIZE); + } + if (tls->sendbuf != NULL) { + nni_free(tls->sendbuf, NNG_TLS_MAX_RECV_SIZE); } + nni_mtx_fini(&tls->lk); + memset(tls, 0xff, sizeof(*tls)); + NNI_FREE_STRUCT(tls); +} + +static void +tls_free(void *arg) +{ + tls *tls = arg; + nni_reap(&tls->reap, tls_reap, tls); } int -- cgit v1.2.3-70-g09d2