test: use IP_PORTRANGE_HIGH on FreeBSD for dynamic port allocation #34336

pull w0xlt wants to merge 1 commits into bitcoin:master from w0xlt:freebsd_high_port_range changing 2 files +20 −1
  1. w0xlt commented at 10:32 am on January 19, 2026: contributor

    On FreeBSD, the default ephemeral port range (10000-65535) overlaps with the test framework’s static port range (11000-26000), possibly causing intermittent “address already in use” failures when tests use dynamic port allocation (port=0).

    This PR adds a helper that sets IP_PORTRANGE_HIGH via setsockopt() before binding, requesting ports from 49152-65535 instead, which avoids the overlap, as suggested in #34331 (comment) by @maflcko .

    From FreeBSD’s sys/netinet/in.h:

    0#define IP_PORTRANGE         19
    1#define IP_PORTRANGE_HIGH    1
    2#define IPPORT_EPHEMERALFIRST 10000  /* default range start */
    3#define IPPORT_HIFIRSTAUTO   49152   /* high range start */
    

    See also: FreeBSD https://man.freebsd.org/cgi/man.cgi?query=ip&sektion=4 man page.

    I’ll leave the PR as a draft since I haven’t tested it on FreeBSD yet and would like to gather feedback on the approach.

    Fixes #34331

  2. test: use IP_PORTRANGE_HIGH on FreeBSD for dynamic port allocation
    On FreeBSD, the default ephemeral port range (10000-65535) overlaps
    with the test framework's static port range (11000-26000), causing
    intermittent "address already in use" failures when tests use dynamic
    port allocation (port=0).
    
    Add a helper function that sets the IP_PORTRANGE socket option to
    IP_PORTRANGE_HIGH before binding, which requests ports from the high
    range (49152-65535) instead. This range does not overlap with the
    test framework's static ports.
    
    Constants from FreeBSD's netinet/in.h:
    - IP_PORTRANGE = 19
    - IP_PORTRANGE_HIGH = 1
    
    Fixes: bitcoin/bitcoin#34331
    
    Co-Authored-By: MarcoFalke <*~=\`'#}+{/-|&$^_@721217.xyz>
    d73b792c5b
  3. DrahtBot added the label Tests on Jan 19, 2026
  4. DrahtBot commented at 10:32 am on January 19, 2026: contributor

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

    Code Coverage & Benchmarks

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

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  5. w0xlt marked this as a draft on Jan 19, 2026
  6. hebasto commented at 10:40 am on January 19, 2026: member
    cc @vasild
  7. vasild commented at 2:37 pm on January 19, 2026: contributor
    0    sock.setsockopt(socket.IPPROTO_IP, IP_PORTRANGE, IP_PORTRANGE_HIGH)
    1    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2OSError: [Errno 22] Invalid argument
    

    It is possible to configure the port range system-wide with, e.g.:

    0root# sysctl net.inet.ip.portrange.first=32768
    
  8. w0xlt closed this on Jan 19, 2026

  9. w0xlt deleted the branch on Jan 19, 2026
  10. vasild commented at 3:08 pm on January 19, 2026: contributor

    This works and has the intended effect, from a C++ program:

    0    int val{IP_PORTRANGE_HIGH};
    1    setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &val, sizeof(val));
    
  11. w0xlt restored the branch on Jan 19, 2026
  12. w0xlt commented at 5:47 pm on January 19, 2026: contributor
    Reopening. I’ve now tested it on FreeBSD and it works.
  13. w0xlt commented at 5:56 pm on January 19, 2026: contributor
    The issue was caused by feature_proxy.py using IPv6 sockets. On FreeBSD, IPv6 requires IPV6_PORTRANGE with IPPROTO_IPV6.

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-01-27 06:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me