fix: use upstream_addr for routing PUT responses in NAT scenarios #2195
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
test_put_contract_three_hop_returns_responseandtest_three_node_network_connectivitytests were flaky, timing out intermittently on CI. Investigation revealed that PUT responses were failing to route back through intermediate hops in NAT scenarios.Root Cause
The
build_op_resultfunction input.rsusedmsg.target_addr()to determine where to route response messages. However, in NAT scenarios,target_addr()returnsNonebecause peers behind NAT don't know their external addresses - theirPeerKeyLocationhas an unknown socket address.When
target_addrisNone, the response has no destination and silently fails to route, causing the test to time out waiting for a response that will never arrive.Why Tests Were Flaky (Not Always Failing)
The tests simulated NAT scenarios using mock connections where addresses may or may not be fully resolved depending on timing and connection establishment order. When the address happened to be known, the test passed; when unknown, it timed out.
This Solution
The fix is a one-line change: prefer
upstream_addrovermsg.target_addr()for routing:upstream_addris the actual connection address from which we received the message - it's always available when we need to send a response back. This ensures responses route correctly even when the peer's address isn't recorded in the message metadata.Testing
cargo test -p freenet --lib[AI-assisted - Claude]