1
0
mirror of https://github.com/danog/dns.git synced 2024-11-30 04:29:06 +01:00

Set pending request before send

While unlikely, the same request ID could have been selected while waiting to send.
This commit is contained in:
Aaron Piotrowski 2019-01-24 18:16:45 -06:00
parent d26f9bb44f
commit 375bf3f63b
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -48,7 +48,7 @@ abstract class Socket {
/**
* @param string $uri
*
* @return Promise<\Amp\Dns\Server>
* @return Promise<self>
*/
abstract public static function connect(string $uri): Promise;
@ -88,6 +88,7 @@ abstract class Socket {
return;
}
\assert($message instanceof Message);
$id = $message->getId();
// Ignore duplicate and invalid responses.
@ -128,16 +129,6 @@ abstract class Socket {
$id = \random_int(0, 0xffff);
} while (isset($this->pending[$id]));
$message = $this->createMessage($question, $id);
try {
yield $this->send($message);
} catch (StreamException $exception) {
$exception = new DnsException("Sending the request failed", 0, $exception);
$this->error($exception);
throw $exception;
}
$deferred = new Deferred;
$pending = new class {
use Amp\Struct;
@ -150,6 +141,16 @@ abstract class Socket {
$pending->question = $question;
$this->pending[$id] = $pending;
$message = $this->createMessage($question, $id);
try {
yield $this->send($message);
} catch (StreamException $exception) {
$exception = new DnsException("Sending the request failed", 0, $exception);
$this->error($exception);
throw $exception;
}
$this->input->reference();
if (!$this->receiving) {