Skip to content

Commit 9168c1a

Browse files
committed
Fix c-ares timeout when lookup local name
1 parent e6fae2e commit 9168c1a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

include/swoole_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ swoole::Logger *sw_logger();
169169
swoole_set_last_error(__errno); \
170170
if (level >= sw_logger()->get_level()) { \
171171
size_t _sw_error_len = \
172-
sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "(ERRNO %d): " str, __errno, ##__VA_ARGS__); \
172+
sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "(ERRNO %d) " str, __errno, ##__VA_ARGS__); \
173173
sw_logger()->put(level, sw_error, _sw_error_len); \
174174
} \
175175
} while (0)

src/network/dns.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ struct ResolvContext {
348348
ares_options ares_opts;
349349
int ares_flags;
350350
int error;
351+
bool completed;
351352
Coroutine *co;
352353
std::unordered_map<int, network::Socket *> sockets;
353354
std::vector<std::string> result;
@@ -374,6 +375,7 @@ std::vector<std::string> dns_lookup_impl_with_cares(const char *domain, int fami
374375
ResolvContext ctx{};
375376
Coroutine *co = Coroutine::get_current_safe();
376377
ctx.co = co;
378+
ctx.completed = false;
377379
char lookups[] = "fb";
378380
int res;
379381
ctx.ares_opts.lookups = lookups;
@@ -486,11 +488,13 @@ std::vector<std::string> dns_lookup_impl_with_cares(const char *domain, int fami
486488
},
487489
ctx->co);
488490
ctx->co = nullptr;
491+
} else {
492+
ctx->completed = true;
489493
}
490494
},
491495
&ctx);
492496

493-
if (ctx.error) {
497+
if (ctx.error || ctx.completed) {
494498
goto _destroy;
495499
}
496500

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
swoole_coroutine: async dns lookup [5]
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc';
5+
?>
6+
--FILE--
7+
<?php
8+
require __DIR__ . '/../include/bootstrap.php';
9+
10+
use Swoole\Coroutine\System;
11+
use function Swoole\Coroutine\run;
12+
13+
run(function () {
14+
$ip = System::dnsLookup('localhost');
15+
Assert::eq($ip, '127.0.0.1');
16+
});
17+
?>
18+
--EXPECT--

0 commit comments

Comments
 (0)