diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-18 20:38:00 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-04-20 07:34:16 -0700 |
| commit | 5902d02ad0a056a146231568f1293ffbcd59f61c (patch) | |
| tree | be38584c02d703ec2322ab941d4d723c752fe187 /src/platform/windows | |
| parent | 40542e7af0f5003d7ad67876ea580a59174031ca (diff) | |
| download | nng-5902d02ad0a056a146231568f1293ffbcd59f61c.tar.gz nng-5902d02ad0a056a146231568f1293ffbcd59f61c.tar.bz2 nng-5902d02ad0a056a146231568f1293ffbcd59f61c.zip | |
fixes #346 nng_recv() sometimes acts on null `msg` pointer
This closes a fundamental flaw in the way aio structures were
handled. In paticular, aio expiration could race ahead, and
fire before the aio was properly registered by the provider.
This ultimately led to the possibility of duplicate completions
on the same aio.
The solution involved breaking up nni_aio_start into two functions.
nni_aio_begin (which can be run outside of external locks) simply
validates that nni_aio_fini() has not been called, and clears certain
fields in the aio to make it ready for use by the provider.
nni_aio_schedule does the work to register the aio with the expiration
thread, and should only be called when the aio is actually scheduled
for asynchronous completion. nni_aio_schedule_verify does the same thing,
but returns NNG_ETIMEDOUT if the aio has a zero length timeout.
This change has a small negative performance impact. We have plans to
rectify that by converting nni_aio_begin to use a locklesss flag for
the aio->a_fini bit.
While we were here, we fixed some error paths in the POSIX subsystem,
which would have returned incorrect error codes, and we made some
optmizations in the message queues to reduce conditionals while holding
locks in the hot code path.
Diffstat (limited to 'src/platform/windows')
| -rw-r--r-- | src/platform/windows/win_iocp.c | 11 | ||||
| -rw-r--r-- | src/platform/windows/win_ipc.c | 8 | ||||
| -rw-r--r-- | src/platform/windows/win_resolv.c | 10 |
3 files changed, 13 insertions, 16 deletions
diff --git a/src/platform/windows/win_iocp.c b/src/platform/windows/win_iocp.c index 1189433a..a3ae3748 100644 --- a/src/platform/windows/win_iocp.c +++ b/src/platform/windows/win_iocp.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Garrett D'Amore <garrett@damore.org> -// Copyright 2017 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -155,12 +155,11 @@ nni_win_event_resubmit(nni_win_event *evt, nni_aio *aio) void nni_win_event_submit(nni_win_event *evt, nni_aio *aio) { - nni_mtx_lock(&evt->mtx); - if (nni_aio_start(aio, nni_win_event_cancel, evt) != 0) { - // the aio was aborted - nni_mtx_unlock(&evt->mtx); + if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&evt->mtx); + nni_aio_schedule(aio, nni_win_event_cancel, evt); nni_aio_list_append(&evt->aios, aio); nni_win_event_start(evt); nni_mtx_unlock(&evt->mtx); diff --git a/src/platform/windows/win_ipc.c b/src/platform/windows/win_ipc.c index 5843917c..d372f639 100644 --- a/src/platform/windows/win_ipc.c +++ b/src/platform/windows/win_ipc.c @@ -491,16 +491,16 @@ nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; + if (nni_aio_begin(aio) != 0) { + return; + } nni_mtx_lock(&w->mtx); NNI_ASSERT(!nni_list_active(&w->waiters, ep)); - if (nni_aio_start(aio, nni_win_ipc_conn_cancel, ep) != 0) { - nni_mtx_unlock(&w->mtx); - return; - } ep->con_aio = aio; nni_list_append(&w->waiters, ep); + nni_aio_schedule(aio, nni_win_ipc_conn_cancel, ep); nni_cv_wake(&w->cv); nni_mtx_unlock(&w->mtx); } diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c index d07b4fd5..72e6b439 100644 --- a/src/platform/windows/win_resolv.c +++ b/src/platform/windows/win_resolv.c @@ -180,6 +180,9 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, int family, int rv; int fam; + if (nni_aio_begin(aio) != 0) { + return; + } switch (family) { case NNG_AF_INET: fam = AF_INET; @@ -211,12 +214,7 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, int family, item->family = fam; nni_mtx_lock(&nni_win_resolv_mtx); - // If we were stopped, we're done... - if ((rv = nni_aio_start(aio, nni_win_resolv_cancel, item)) != 0) { - nni_mtx_unlock(&nni_win_resolv_mtx); - NNI_FREE_STRUCT(item); - return; - } + nni_aio_schedule(aio, nni_win_resolv_cancel, item); nni_task_dispatch(&item->task); nni_mtx_unlock(&nni_win_resolv_mtx); } |
