Skip to content

Commit

Permalink
Fix c-ares timeout when lookup local name
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 9, 2021
1 parent e6fae2e commit 9168c1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/swoole_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ swoole::Logger *sw_logger();
swoole_set_last_error(__errno); \
if (level >= sw_logger()->get_level()) { \
size_t _sw_error_len = \
sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "(ERRNO %d): " str, __errno, ##__VA_ARGS__); \
sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "(ERRNO %d) " str, __errno, ##__VA_ARGS__); \
sw_logger()->put(level, sw_error, _sw_error_len); \
} \
} while (0)
Expand Down
6 changes: 5 additions & 1 deletion src/network/dns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ struct ResolvContext {
ares_options ares_opts;
int ares_flags;
int error;
bool completed;
Coroutine *co;
std::unordered_map<int, network::Socket *> sockets;
std::vector<std::string> result;
Expand All @@ -374,6 +375,7 @@ std::vector<std::string> dns_lookup_impl_with_cares(const char *domain, int fami
ResolvContext ctx{};
Coroutine *co = Coroutine::get_current_safe();
ctx.co = co;
ctx.completed = false;
char lookups[] = "fb";
int res;
ctx.ares_opts.lookups = lookups;
Expand Down Expand Up @@ -486,11 +488,13 @@ std::vector<std::string> dns_lookup_impl_with_cares(const char *domain, int fami
},
ctx->co);
ctx->co = nullptr;
} else {
ctx->completed = true;
}
},
&ctx);

if (ctx.error) {
if (ctx.error || ctx.completed) {
goto _destroy;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/swoole_coroutine/dnslookup_5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
swoole_coroutine: async dns lookup [5]
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;

run(function () {
$ip = System::dnsLookup('localhost');
Assert::eq($ip, '127.0.0.1');
});
?>
--EXPECT--

0 comments on commit 9168c1a

Please sign in to comment.