Bug? probable rESP8266WiFi lib error: Silent data loss fix

peacemaker

Expert
Licensed User
Longtime User
Hi, all and @Erel,

Server of my sensors shows an error of packet receiving, very rarely (maybe one per a week). It's JSON-text, rather long and it is being sent by chunked POST HTTP requests, chunk by chunk.
And error log shows that some small piece of JSON-text _in_the_middle_ is ... lost. So, JSON is mis-formatted.

AI agent suddenly told that there is an error in the rESP8266WiFi lib at some packet size.
I'm not sure yet, just re-compiling and tracking the sensors work - it's long test, but maybe it's interesting to re-check in advance.

The lib v.1.60 source of Erel is https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.76644/#post-999092.
I use my tiny modified version v.1.61 https://www.b4x.com/android/forum/t...ethod-with-connection-timeout.168470/#content that the same, changes only in BufferedWiFiClient.cpp file (that can be pulled into 1.60 also).

Fixed file is with "Silent data loss fix" remark.
Now the compilation is OK and i'm testing v.1.62.

Sorry if any false-positive worry from my side - i'm almost zero at C and C++. Just vibecoding now :)

Please, welcome to audit this fix - maybe it's really useful founding of the AI.

## What exactly has been fixed

1. `flush()` no longer **resets the `index` silently**: bytes are deleted from the buffer only after
the wrapped client reported that they were gone (`sent > 0`), with partial sending, the remainder is shifted
`memmove' and will be repeated later.
2. `write()` no longer **reports success if buffer reset failed**: if buffer is not released
it turned out, `0` is returned. This is enough for the chain to
`B4RStream::WriteBytes → AsyncStreams::Write2 → AStream_Error → HttpJob.HttpErrorsCounter`
I saw a short recording and raised a sending error (the recording will be saved in write-ahead and repeated).
3. The dead line of the original `total += index` ** has been removed after ** `flush()` (there is already `index == 0`).
In addition, returning "flushed + size" would be a violation of the contract `Print::write`:
`B4RStream::writeBytes` subtracts the return from the remainder of `Length` — a number greater than `Length` would give
overflow of an unsigned counter. Now `write()` never returns more than it was given.
4. Added `wrappedClient == 0` protections (calling before `setClient`) and clipping the "over-count" from the wrapped
the client. Unnecessary retry cycles **not** added: ESP32-the client locks itself to its own
send-timeout, multiplying it would be dangerous for watchdog.
5. `stop()`: What could not be sent before the socket was closed is still lost (the socket is closed),
but now it is clearly marked — when `B4R_WIFI_BUFFER_DEBUG` is enabled, it is printed in Serial.
 

Attachments

Last edited:
For now during tests the transmission errors are started to be trapped, and the log started to annoy by the alerts :)
Before, i did not know so detailed about the connection quality...
 
Back
Top