Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP-32 panic when trying to connect to the old socket #46

Closed
AlexanderWingard opened this issue Oct 16, 2019 · 9 comments
Closed

ESP-32 panic when trying to connect to the old socket #46

AlexanderWingard opened this issue Oct 16, 2019 · 9 comments
Assignees
Labels
possible-bug Something isn't working, and I think it is a bug
Projects

Comments

@AlexanderWingard
Copy link

Hi!

First of all, thanks for this lovely library!

I have found something that is similar to #25

I use https://github.com/websockets/wscat for my development. And start up a server.

The first time, client.connect works.
Also, if I never start the server, client.connect does not crash and I can stay in a client.available loop.

However, if I start the board, it successfully connects to the server. Then reboot the board (without wscat noticing) and it tries to connect to wscat that still thinks it's connected to the previous boot. Then the board crashes.

I have extracted the stacktrace:

PC: 0x400d2e18: WiFiClientRxBuffer::read(unsigned char*, unsigned int) at /home/lex/.arduino15/packages/esp32/hardware/esp32/1.0.4-rc1/libraries/WiFi/src/WiFiClient.cpp line 107
EXCVADDR: 0x00000008

Decoding stack results
0x400d2e18: WiFiClientRxBuffer::read(unsigned char*, unsigned int) at /home/lex/.arduino15/packages/esp32/hardware/esp32/1.0.4-rc1/libraries/WiFi/src/WiFiClient.cpp line 107
0x400d2ee9: WiFiClient::read(unsigned char*, unsigned int) at /home/lex/.arduino15/packages/esp32/hardware/esp32/1.0.4-rc1/libraries/WiFi/src/WiFiClient.cpp line 434
0x4015fcea: WiFiClient::read() at /home/lex/.arduino15/packages/esp32/hardware/esp32/1.0.4-rc1/libraries/WiFi/src/WiFiClient.cpp line 345
0x400e32af: websockets::network::GenericEspTcpClient ::readLine() at /home/lex/Arduino/libraries/ArduinoWebsockets/src/tiny_websockets/network/generic_esp/generic_esp_clients.hpp line 46
0x400e2fce: websockets::WebsocketsClient::connect(String, int, String) at /home/lex/Arduino/libraries/ArduinoWebsockets/src/websockets_client.cpp line 279
0x400d1ac5: loop() at /home/lex/Arduino/sketch_oct15a/sketch_oct15a.ino line 95
0x400e870d: loopTask(void*) at /home/lex/.arduino15/packages/esp32/hardware/esp32/1.0.4-rc1/cores/esp32/main.cpp line 19
0x40088dbd: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Board: Huzzah 32
Websockets: 0.4.11
Wscat: 2.2.1

Don't really know why it behaves differently when trying to connect to a non open port vs a port that I guess "thinks" it's open.

This will probably not cause any issue for me during production when I run with a real server but I would like to contribute to making this library rock-solid in any way I can.

Cheers!

@gilmaimon
Copy link
Owner

Hi Alexander,
First of all, I really appreciate the helpful attitude, thanks!

That's really interesting... I wonder if it is because wscat returns some weird answer in that case or if its a problem with the esp32 or library impl itself.

I will look into it. Thank you for the detailed issue.
Gil.

@gilmaimon gilmaimon added the possible-bug Something isn't working, and I think it is a bug label Oct 17, 2019
@gilmaimon gilmaimon added this to To do in Bugfixes via automation Oct 17, 2019
@gilmaimon gilmaimon moved this from To do to Up Next in Bugfixes Oct 19, 2019
@gilmaimon
Copy link
Owner

So I ran few tests:

  • The crash seems to be in esp32's function WiFiClient.read() (without any params, which should read one byte).
  • The same scenario with another server impl does not crash the esp

I will keep you updated if I have any more information. Currently I'm not convinced that it's a library problem.

Gil 😃

@gilmaimon gilmaimon moved this from Up Next to In progress in Bugfixes Oct 19, 2019
@AlexanderWingard
Copy link
Author

Interesting, so you were able to reproduce the behaviour? I also don't think that it's a library problem actually but maybe some workaround could be implemented. I'm very curious to know what the difference is on TCP level in the different scenarios. Could maybe be investigated with Wireshark somehow.

With more info maybe it should be reported to Espressif?

@AlexanderWingard
Copy link
Author

Hmm, just thought of something. We should try the same thing using only wifi-client and regular netcat. I will do so when I get back if you don't get to it before me.

@AlexanderWingard
Copy link
Author

OK, interesting. I could not reproduce the crash using only WiFiClient and netcat.

Both reading and writing just fails silently when the arduino is connected to the "dead" socket. Also client.connected() returns true until I terminate the netcat program.

Here is the code I used:

#include <WiFi.h>

const char* ssid     = "";
const char* password = "";
const char* host = "";
int port = 1234;
WiFiClient client;
void setup()
{
  Serial.begin(115200);
  delay(10);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
}

void loop()
{
  if (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  } else if (!client.connected()) {
    Serial.println("Not connected");
  }
  client.print("hello");
  delay(1000);
}

@gilmaimon
Copy link
Owner

Interesting, I'll use your code tomorrow to try a few stuffs. I wonder if using .read() will crash (because this is what's crashing in the library). Also it's interesting that connected returns true after reset (if I understand correctly).

Thanks for the details, I'll let you know tomorrow if I learnt anything new.

Gil :)

gilmaimon added a commit that referenced this issue Oct 21, 2019
…efore attempting a read. Which solved the crash in issue #46
@gilmaimon
Copy link
Owner

Hi,

So as you can see in the commit message I made a change that partially solves this issue. Now the client will not crash, it will just not connect to the server.

This is a good first step, but yet it's not possible to reconnect after such reset. It seems like it has more to do with Arduino internals and server impl and less with the library itself.

Let me know how the new code works for you, do you also not get a crash?

Gil.

@AlexanderWingard
Copy link
Author

Works much better for me! I'm super happy with the board not crashing and ending up in my reconnect loop. Then if wscat tries to send something it detects that the client has disconnected and somehow starts to listen again and then the board can connect.

Great work!

@gilmaimon
Copy link
Owner

Closed :)

Bugfixes automation moved this from In progress to Done Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
possible-bug Something isn't working, and I think it is a bug
Projects
Bugfixes
  
Done
Development

No branches or pull requests

2 participants