Skip to content

test: Potential test errors, due to assumptions on socket data chunking (interface_http, ...) #36726

Open

Description

@optout21

Summary: A test run with a forced reproducer, with changed socket data send chunking discovered issues with a few tests.

All networking code using sockets must not make assumption about the chunking of the incoming/outgoing data, and should be resilient against any chunking. Normally, the chunking of the sends of the writer dictate the chunking of the receives at the reader, but this is not guaranteed by IP/TCP, intermediate networking elements may change the packet structure. In test using sockets on the same host, this is not very likely, but still possible.

A test run was executed with a stress test reproducer. For every socket send, sending was changed to break up data in small chunks. This forcedly changed chunking of the data stream. This way code was tested for robustness in face of unexpected chunking of the data.
The run is here: #36724 , the reproducer can be seen here .

A few test failed, which where analyzed.

On the positive note, NO NON-TEST ISSUES were identified.

The list of failed tests with the failure root cause:

Failed tests:

  1. interface_http.py -- Failed in several CI runs. A wrong assumption in the test; it is implicitly assumed that an HTTP response arrives in a single recv. Being fixed, test: interface_http: Fix for case when a response doesn't arrive in one recv #36729 .

  2. rpc_setban.py --v2transport Failed in two CI runs (1 2). The reason is more subtle, the tested use case can have two outcomes, depending on weather a full key (64 bytes) could be sent on a closing connection or not (ShouldReconnectV1()). The second case is very unlikely, but still can happen. The test is only prepared for the first outcome, with the reproduces the second can happen, causing it to fail. Up for discussion whether should be fixed or kept as-is.

  3. p2p_v2_encrypted.py -- Failed once, a time-out occurred (on Windows). Also related to v1/v2 transport selection (ShouldReconnectV1()), but the reason is in the test framework (p2p.py, 358-361). The Python v1 peer disconnects after reading 4 bytes. If they are sent in separate syscalls, the sender node will not be able to send all 64 key bytes. This will cause ShouldReconnectV1() to return false, and the node will not reconnect. The test waits on reconnect. A potential fix is to change Python v1 parser to behave like bitcoind by moving the magic check after the full-header length check. Fix is attempted in test: p2p: check v1 magic bytes only after the full header #36730 .

A note regarding chance of occurrence. When a test makes an assumption about data chunking (1.), it can pass reliably in CI runs and local tests. However, this is still a latent bug, as there is no guarantee for correct execution. Data chunking can change between sender and receiver. So the fix is recommended, to make the test more resilient, but the issue is low priority.

Activity

  1. optout21 commented on Nov 2, 2026

    @optout21
    ContributorAuthor

    PR #36729 raised to fix interface_http.

  2. optout21 commented on Nov 2, 2026

    @optout21
    ContributorAuthor

    PR #36730 raised to fix p2p_v2_encrypted.py, with a small change to the test framework p2p v1 peer.

  3. optout21 commented on Nov 5, 2026

    @optout21
    ContributorAuthor

    A similar reproducer run was executed, with receiver-side forced chunking of data: #36751 .

    Test failures identified there, with some CI runs where they failed, are:

    • sock_tests/send_and_receive unit test. A unit test failure prevented execution of the integration tests, so a fix was included in the reproducer PR as well. It still fails in the per-commit checks log.

    • interface_http.py Failed many times. Three logs are included here: 1 2 3.

    • feature_torcontrol.py Failed in two windows tests (1 2)

  4. Pranav-error commented on Nov 12, 2026

    @Pranav-error

    While going through the socket-chunking cases here, I noticed another spot that assumes a recv()-boundary-independent marker count, in a method not covered by #36729: the response-drain loop at the end of check_slow_read_throttle in test/functional/interface_http.py:

    num_res = 0
    while num_res < count + 2:
        response = conn.conn.sock.recv(10 * 1024 * 1024)
        num_res += response.count(b"HTTP/1.1 200")

    response.count(b"HTTP/1.1 200") is evaluated on each individual recv() chunk. If a recv() boundary splits the HTTP/1.1 200 marker across two chunks (e.g. one ends ...HTTP/1., the next starts 1 200...), it is counted in neither, so num_res undercounts and the loop can't reach count + 2, blocking until the socket timeout. A ~10 MB drain is quite likely to span multiple recv() calls, so this looks like the same latent class.

    Counting over the accumulated buffer avoids the split-marker case:

    buf = bytearray()
    num_res = 0
    while num_res < count + 2:
        data = conn.conn.sock.recv(10 * 1024 * 1024)
        if not data:
            break
        buf += data
        num_res = buf.count(b"HTTP/1.1 200")

    Found by inspection; I haven't run it under the forced-chunking reproducer. Happy to open a PR if it's useful and not already covered elsewhere.

  5. optout21 commented on Nov 12, 2026

    @optout21
    ContributorAuthor

    Thanks, I will check. It can be included in #36729, if you agree.

  6. Pranav-error commented on Nov 12, 2026

    @Pranav-error

    Sounds good, please include it in #36729.

    The minimal change is to accumulate into a buffer and count over everything received so far, so a marker split across a recv() boundary is still counted once both halves arrive:

    buf = bytearray()
    num_res = 0
    while num_res < count + 2:
        data = conn.conn.sock.recv(10 * 1024 * 1024)
        if not data:
            break
        buf += data
        num_res = buf.count(b"HTTP/1.1 200")

    Happy to open a separate PR instead if you'd rather keep #36729 focused — whichever is easier for you.

  7. optout21 commented on Nov 18, 2026

    @optout21
    ContributorAuthor

    While going through the socket-chunking cases here, I noticed another spot that assumes a recv()-boundary-independent marker count, in a method not covered by #36729:

    Thanks for looking into this. I looked at it now, and as I see, a fix has been already applied.
    You're right that the original drain loop had the same problem: counting HTTP/1.1 200 in each recv() chunk misses a marker that's split across two chunks. It also spun forever on EOF, since recv() keeps returning b"".

    The fix is already in the PR (and has been with its first version). The last hunk of 568d8f23ed replaces that loop with recv_responses(count + 2). That helper collects into one buffer and frames each response by Content-Length, so it doesn't depend on where recv() splits the data, and it stops when the connection closes. The HTTP/1.1 200 count is then taken over the whole buffer and checked with assert_greater_than_or_equal(num_res, count + 2). So no separate PR is needed, but please do re-check if you wish.

  8. Pranav-error commented on Nov 20, 2026

    @Pranav-error

    Thank you @optout21!

  9. darosior commented on Nov 24, 2026

    @darosior
    Member

    2. rpc_setban.py --v2transport Failed in two CI runs (1 2). The reason is more subtle, [...]

    Ran into this when porting macOS to using poll. On macOS, after node1's FIN, poll() reports POLLHUP and no longer POLLOUT, so node0's socket wait doesn't return a SendEvent. node0 reads EOF without having sent anything, and ShouldReconnectV1() returns false. (cc @theuni)

    The switch to poll() exacerbated this behaviour, but the test is inherently racy, so i think it's worth fixing.

  10. fanquake commented on Nov 25, 2026

    @fanquake
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions