Skip to content

Commit 93a2954

Browse files
committed
Explicitly ignore UDP read errors
1 parent 07ca854 commit 93a2954

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/Query/UdpTransportExecutor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ public function query(Query $query)
174174
// try to read a single data packet from the DNS server
175175
// ignoring any errors, this is uses UDP packets and not a stream of data
176176
$data = @\fread($socket, $max);
177+
if ($data === false) {
178+
return;
179+
}
177180

178181
try {
179182
$response = $parser->parseMessage($data);

tests/Query/UdpTransportExecutorTest.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ public function testQueryRejectsIfSendToServerFailsAfterConnection()
158158
throw $exception;
159159
}
160160

161+
public function testQueryKeepsPendingIfReadFailsBecauseServerRefusesConnection()
162+
{
163+
$socket = null;
164+
$callback = null;
165+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
166+
$loop->expects($this->once())->method('addReadStream')->with($this->callback(function ($ref) use (&$socket) {
167+
$socket = $ref;
168+
return true;
169+
}), $this->callback(function ($ref) use (&$callback) {
170+
$callback = $ref;
171+
return true;
172+
}));
173+
174+
$executor = new UdpTransportExecutor('0.0.0.0', $loop);
175+
176+
$query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
177+
$promise = $executor->query($query);
178+
179+
$this->assertNotNull($socket);
180+
$callback($socket);
181+
182+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
183+
184+
$pending = true;
185+
$promise->then(function () use (&$pending) {
186+
$pending = false;
187+
}, function () use (&$pending) {
188+
$pending = false;
189+
});
190+
191+
$this->assertTrue($pending);
192+
}
193+
161194
/**
162195
* @group internet
163196
*/
@@ -177,27 +210,6 @@ public function testQueryRejectsOnCancellation()
177210
$promise->then(null, $this->expectCallableOnce());
178211
}
179212

180-
public function testQueryKeepsPendingIfServerRejectsNetworkPacket()
181-
{
182-
$loop = Factory::create();
183-
184-
$executor = new UdpTransportExecutor('127.0.0.1:1', $loop);
185-
186-
$query = new Query('google.com', Message::TYPE_A, Message::CLASS_IN);
187-
188-
$wait = true;
189-
$promise = $executor->query($query)->then(
190-
null,
191-
function ($e) use (&$wait) {
192-
$wait = false;
193-
throw $e;
194-
}
195-
);
196-
197-
\Clue\React\Block\sleep(0.2, $loop);
198-
$this->assertTrue($wait);
199-
}
200-
201213
public function testQueryKeepsPendingIfServerSendsInvalidMessage()
202214
{
203215
$loop = Factory::create();

0 commit comments

Comments
 (0)