Skip to content

Commit

Permalink
Prevent UDP sockets from leaking and close sockets on timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Nov 7, 2017
1 parent 4dbc6b1 commit 36ef1a6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/BasicResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ public function query(string $name, int $type): Promise {
$response = yield $socket->ask($question, $this->config->getTimeout());
$this->assertAcceptableResponse($response);

// UDP sockets are never reused, they're not in the $this->sockets map
if ($protocol === "udp") {
// Defer call, because it interferes with the unreference() call in Internal\Socket otherwise
Loop::defer(function () use ($socket) {
$socket->close();
});
}

if ($response->isTruncated()) {
if ($protocol !== "tcp") {
// Retry with TCP, don't count attempt
Expand Down Expand Up @@ -258,6 +266,12 @@ public function query(string $name, int $type): Promise {
return new Record($data, $type, $ttls[$type]);
}, $result[$type]);
} catch (TimeoutException $e) {
// Defer call, because it might interfere with the unreference() call in Internal\Socket otherwise
Loop::defer(function () use ($socket, $uri) {
unset($this->sockets[$uri]);
$socket->close();
});

$i = ++$attempt % \count($nameservers);
$socket = yield $this->getSocket($protocol . "://" . $nameservers[$i]);

Expand Down

0 comments on commit 36ef1a6

Please sign in to comment.