net: Add interrupt to pcp retry loop #33338

pull sedited wants to merge 1 commits into bitcoin:master from sedited:pcp_interrupt changing 5 files +36 −24
  1. sedited commented at 10:20 AM on September 8, 2025: contributor

    Without this interrupt bitcoind takes a long time to exit if requested to do so after a failed pcp lookup on startup.

  2. net: Add interrupt to pcp retry loop
    Without this interrupt bitcoind takes a long time to exit if requested
    to do so after a failed pcp lookup on startup.
    188de70c86
  3. DrahtBot added the label P2P on Sep 8, 2025
  4. DrahtBot commented at 10:20 AM on September 8, 2025: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33338.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK fjahr, hodlinator, achow101

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. sedited commented at 10:21 AM on September 8, 2025: contributor

    Logs:

    <details> <summary>Master logs when mapping fails (Click to expand)</summary>

    2025-09-08T10:02:52Z msghand thread start
    2025-09-08T10:02:52Z Imported mempool transactions from file: 3 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
    2025-09-08T10:02:52Z initload thread exit
    2025-09-08T10:02:52Z [net:warning] pcp: Could not receive response: Connection refused (111)
    2025-09-08T10:02:52Z [net] portmap: gateway [IPv6]: fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:02:52Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:4bfd:91da:7cba:67cb port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:02:52Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:4bfd:91da:7cba:67cb
    2025-09-08T10:02:53Z [net] trying v2 connection [2001:e60:1154:ccd9:3515:d56c:bb7:5f4c]:38333 lastseen=58.3hrs
    2025-09-08T10:02:53Z [net] pcp: Timeout
    2025-09-08T10:02:53Z [net] pcp: Retrying (1)
    2025-09-08T10:02:54Z [net] pcp: Timeout
    2025-09-08T10:02:54Z [net] pcp: Retrying (2)
    ^C2025-09-08T10:02:54Z tor: Thread interrupt
    2025-09-08T10:02:54Z Shutdown in progress...
    2025-09-08T10:02:54Z torcontrol thread exit
    2025-09-08T10:02:54Z dnsseed thread exit
    2025-09-08T10:02:54Z addcon thread exit
    2025-09-08T10:02:54Z net thread exit
    2025-09-08T10:02:55Z msghand thread exit
    2025-09-08T10:02:55Z [net] pcp: Timeout
    2025-09-08T10:02:55Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:02:55Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:1386:bc1d:7d89:306d port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:02:55Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:1386:bc1d:7d89:306d
    2025-09-08T10:02:56Z [net] pcp: Timeout
    2025-09-08T10:02:56Z [net] pcp: Retrying (1)
    2025-09-08T10:02:57Z [net] pcp: Timeout
    2025-09-08T10:02:57Z [net] pcp: Retrying (2)
    2025-09-08T10:02:58Z [net] connection attempt to [2001:e60:1154:ccd9:3515:d56c:bb7:5f4c]:38333 timed out
    2025-09-08T10:02:58Z opencon thread exit
    2025-09-08T10:02:58Z [net] pcp: Timeout
    2025-09-08T10:02:58Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:02:58Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:17ff:1c49:2591:5195 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:02:58Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:17ff:1c49:2591:5195
    2025-09-08T10:02:59Z [net] pcp: Timeout
    2025-09-08T10:02:59Z [net] pcp: Retrying (1)
    2025-09-08T10:03:00Z [net] pcp: Timeout
    2025-09-08T10:03:00Z [net] pcp: Retrying (2)
    2025-09-08T10:03:01Z [net] pcp: Timeout
    2025-09-08T10:03:01Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:01Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:5e43:d231:cd0b:5424 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:01Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:5e43:d231:cd0b:5424
    2025-09-08T10:03:02Z [net] pcp: Timeout
    2025-09-08T10:03:02Z [net] pcp: Retrying (1)
    2025-09-08T10:03:03Z [net] pcp: Timeout
    2025-09-08T10:03:03Z [net] pcp: Retrying (2)
    2025-09-08T10:03:04Z [net] pcp: Timeout
    2025-09-08T10:03:04Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:eb22:5bda:fe93:efb3 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:eb22:5bda:fe93:efb3
    2025-09-08T10:03:05Z [net] pcp: Timeout
    2025-09-08T10:03:05Z [net] pcp: Retrying (1)
    2025-09-08T10:03:06Z [net] pcp: Timeout
    2025-09-08T10:03:06Z [net] pcp: Retrying (2)
    2025-09-08T10:03:07Z [net] pcp: Timeout
    2025-09-08T10:03:07Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:07Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:ad0f:260a:2c63:7494 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:07Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:ad0f:260a:2c63:7494
    2025-09-08T10:03:08Z [net] pcp: Timeout
    2025-09-08T10:03:08Z [net] pcp: Retrying (1)
    2025-09-08T10:03:09Z [net] pcp: Timeout
    2025-09-08T10:03:09Z [net] pcp: Retrying (2)
    2025-09-08T10:03:10Z [net] pcp: Timeout
    2025-09-08T10:03:10Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:10Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:65cf:20cb:5921:45bf port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:10Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:65cf:20cb:5921:45bf
    2025-09-08T10:03:11Z [net] pcp: Timeout
    2025-09-08T10:03:11Z [net] pcp: Retrying (1)
    2025-09-08T10:03:12Z [net] pcp: Timeout
    2025-09-08T10:03:12Z [net] pcp: Retrying (2)
    2025-09-08T10:03:13Z [net] pcp: Timeout
    2025-09-08T10:03:13Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:13Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:5f6b:1db9:7ba0:44a port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:13Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:5f6b:1db9:7ba0:44a
    2025-09-08T10:03:14Z [net] pcp: Timeout
    2025-09-08T10:03:14Z [net] pcp: Retrying (1)
    2025-09-08T10:03:15Z [net] pcp: Timeout
    2025-09-08T10:03:15Z [net] pcp: Retrying (2)
    2025-09-08T10:03:16Z [net] pcp: Timeout
    2025-09-08T10:03:16Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:16Z [net] pcp: Requesting port mapping for addr 1111:1111::1 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:03:16Z [net] pcp: Internal address after connect: 1111:1111::1
    2025-09-08T10:03:17Z [net] pcp: Timeout
    2025-09-08T10:03:17Z [net] pcp: Retrying (1)
    2025-09-08T10:03:18Z [net] pcp: Timeout
    2025-09-08T10:03:18Z [net] pcp: Retrying (2)
    2025-09-08T10:03:19Z [net] pcp: Timeout
    2025-09-08T10:03:19Z [net] pcp: Giving up after 3 tries
    2025-09-08T10:03:19Z mapport thread exit
    2025-09-08T10:03:19Z [net] Flushed 13682 addresses to peers.dat  22ms
    2025-09-08T10:03:19Z DumpAnchors: Flush 0 outbound block-relay-only peer addresses to anchors.dat started
    2025-09-08T10:03:19Z DumpAnchors: Flush 0 outbound block-relay-only peer addresses to anchors.dat completed (0.01s)
    2025-09-08T10:03:19Z scheduler thread exit
    2025-09-08T10:03:19Z Writing 3 mempool transactions to file...
    2025-09-08T10:03:19Z Writing 0 unbroadcast transactions to file.
    2025-09-08T10:03:19Z Dumped mempool: 0.000s to copy, 0.008s to dump, 1011 bytes dumped to file
    2025-09-08T10:03:19Z Flushed fee estimates to fee_estimates.dat.
    2025-09-08T10:03:19Z Shutdown done
    

    </details>

    <details> <summary>This PR's logs when mapping fails (Click to expand)</summary>

    2025-09-08T10:07:03Z msghand thread start
    2025-09-08T10:07:03Z addcon thread start
    2025-09-08T10:07:03Z initload thread exit
    2025-09-08T10:07:03Z [net:warning] pcp: Could not receive response: Connection refused (111)
    2025-09-08T10:07:03Z [net] portmap: gateway [IPv6]: fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:03Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:4bfd:91da:7cba:67cb port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:03Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:4bfd:91da:7cba:67cb
    ^C2025-09-08T10:07:03Z tor: Thread interrupt
    2025-09-08T10:07:03Z Shutdown in progress...
    2025-09-08T10:07:03Z torcontrol thread exit
    2025-09-08T10:07:03Z addcon thread exit
    2025-09-08T10:07:03Z opencon thread exit
    2025-09-08T10:07:03Z dnsseed thread exit
    2025-09-08T10:07:03Z net thread exit
    2025-09-08T10:07:03Z msghand thread exit
    2025-09-08T10:07:04Z [net] pcp: Timeout
    2025-09-08T10:07:04Z [net] pcp: Retrying (1)
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:1386:bc1d:7d89:306d port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:1386:bc1d:7d89:306d
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:17ff:1c49:2591:5195 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:17ff:1c49:2591:5195
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:5e43:d231:cd0b:5424 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:5e43:d231:cd0b:5424
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:eb22:5bda:fe93:efb3 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:eb22:5bda:fe93:efb3
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:ad0f:260a:2c63:7494 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:ad0f:260a:2c63:7494
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:65cf:20cb:5921:45bf port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:65cf:20cb:5921:45bf
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 2a02:1210:4a25:2f00:5f6b:1db9:7ba0:44a port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 2a02:1210:4a25:2f00:5f6b:1db9:7ba0:44a
    2025-09-08T10:07:04Z [net] pcp: Requesting port mapping for addr 1111:1111::1 port 38333 from gateway fe80::a6ce:daff:fe8f:12a0%4
    2025-09-08T10:07:04Z [net] pcp: Internal address after connect: 1111:1111::1
    2025-09-08T10:07:04Z mapport thread exit
    2025-09-08T10:07:04Z [net] Flushed 13692 addresses to peers.dat  20ms
    2025-09-08T10:07:04Z DumpAnchors: Flush 0 outbound block-relay-only peer addresses to anchors.dat started
    2025-09-08T10:07:04Z DumpAnchors: Flush 0 outbound block-relay-only peer addresses to anchors.dat completed (0.01s)
    2025-09-08T10:07:04Z scheduler thread exit
    2025-09-08T10:07:04Z Writing 3 mempool transactions to file...
    2025-09-08T10:07:04Z Writing 0 unbroadcast transactions to file.
    2025-09-08T10:07:04Z Dumped mempool: 0.000s to copy, 0.006s to dump, 1011 bytes dumped to file
    2025-09-08T10:07:04Z Flushed fee estimates to fee_estimates.dat.
    2025-09-08T10:07:04Z Shutdown done
    

    </details>

  6. fanquake added this to the milestone 30.0 on Sep 8, 2025
  7. in src/common/pcp.cpp:222 in 188de70c86
     217 | @@ -217,7 +218,8 @@ CNetAddr PCPUnwrapAddress(std::span<const uint8_t> wrapped_addr)
     218 |  //! PCP or NAT-PMP send-receive loop.
     219 |  std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &protocol, std::span<const uint8_t> request, int num_tries,
     220 |          std::chrono::milliseconds timeout_per_try,
     221 | -        std::function<bool(std::span<const uint8_t>)> check_packet)
     222 | +        std::function<bool(std::span<const uint8_t>)> check_packet,
     223 | +        CThreadInterrupt& interrupt)
    


    hodlinator commented at 1:04 PM on September 8, 2025:

    nit: Would prefer keeping the lambda as the last parameter and also avoiding to mix &-placement within the same argument list.

            CThreadInterrupt &interrupt,
            std::function<bool(std::span<const uint8_t>)> check_packet)
    

    fjahr commented at 7:29 PM on September 8, 2025:

    CThreadInterrupt& interrupt is our preferred style though (see PointerAlignment: Left in src/.clang-format), so maybe we rather want to fix the other parameter to keep it consistent.

  8. in src/common/pcp.cpp:243 in 188de70c86
     239 | @@ -238,6 +240,7 @@ std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &p
     240 |          auto cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now());
     241 |          auto deadline = cur_time + timeout_per_try;
     242 |          while ((cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now())) < deadline) {
     243 | +            if (interrupt) return std::nullopt;
    


    fjahr commented at 2:51 PM on September 8, 2025:

    nit: comment above lists all the conditions for the wait to exit so it should list interrupt as well.

  9. in src/test/pcp_tests.cpp:18 in 188de70c86
      14 | @@ -15,6 +15,8 @@
      15 |  
      16 |  using namespace std::literals;
      17 |  
      18 | +static CThreadInterrupt g_interrupt;
    


    fjahr commented at 2:52 PM on September 8, 2025:

    nit: Could add a simple test where the interrupt is instantly called to make sure the line is covered somehow.

  10. fjahr commented at 2:54 PM on September 8, 2025: contributor

    utACK 188de70c86414b8b2ad5143f5c607b67686526ea

    Looks good to me, happy to re-review if you decide to address the nits.

  11. hodlinator approved
  12. hodlinator commented at 7:15 PM on September 8, 2025: contributor

    utACK 188de70c86414b8b2ad5143f5c607b67686526ea

    Change just passes down interrupt for checking in inner loops.


    Didn't figure out a way of reproducing the behavior PR-author describes in #33338 (comment).

    • Wasn't able to bring to (zombie) life a local IPv6 gateway.

    • Only have one ethernet interface on my local machine. Seems the author may have a handful.

    • Reproduced something similar anyway through hacking this in:

      diff --git a/src/common/pcp.cpp b/src/common/pcp.cpp
      index ed6624cb89..33b4c653fa 100644
      --- a/src/common/pcp.cpp
      +++ b/src/common/pcp.cpp
      @@ -246,9 +246,11 @@ std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &p
                       LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "%s: Could not wait on socket: %s\n", protocol, NetworkErrorString(WSAGetLastError()));
                       return std::nullopt; // Network-level error, probably no use retrying.
                   }
      -            if (!occurred) {
      +            std::this_thread::sleep_for(3s);
      +            //if (!occurred) {
                       LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Timeout\n", protocol);
                       break; // Retry.
      +            /*
                   }
      
                   // Receive response.
      @@ -263,6 +265,7 @@ std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &p
                       got_response = true; // Got expected response, break from receive loop as well as from retry loop.
                       break;
                   }
      +            */
               }
           }
           if (!got_response) {
      

      ...and testing Ctrl+C with/without interrupt/PR.

  13. achow101 commented at 8:29 PM on September 8, 2025: member

    ACK 188de70c86414b8b2ad5143f5c607b67686526ea

  14. achow101 merged this on Sep 8, 2025
  15. achow101 closed this on Sep 8, 2025

  16. alexanderwiederin referenced this in commit 49e068b15b on Sep 16, 2025
  17. alexanderwiederin referenced this in commit 4b0c2f2a8f on Sep 17, 2025
  18. alexanderwiederin referenced this in commit 2edb618ffe on Sep 17, 2025
  19. stringintech referenced this in commit fb8510ba20 on Sep 17, 2025
  20. luke-jr referenced this in commit 5d10d86de1 on Sep 19, 2025
  21. bug-castercv502 referenced this in commit 9f5d12d027 on Sep 28, 2025
  22. yuvicc referenced this in commit d89c6d0002 on Sep 28, 2025
  23. stickies-v referenced this in commit a785fe6759 on Nov 5, 2025
  24. Fabcien referenced this in commit 37cd4bee71 on Apr 9, 2026
  25. roqqit referenced this in commit 28aa39fe94 on Jun 11, 2026
  26. Kino1994 referenced this in commit e6f27cd73a on Jun 28, 2026
  27. BigcoinBGC referenced this in commit 4fb480303c on Jun 30, 2026
  28. Kino1994 referenced this in commit 224ec01942 on Aug 19, 2026
  29. bitcoin locked this on Sep 8, 2026
Labels

Milestone
30.0


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-09-09 07:56 UTC