Skip to content

Guard TCP API writes after Wi-Fi reconnects#10505

Open
Copilot wants to merge 3 commits into
developfrom
copilot/fix-wifi-tcp-api-dead-sockets
Open

Guard TCP API writes after Wi-Fi reconnects#10505
Copilot wants to merge 3 commits into
developfrom
copilot/fix-wifi-tcp-api-dead-sockets

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 19, 2026

🙏 Thank you for sending in a pull request, here's some tips to get started!

  • Problem

    • The TCP API server could keep writing to stale WiFi sockets after reconnects, causing repeated errno 11 (EAGAIN) write failures, noisy logs, and degraded API stability.
  • Change

    • Stream write path
      • Consolidates framed output into a shared writeFrame() path in StreamAPI.
      • Stops draining queued packets as soon as a frame write fails.
    • TCP socket health checks
      • Adds transport-specific pre-write checks in ServerAPI:
        • reject disconnected clients
        • reject non-writable clients when availableForWrite() is supported
      • Closes the client and disables further writes on pre-write failure or short write.
    • Transport compatibility
      • Uses compile-time detection for availableForWrite() so non-TCP / non-WiFi stream types keep existing behavior.
    • Coverage
      • Adds focused unit coverage for the socket write-readiness helper logic.
  • Example

    if (!canWriteFrame(totalLen))
        return false;
    
    size_t written = stream->write(buf, totalLen);
    if (written != totalLen) {
        onFrameWriteFailed(totalLen, written);
        return false;
    }

🤝 Attestations

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.registry.nm1.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt -vvv (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_client_write_checks -vvv (dns block)
  • api.registry.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt -vvv (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_client_write_checks -vvv (dns block)
  • collector.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_mqtt -vvv (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native -f test_client_write_checks -vvv (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix wifi TCP API server writing into dead sockets after reconnect Guard TCP API writes after Wi-Fi reconnects May 19, 2026
Copilot AI requested a review from thebentern May 19, 2026 21:28
@thebentern thebentern marked this pull request as ready for review May 20, 2026 20:51
@thebentern thebentern requested a review from Copilot May 20, 2026 22:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves TCP-based API robustness by preventing StreamAPI/ServerAPI from continuing to write framed protobuf output to stale or non-writable WiFi/TCP clients after reconnects, reducing repeated write failures and log noise.

Changes:

  • Refactors StreamAPI framed output into a shared writeFrame() path and stops draining queued frames after a write failure.
  • Adds ServerAPI transport-specific pre-write checks (disconnected / non-writable) and closes the client on pre-check failure or short writes.
  • Introduces a compile-time availableForWrite() detection helper and adds unit tests for that helper logic.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/mesh/StreamAPI.h Adds frame-write hook points and changes emitTxBuffer() to return success/failure.
src/mesh/StreamAPI.cpp Consolidates framing + write/flush into writeFrame() and aborts queue draining on failed writes.
src/mesh/api/ClientWriteChecks.h New SFINAE-based helpers to detect and query availableForWrite() safely when present.
src/mesh/api/ServerAPI.h Wires ServerAPI into the new StreamAPI frame-write hooks and includes the helper header.
src/mesh/api/ServerAPI.cpp Implements pre-write readiness checks and closes/disables the API service on failures.
test/test_client_write_checks/test_main.cpp Adds unit coverage for availableForWrite() detection and readiness helper behavior.

Comment thread src/mesh/api/ServerAPI.cpp Outdated
Comment thread src/mesh/StreamAPI.cpp Outdated
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 21, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.registry.nm1.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio run -e native (dns block)
  • api.registry.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio test -e native (dns block)
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio run -e native (dns block)
  • collector.platformio.org
    • Triggering command: /home/REDACTED/.local/bin/pio /home/REDACTED/.local/bin/pio run -e native (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@fifieldt fifieldt added the bugfix Pull request that fixes bugs label May 29, 2026
Copilot AI and others added 2 commits May 30, 2026 09:44
@fifieldt fifieldt force-pushed the copilot/fix-wifi-tcp-api-dead-sockets branch from 7e64dfe to 88426a4 Compare May 29, 2026 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Wifi: TCP API server writes into dead sockets after Wi‑Fi reconnect → write(): errno 11 (EAGAIN) spam and API freeze

4 participants