dns: refactor QueryWrap lifetime management
- Prefer RAII-style management over manual resource management.
- Prefer
env->SetImmediate()
over a separateuv_async_t
. - Perform
ares_destroy()
before possibly tearing down c-ares state. - Verify that the number of active queries is non-negative.
- Let pending callbacks know when their underlying
QueryWrap
object has been destroyed.
The last item has been a real bug, in that when Workers shut down during currently running DNS queries, they may run into use-after-free situations because:
- Shutting the
Worker
down leads to the cleanup code deleting theQueryWrap
objects first; then - deleting the
ChannelWrap
object (as it has been created before theQueryWrap
s), whose destructor runsares_destroy()
, which in turn invokes all pending query callbacks withARES_ECANCELLED
, - which lead to use-after-free, as the callback tried to access the
deleted
QueryWrap
object.
The added test verifies that this is no longer an issue.
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
commit message follows commit guidelines