Skip to content

testing/nettest: Add a TCP case for sends that time out on I/O buffers. - #4099

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:iob-leak-up
Nov 23, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:iob-leak-up

Conversation

@casaroli

@casaroli casaroli commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A new case of cmocka_net_tcp, test_tcp_sndtimeo_iob: a TCP pair over 127.0.0.1 whose reader does not read, and a sender with SO_SNDTIMEO 100 ms. The sends fill the write buffers, then 10 sends time out in the wait for an I/O buffer. After both ends close, the free I/O buffers in /proc/iobinfo must come back to the number before the case (within 3 s).

On NuttX master each such timeout keeps one I/O buffer on the committed list; apache/nuttx#22129 fixes it.

The case is built with CONFIG_NET_IPv4, CONFIG_NET_SOCKOPTS and CONFIG_NET_TCP_WRITE_BUFFERS. The case sets SO_SNDBUF above what it sends (a stack without SO_SNDBUF gives ENOPROTOOPT and has no send limit), so that the sends wait for an I/O buffer and not for the send buffer. It is skipped when the sends do not take the free I/O buffers down to CONFIG_IOB_THROTTLE, without /proc/iobinfo, and when a send fails with ENOMEM: with a fixed pool of devif callbacks, the connections of earlier cases in TIME_WAIT can keep the callbacks that the sender needs.

Impact

Only testing/nettest: one new source file and its lines in Makefile and CMakeLists.txt. No change to other cases.

Testing

qemu-armv8a:knsh under HVF, 96 IOBs of 196 bytes, IOB_THROTTLE 8, TCP write buffers:

NuttX master with apache/nuttx#22129
test_tcp_sndtimeo_iob, first run after boot FAIL: 61 free, 72 before PASS
the same, second run of cmocka_net_tcp in the same boot SKIPPED (ENOMEM) SKIPPED (ENOMEM)
knsh, 4 CPUs PASS
flat qemu-armv8a:nsh FAIL: 85 free, 96 before PASS
knsh + CONFIG_NET_SEND_BUFSIZE=8192 FAIL: 61 free, 72 before PASS

Builds in the NuttX CI image with -Werror, with apache/nuttx#22129, all OK with 0 warnings: qemu-armv8a:netnsh with the case (Make and CMake), sim:tcpblaster with the case, qemu-armv8a:netnsh without TCP write buffers (the case is not built). The knsh images of the runs above build the case at make import (-Wall, 0 warnings).

tools/checkpatch.sh -c -u -m -g passes.

@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Testing Claude review ;-)

Summary of PR #4099 – testing/nettest: TCP SO_SNDTIMEO / IOB leak test
(apache/nuttx-apps, draft, author: casaroli, 1 approval of 2 required)

Adds test_tcp_sndtimeo_iob to cmocka_net_tcp (one commit, 5 files, +301/-0).
A loopback TCP connection is created whose reader never reads; the sender
sets SO_SNDTIMEO=100 ms and sends 4 KiB blocks until 10 sends fail with
EAGAIN. After all sockets close, the test requires the free IOB count in
/proc/iobinfo to return to its starting value (polled up to 3 s).
Skipped without /proc/iobinfo or when send() returns ENOMEM. Built only with
CONFIG_NET_IPv4 && CONFIG_NET_SOCKOPTS && CONFIG_NET_TCP_WRITE_BUFFERS.

It is the regression test for apache/nuttx#22129 ("mm/iob: Undo the waiter
count of an IOB wait that ends without an IOB"): per the author, master
leaks one IOB per timed-out send. Reported testing: fails on master and
passes with the fix on qemu-armv8a knsh (61/72 free) and flat nsh (85/96
free); -Werror builds clean; checkpatch passes. nuttx#22129 must be merged
first, otherwise this test fails in CI.

Open review notes:

  1. Build guards: Makefile/CMake require IPv4+SOCKOPTS+TCP_WRITE_BUFFERS, but
    test_tcp.c/.h guard only on CONFIG_NET_TCP_WRITE_BUFFERS inside the outer
    #if - please confirm the outer condition matches, otherwise some configs
    will fail to link.
  2. With CONFIG_NET_SEND_BUFSIZE > 0 the sends may time out on the SO_SNDBUF
    wait rather than the IOB wait, so the leak path may not be exercised.
  3. Close order: the sender (fds[1]) is closed before the peer holding unread
    data (fds[2]); closing fds[2] first would reset the connection right away
    and make buffer recovery more deterministic.
  4. With a large IOB pool, 2000 x 4 KiB may never reach 10 timeouts and the
    test fails instead of skipping (tested pools were 72/96 IOBs).
  5. Nits: ntotal parsed but unused (%*d); skipping on every ENOMEM could hide
    a real regression that returns ENOMEM.

Overall: clean, small test with correct setup/teardown on skip paths and a
convincing fail-before/pass-after result. Good to merge after nuttx#22129,
once the guard question (1) is answered.

@casaroli

Copy link
Copy Markdown
Contributor Author
  1. The guards match: in test_tcp.c and test_tcp.h the case is inside #ifdef CONFIG_NET_IPv4 and #ifdef CONFIG_NET_SOCKOPTS, as in the Makefile and CMakeLists.txt.

  2. and 4. Done: with CONFIG_NET_SEND_BUFSIZE > 0 the case sets SO_SNDBUF above what it sends, and it skips when the sends do not take the free IOBs down to CONFIG_IOB_THROTTLE.

  3. The order does not change the result: the lost IOBs are on the committed list of the pool, not in the connection.

  4. Done: ntotal is gone. A send that fails with ENOMEM does not wait for an IOB, so the case cannot test that wait.

@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Re-review of #4099 (f6f2364 -> 9b4a934, rebased on master)

testing/nettest: test_tcp_sndtimeo_iob – regression test for
apache/nuttx#22129 (IOB leaked per SO_SNDTIMEO timeout while waiting for an
IOB). Loopback pair, reader never reads, 100 ms SO_SNDTIMEO; after 10
timeouts and close, free IOBs in /proc/iobinfo must return to the pre-test
value within 3 s.

Changes in this push:

  • SO_SNDBUF raised to TEST_MAXSENDS * TEST_SENDSIZE (ENOPROTOOPT accepted),
    so sends wait for an IOB, not for the send buffer.
  • Instead of asserting 10 timeouts, the case skips when fewer than 10
    timeouts occur or nfree > CONFIG_IOB_THROTTLE (large pool / send limit).
  • ntotal dropped (sscanf "%*d %d").

Checked: registration in test_tcp.c is nested under CONFIG_NET_IPv4 ->
CONFIG_NET_SOCKOPTS -> CONFIG_NET_TCP_WRITE_BUFFERS, matching Makefile and
CMakeLists.txt.

Remaining notes (non-blocking):

  1. More skip paths now: a regression that keeps sends from reaching the IOB
    wait shows up as a skip, not a failure - CI logs should show the case ran.
  2. Close order: the sender (fds[1]) is closed before the non-reading peer
    (fds[2]); closing fds[2] first would reset at once and make IOB recovery
    more deterministic.
  3. A second run in the same boot is skipped (ENOMEM, TIME_WAIT callbacks);
    worth a note in the test.
  4. Nit: loop counter i reused as the SO_SNDBUF value; a separate int would
    read better.

Verdict: LGTM once apache/nuttx#22129 is merged.

test_tcp_sndtimeo_iob: a loopback connection whose reader does not
read, and a sender with SO_SNDTIMEO.  The sends fill the write buffers,
then 10 sends time out in the wait for an I/O buffer.  After both ends
close, the free I/O buffers in /proc/iobinfo must come back to the
number before the test.  The case is skipped without /proc/iobinfo,
or when a send fails with ENOMEM (no free devif callback: connections
of earlier cases in TIME_WAIT can keep theirs).

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@raiden00pl

Copy link
Copy Markdown
Member

@cederom Pro tip: tell Claude not to be so verbose, so that a human can actually read it :P LLMs are very wordy by default, and it’s unreadable for human

@casaroli

Copy link
Copy Markdown
Contributor Author
  1. The note is in the case, at the ENOMEM skip in the send loop: connections of earlier cases in TIME_WAIT can keep the devif callbacks.

  2. Done.

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change: the SO_SNDBUF value now has its own int sndbuf instead of reusing the loop counter i. That fixes the nit. Nothing else changed.

These are still open and don't block merging:

  • The close order: the sender is closed before the peer that never reads.
  • There are more skip paths, so CI logs should show whether the case actually ran.
  • A second run in the same boot is skipped because of ENOMEM.

@casaroli
casaroli marked this pull request as ready for review November 22, 2026 16:55
@xiaoxiang781216
xiaoxiang781216 merged commit 95c5d82 into apache:master Nov 23, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants