Skip to content

Match WiFi module replies by slot and sequence, bound the wait - #1006

Merged
chrisgleissner merged 5 commits into
GideonZ:masterfrom
JC-000:fix/wifi-reply-misroute
Oct 28, 2026
Merged

chrisgleissner merged 5 commits into
GideonZ:masterfrom
JC-000:fix/wifi-reply-misroute

Conversation

@JC-000

@JC-000 JC-000 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1005

Reviewers: @GideonZ (WiFi and UART layer; no area contact is listed for them).

The fix

  • New software/io/wifi/wifi_reply.h: a table of 12 reply slots, separate from the TX buffers. hdr.thread carries the slot, hdr.sequence a per-request sequence. The module already echoes both, so the module and the wire format are unchanged.
  • wifi_rx_isr delivers a reply only if the slot is taken and the sequence matches. Anything else goes to the receive queue as before.
  • wifi_tx_packet no longer registers a waiter (BUFARGS_NOREPLY). The module never answers frames.
  • The late clear in TRANSMIT and wifi_setbaud is gone; the ISR releases the slot.
  • Running out of slots is an error (-1) for the RPCs, not 0, which callers read as success. A timeout also returns -1; the comment in u64_config.cc is updated.
  • A starved TX buffer is an error too: BUFARGS_NOREPLY took the failure value as 0, which the RPCs read as success and wifi_tx_packet as ERR_OK. It now takes the value to return: -1 for the RPCs, pdFALSE for wifi_detect (a success flag), ERR_MEM for wifi_tx_packet. A UDP send over WiFi now fails with ENOMEM instead of reporting success when the buffers are starved (lwIP keeps a TCP segment queued and retries it). The power-on mode, wake-on-WiFi and recovery-server writes no longer retry on a starved buffer; they give up at once.
  • Bounded waits: 10 s per command, 60 s for scan, connect and autoconnect. On a timeout the slot is released under a critical section, a reply that raced in is taken, and the caller returns -1. wifi_detect and wifi_setbaud use the same path, and wifi_detect frees its reply buffer.
  • sequence_nr is now taken under the same critical section as the slot (it was incremented unlocked).

Why this closes the interleavings: a frame can no longer write any slot (A). A reply is accepted only for the sequence its slot was claimed with, and a slot is released by the ISR on delivery or by the owner on timeout (and all slots are cleared when the WiFi command channel is initialised, so a task deleted while it waited cannot be notified later), so a stale or duplicate reply for a reused slot has the wrong sequence and is dropped, and nothing erases a newer owner (B). A timeout releases the slot, so a late reply cannot be taken for the next call and shift the replies (C). A reply that arrives while the owner is abandoning is taken in the drain after the release, so its buffer is freed. The wait is bounded, so a lost reply can no longer hang the caller.

Claiming a slot fails (like running out of TX buffers) if all 12 are in use.

Tests

  • Host: make -C target/pc/linux/wifireply test (also in make host_tests). It covers plain delivery, frames while a command waits, a late reply after abandon with the slot reused, unknown reply, exhaustion and sequence wrap. The same target builds wifi_cmd.cc against stubs of the UART and the RTOS and checks the RPCs and wifi_detect when there is no TX buffer (-1 and pdFALSE, no reply slot used) and that a request without a reply is -1. make legacy models the old table (it is a model, not master's code) and fails 5 checks. Not run by CI.
  • The wifi_cmd host test also checks the reply at three moments: during the wait (returned), after the timeout but before the slot is released (taken by the drain and returned), and after the release (goes to the receive queue, the call is -1). It also checks that every timed-out request is sent with a bounded wait and that all reply slots are reusable afterwards.
  • Hardware RED/GREEN (U64E, core 1.50), using wifi_misroute.py from WiFi module replies can wake the wrong task, and the real caller then waits forever #1005:
  • Builds: make u64_no_esp (size report PASS), make u64ii_no_esp and make -C target/u2plus_L/riscv/ultimate compile and link the application; the u64ii updater step needs the ESP bins and was not run.

Untested

  • C64U / U64 Elite-II and U2+L hardware (they compile and link only), and the u2plus_L updater build.
  • The standard test profile.
  • The 60 s limit for scan and connect against the module's stored-AP ladder, which can take longer with several stored APs out of range: the UI then gets -1 and the module still connects and reports it itself. Degraded, not hung.
  • wifi_tx_packet returning ERR_MEM on a starved buffer (compiled, not exercised: the host test does not link wifi.cc). None of the starved-buffer cases can be triggered on demand on a real device, so they are covered by the host test only.

The first two commits and the last one were written with Claude Opus 5.5, the merge and the fourth commit with Claude Sonnet 5.5, in Claude Code; reviewed by separate sessions.

🤖 Generated with Claude Code

JC-000 and others added 2 commits October 16, 2026 14:40
The reply tag was the number of the TX buffer, which is freed once sent,
long before the reply arrives. Every network frame took a TX buffer
through BUFARGS and registered its sender as the waiter for that number,
though the module never answers frames. A command reply could therefore
wake the task that sent a frame, and the real caller waited forever.

Replies now use a separate table of slots. A slot is released when its
reply is taken or the wait is abandoned, and a reply is delivered only if
its sequence matches the one the slot was claimed with. Frames do not
register. Commands wait at most 10 s (60 s for scan and connect), and
free the reply buffer.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 17, 2026
JC-000 and others added 2 commits October 23, 2026 18:26
Conflicts: Makefile (host_tests list: keep wifireply beside the new recovery
tests) and software/io/wifi/wifi_cmd.h (keep this branch's reply declarations,
drop the extern sequence_nr and tasksWaitingForReply that it removes, keep the
recovery URL declarations from master).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
BUFARGS_NOREPLY returned pdFALSE (0) when the UART had no free TX buffer.
The RPCs answer 0 for success, so every one of them read that as done, and
wifi_tx_packet returned ERR_OK for a frame that was never queued. The macro
now takes the value to return: -1 for the RPCs (as a missing reply slot
already does), pdFALSE for wifi_detect, whose result is a success flag, and
ERR_MEM for wifi_tx_packet.

The host test builds wifi_cmd.cc against stubs of the UART and the RTOS and
checks the RPCs and wifi_detect with no TX buffer, that this uses no reply
slot, and that a request without a reply is still -1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged master (9978476) into this branch; it was conflicting. Two conflicts, both mechanical:

  • Makefile: the host_tests list. Kept wifireply and master's three recovery tests.
  • software/io/wifi/wifi_cmd.h: kept this branch's reply declarations and master's wifi_set/get_recovery_url; dropped the extern sequence_nr / tasksWaitingForReply that this PR removes.

Also added one commit: a starved TX buffer is now a failure. BUFARGS_NOREPLY returned 0 when the UART had no free buffer, which the RPCs read as success and wifi_tx_packet read as ERR_OK for a frame that was never queued. The macro now takes the failure value: -1 for the RPCs (as for a missing reply slot), pdFALSE for wifi_detect, ERR_MEM for wifi_tx_packet. A new host test builds wifi_cmd.cc against UART/RTOS stubs; it fails 16 checks before the change and passes after. Effects: a UDP send over WiFi now fails with ENOMEM instead of reporting success when the buffers are starved (lwIP keeps a TCP segment queued and retries it). The power-on mode, wake-on-WiFi and recovery-server writes no longer retry on a starved buffer; they give up at once. Not run on hardware: I cannot make the UART run out of TX buffers on demand on a real device, so a normal hardware run never reaches this path. The host test is the evidence for it.

Written with Claude Sonnet 5.5 in Claude Code; reviewed adversarially by separate Claude Opus 5.5 sessions before posting.

🤖 Generated with Claude Code

@chrisgleissner chrisgleissner left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @JC-000, for addressing both inline findings in 7ead3d2 and replying in their threads.

Both threads are resolved and I verified the fixes. The host suite, ASan/UBSan checks, and GitHub build pass. The five timeout/drain mutations fail 24/5/6/28/25 checks; the legacy model reports five expected assertion failures without crashing. No code-review finding remains open.

Targeted U64 RED/GREEN passes on core 1.50 / FPGA 126: base 9978476 froze on loaded reconnect 1 (UI silent 60 s, 25 successful REST probes); the PR firmware completed 40 loaded reconnects without a stall. Separate WiFi, Ethernet, simultaneous-interface stress, edge-mode and link-recovery checks passed.

It would be appreciated if you could run a controlled at least 1h WiFi/Ethernet soak test (see tests/soak/README.md and specifically network/connection_test.py for details), just to ensure nothing regressed, and add the high-level results here.

Otherwise, this is ready to merge from my perspective.

… belongs

The no-reply test passed with the slot release removed: once the leaked
slots were used up, the request failed before it was sent, which is -1
too. It now checks that every request is sent, that its wait is bounded
by the command timeout, and that all reply slots can be claimed afterwards.

The notification stub can now deliver, and like FreeRTOS it writes the
notification value even when it returns pdFALSE. The module's reply is
checked at three moments: during the wait, after the wait timed out but
before the slot is released (only the drain after the release takes it),
and after the release. The first two give the caller the reply, not -1,
and free its buffer; the last goes to the receive queue and the call is -1.

The legacy model of the table indexed it with thread 0xFF. It now checks
the thread as the ISR before the fix did (thread < NUM_TX_BUFFERS), so
make legacy fails its 5 checks instead of crashing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrisgleissner

chrisgleissner commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @JC-000,

Could you please perform the soak test mentioned earlier and publish the results? It would be good to merge in past PRs before raising new ones.

Many thanks

@JC-000

JC-000 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Soak results for this PR, as asked: a 2 x 60 min run of tests/soak/network/connection_test.py, Ethernet first, then WiFi.

Setup. Ultimate 64 Elite, core 1.50, firmware 3.15a. Image: this PR's head (7ead3d2) plus #835 (4592eb1) merged on top and nothing else; #835 is there only because it is the UDP change my other bench work needs, and it does not touch the WiFi code. The ESP32 module was master's (V1.6), flashed over a newer bench module for this run. Config reset to defaults before the run; nothing else set.

Suite. --profile soak (concurrent, one runner, readwrite surface), --duration-s 3600 per leg, probes ident, dma, telnet, ftp, http, run from a Mac whose traffic to the device used its wired interface. Suite files unmodified.

Leg Iterations FAIL Ping (1/s, 3660)
Ethernet (10.43.23.81, WiFi associated and idle) 550 0 0 lost
WiFi (10.43.23.83, cable out) 510 0 0 lost, avg 14.8 ms

Latency, p50 / p99 in ms, last summary line:

Probe Ethernet WiFi
ident 10 / 18 18 / 85
dma 29 / 268 74 / 355
ftp 70 / 537 188 / 1622
http 28 / 64 62 / 193
telnet 10277 / 10816 10886 / 11648

The telnet figure is the probe's own pace: its settings operations walk the menu cursor one row at a time, reading the screen back after every key (tests/e2e/lib/browser.py:441-467), and in the logs they take about 10 s each, while the telnet connect and open-menu operations take 0.1 and 0.8 s.

Both legs exited 0, and telnet and ftp accepted a new session right after each run. On the Ethernet leg the audio and video stream monitors received 890,492 and 12,801,759 packets with none lost or reordered. Streams are Ethernet-only in the firmware (data_streamer.cc:144), so the WiFi leg ran without the stream probes.

Two macOS workarounds, both outside the suite. The ping probe runs ping -w 3, which macOS does not accept, so I left it out of --probes and logged ICMP with a separate ping -c 3660 -i 1 per leg (the -w flag may be worth a portability note in the suite). The Mac's multicast route points at a tunnel interface, so the stream monitor saw nothing until I ran the suite through a small wrapper that binds its multicast join to the wired interface.

There is no baseline on master, so this shows no failures and plausible latencies, not a before/after comparison.

Written with Claude Sonnet 5.5 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Thanks @JC-000 , much appreciated!

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

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WiFi module replies can wake the wrong task, and the real caller then waits forever

2 participants