Maurice MakaayandMaurice Makaay 030b747616 Fix race condition causing 'ack timeout 4' disconnects (#4)
The AsyncClient::send() methods sets a boolean to true after pushing
data over the TCP socket successfully using tcp_output(). It also sets
a timestamp to remember at what time the data was sent.

The AsyncClient::_sent() callback method reacts to ACKs coming from
the connected client. This method sets the boolean to false.

In the AsyncClient::_poll() method, a check is done to see if the
boolean is true ("I'm waiting for an ACK") and if the time at which
the data was sent is too long ago (5000 ms). If this is the case,
a connection issue with the connected client is assumed and the
connection is forcibly closed by the server.

The race condition is when these operations get mixed up, because
of multithreading behavior. The _sent() method can be called during
the execution of the send() method:

1. send() sends out data using tcp_output()
2. _sent() is called because an ACK is processed, sets boolean to false
3. send() continues and sets boolean to true + timestamp to "now"

After this, the data exchange with the client was successful. Data were
sent and the ACK was seen.
However, the boolean ended up as true, making the _poll() method think
that an ACK is still to be expected. As a result, 5000 ms later, the
connection is dropped.

This commit fixes the code by first registering that an ACK is
expected, before calling tcp_output(). This way, there is no race
condition when the ACK is processed right after that call.

Additionally, I changed the boolean to an integer counter value.
The server might send multiple messages to the client, resulting in
multiple expected ACKs. A boolean does not cover this situation.

Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
2021-04-11 19:09:21 +02:00
2019-10-02 20:01:46 +03:00
2019-10-02 11:51:39 +03:00
2017-09-08 13:07:27 +03:00
2021-04-08 15:34:35 +02:00
2017-09-08 13:04:57 +03:00
2019-10-14 12:46:34 +02:00

AsyncTCP

Build Status Codacy Badge

A fork of the AsyncTCP library by @me-no-dev for ESPHome.

Async TCP Library for ESP32 Arduino

Join the chat at https://gitter.im/me-no-dev/ESPAsyncWebServer

This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-connection network environment for Espressif's ESP32 MCUs.

This library is the base for ESPAsyncWebServer

AsyncClient and AsyncServer

The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use.

S
Description
Async TCP Library for ESP32
Readme
588 KiB
Languages
C++ 99.6%
CMake 0.2%
Makefile 0.2%