diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-01-05 11:16:03 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-01-05 13:22:32 -0800 |
| commit | 1eaf9e86a8f54d77d6f392829d1b859c94965329 (patch) | |
| tree | 2efa5ea0befd760b9011989639f9572a58a55f03 /src/supplemental | |
| parent | 36ff88911f8c4a0859457b0fc511333965163c82 (diff) | |
| download | nng-1eaf9e86a8f54d77d6f392829d1b859c94965329.tar.gz nng-1eaf9e86a8f54d77d6f392829d1b859c94965329.tar.bz2 nng-1eaf9e86a8f54d77d6f392829d1b859c94965329.zip | |
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.
Diffstat (limited to 'src/supplemental')
| -rw-r--r-- | src/supplemental/tcp/tcp.c | 7 | ||||
| -rw-r--r-- | src/supplemental/tls/mbedtls/tls.c | 58 |
2 files changed, 37 insertions, 28 deletions
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. <info@staysail.tech> +// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2019 Devolutions <info@devolutions.net> // @@ -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 |
