Repository navigation
Description
Summary
When a coroutine awaits getaddrinfo (via the libuv reactor's
addrinfo event) and is cancelled after the addrinfo callback has
already fired but before the consumer picks up event->result, the
addrinfo memory leaks under ASAN/LSan.
Stack:
```
malloc → getaddrinfo (libc) → uv__getaddrinfo_work (libuv)
```
Location
ext/async/libuv_reactor.c, around libuv_dns_getaddrinfo_dispose and
on_addrinfo_event.
The flow:
on_addrinfo_eventruns (DNS resolved).- Sets
LIBUV_DNS_F_CALLBACK_DONEand stores the libuv-allocated
struct addrinfo *resintoevent->result. - Notifies callbacks.
- Consumer is supposed to call
libuv_freeaddrinfo(event->result)
when finished. libuv_dns_getaddrinfo_dispose— when CALLBACK_DONE is set —
just callspefree(event)without freeingevent->result.
Failure mode: if the awaiting coroutine is cancelled between (3) and
(4), nobody calls libuv_freeaddrinfo and the addrinfo lives on as a
heap leak.
Reproducer
fuzzy_tests/cross_topic/cancel_during_io.feature — TCP-accept
scenarios with a zero-delay cancel reliably reproduce this on Linux
ASAN. Three scenarios were temporarily removed from the feature
pending this fix:
- `Scenario: cancel a coroutine blocked on TCP accept`
- `Scenario: many accepters cancelled together`
- `Scenario Outline ms=0 example`
(They are referenced in a comment block in the feature file.)
stream_socket_server('tcp://127.0.0.1:0') always goes through libuv's
async getaddrinfo path even for IP literals — so the leak surfaces even
without an actual DNS call.
Naive fix doesn't work
I tried freeing in dispose when CALLBACK_DONE is set:
```c
struct addrinfo *result = ((zend_async_dns_addrinfo_t *) event)->result;
if (result != NULL) uv_freeaddrinfo(result);
pefree(...);
```
This causes `free(): double free detected` because consumers that
do pick up event->result and call libuv_freeaddrinfo themselves
(via the public `free_addrinfo` API entry) leave `event->result`
pointing to freed memory.
Suggested fix
Either:
- Set
event->result = NULLin callers after they call
libuv_freeaddrinfo. Then the dispose-side free is safe to add. - Add an explicit `LIBUV_DNS_F_RESULT_CONSUMED` flag that
callers must set after consuming. Dispose checks the flag before
freeing. - Hand ownership over at notify time — change the contract so
callbacks always own the addrinfo (and the event no longer holds
it).
(1) is the minimal change but requires touching every caller.
(2) is more local but adds a flag.
(3) is cleanest but is a contract change.
Environment
- Linux x64, ASAN debug+ZTS build
- libuv from system (Ubuntu 22.04)
- Surfaced by CI run https://github.com/true-async/php-async/actions/runs/25626854837
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
#102: ci: SKIPIF zts for thread_pool/thread_channel; defer cancel-dur…
#111: fix libuv reactor getaddrinfo result leak on cancel
#111: re-trigger CI after php-src error-path fix
#111: pdo_pgsql 029: extend pool-count polling window to 5s
Revert "#111: pdo_pgsql 029: extend pool-count polling window to 5s"
Merge pull request #112 from true-async/111-libuv-reactor-getaddrinfo…
fix: bounded shutdown drain loop for cancelled threadpool requests (#111