Skip to content

Commit 4fab0e2

Browse files
committed
fix(socket): do not start a connect once a stop has been requested
A stop that arrived while one address was being tried did not end the connect. The loop went on to the remaining addresses and sent each of them a SYN, and when all had failed the manual DNS fallback ran, which does not look at the token and waits up to 2.5 s for each nameserver. Check the token before each connect attempt, before each lookup and before the fallback. A lookup that has already started still cannot be interrupted.
1 parent f96897e commit 4fab0e2

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎src/socket.cppm‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public:
8989
// musl-static build can't — its nameservers live in $PREFIX/etc/resolv.conf
9090
// which libc never reads — so fall back to a manual DNS query there.
9191
auto try_resolved = [&](const char* node, bool numeric) -> bool {
92+
if (stop_.stop_requested()) return false;
9293
struct addrinfo hints{};
9394
hints.ai_family = AF_UNSPEC;
9495
hints.ai_socktype = SOCK_STREAM;
@@ -110,6 +111,7 @@ public:
110111
// Fall back to a manual DNS query when libc can't resolve (Termux:
111112
// nameservers live in $PREFIX/etc/resolv.conf, which libc ignores).
112113
auto try_manual = [&]() -> bool {
114+
if (stop_.stop_requested()) return false;
113115
// DNS must be snappy: a UDP query to a working resolver answers
114116
// in well under a second. Cap it hard (independent of the much
115117
// larger connect timeout) so an intermittently-dropped packet to
@@ -180,6 +182,7 @@ public:
180182
// Connect to the first reachable address in a resolved list.
181183
bool connect_addrinfo(struct addrinfo* result, int timeoutMs) {
182184
for (auto* rp = result; rp != nullptr; rp = rp->ai_next) {
185+
if (stop_.stop_requested()) return false;
183186
SocketHandle fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
184187
if (fd == INVALID_SOCKET_FD) {
185188
continue;

0 commit comments

Comments
 (0)