WIP: RFC: netbase: Remove manual_connection and return the failure reason to callers

bitcoin/bitcoin: PR #36970

1 minor finding · 3 suggestions

Findings

Link

[minor] · Proxy warnings omit the connection failure reason

The prerequisite logging change makes refused proxy connections and missing Unix socket paths produce endpoint-only warnings; their causes require -debug=net, not -debug=proxy or -debug=i2p.

Location: src/netbase.cpp:668 (head).

Details and suggested fix

Both the TCP and Unix-socket branches of Proxy::Connect receive a failure reason, log it under BCLog::NET, and return an empty pointer. ConnectThroughProxy and Session::Hello therefore cannot include the cause in their first-hit warnings. Compared with base, immediate and post-wait connect() errors no longer reach LogInfo; that demotion already occurs in prerequisite 058e26fd, rather than the new error-returning API commit. Return the error from Proxy::Connect, for example using util::Expected<std::unique_ptr<Sock>, std::string>, and include it in the existing LogWarnThenDebug messages. Operators would then receive an actionable initial diagnostic while repeated failures remain debug-only.


Optional improvements

Link

[suggestion] · One proxy's failure suppresses connection warnings for other endpoints

With separately configured proxies, the first endpoint's failure consumes the warning for every endpoint, so a later refusal from another proxy receives only the categorized debug diagnostic.

Location: src/netbase.cpp:799 (head).

Details and suggested fix

ConnectThroughProxy has one LogWarnThenDebug expansion, whose static flag remains set for the process lifetime, including after successful connections. Separate IPv4, IPv6, CJDNS, and onion proxies all use this site. For example, a startup failure of one proxy suppresses the connection warning when an independently configured proxy first fails later. This follows the helper's per-statement convention, but couples notifications for distinct configured services. Key the suppression by proxy endpoint so each service gets an initial visible warning while repeated failures of that endpoint remain debug-only.


Link

[suggestion] · The I2P test does not reliably check repeated Couldn't listen warnings

The second assertion requires a repeated Hello failure but not a repeated Listen failure, so it catches an unconditional Listen warning only if a background retry happens during the addnode interval.

Location: test/functional/p2p_i2p_ports.py:41 (head).

Details and suggested fix

addnode with onetry reaches Session::Connect, which calls CreateIfNotCreatedAlready and Hello, but never Session::Listen. The latter runs independently in ThreadI2PAcceptIncoming, with a one-second initial retry delay that grows after each failure. The second assert_debug_log block can therefore finish without exercising the Couldn't listen site again. Add a separate assertion that waits, with a timeout, for [i2p] i2p: Couldn't listen: Cannot connect to ... while rejecting [warning] i2p: Couldn't listen. Keep the existing addnode assertion for the Hello site.


Link

[suggestion] · The returned getsockopt() failure reason omits the OS error

On the rare getsockopt(SO_ERROR) failure path, the new returned reason omits the OS error even though the existing warn-then-debug diagnostic retains it.

Location: src/netbase.cpp:614 (head).

Details and suggested fix

When GetSockOpt returns SOCKET_ERROR, ConnectToSocket returns only getsockopt() for %s failed. Manual-peer and Tor-control callers log that string with LogInfo, so after the first warning their default-visible diagnostic identifies the failed operation but not its cause. This does not remove an existing diagnostic; it leaves the new caller-facing result incomplete. Capture NetworkErrorString(WSAGetLastError()) before logging and include that captured text in both the existing log and the returned string.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Warn-then-debug logging was introduced for failures likely caused by system configuration problems: they can degrade node functionality and warrant administrator attention, but remote activity can trigger them frequently. The stated policy was to warn the first time the log statement is hit and use categorized debug logging afterward.

This directly explains preserving a non-debug diagnostic while suppressing repeated proxy and I2P failures. It documents statement-level suppression as the helper’s intended behavior, but does not separately establish why endpoint-specific failures or later outage episodes should share that suppression.

Source 1

Tor control connection refusals were historically debug-only when Tor was not running. A later connection-path change caused each failed reconnect to emit an unconditional failure message and two warnings; the historical cleanup proposal described 18 lines per hour on a default node without Tor as log pollution.

Unconditional visibility of every infrastructure connection failure was not an invariant. In particular, an unavailable default Tor control service was treated differently from an actionable failure of an explicitly used service.

Source 1

The Tor logging discussion considered retaining more visible failure reasons for explicitly configured Tor, versus warning once and relegating retries to debug. The author’s final reported revision selected LogWarnThenDebug and restored manual_connection=false.

This is a concrete historical resolution favoring an initial visible warning over unconditional diagnostics on every retry. It supports distinguishing initial operator notification from repetitive retry detail, without proving that the same policy was explicitly chosen for every proxy endpoint or I2P session.

Source 1 · Source 2 · Source 3 · Source 4

Ralph's inferences

The pinned base preserves non-debug visibility for immediate connect() errors and errors obtained after waiting when manual_connection is true; Proxy::Connect also passes true. This is not a blanket policy for all failure reasons: timeouts, wait failures, socket-creation failures, and unsupported destination addresses remain debug-only, while getsockopt failure has its own warn-then-debug diagnostic.

The historically implemented visibility boundary is narrower than 'all manual or proxy connection failures are visible.' Moving error reporting to callers changes that boundary when callers log every returned reason, including timeouts. The inspected code establishes the distinction, but the original rationale for each exception was not retrieved.

The helper implements process-lifetime suppression per macro expansion using a static atomic flag. It does not key suppression by endpoint, session object, error reason, or recovery episode, and successful connections do not reset that flag.

Applied at the proposed proxy and SAM warning sites, this means different endpoints and I2P sessions share the first warning at each site. Separate sites retain independent flags—for example, the I2P listening diagnostic has its own suppression state. This matches the documented 'first time the log line is hit' mechanism, but its suitability across endpoints and recovery episodes remains an extrapolation rather than an explicitly documented historical decision.

Source 1