eModbus / eModbus

Modbus library for RTU, ASCII and TCP protocols. Primarily developed on and for ESP32 MCUs.
https://emodbus.github.io
MIT License
363 stars 122 forks source link

ModbusServerTCPasync crash calling stop() if server it's not running, please add a check #245

Closed dsilletti closed 1 year ago

dsilletti commented 1 year ago

Describe the bug stopping the server with MBserver.stop() cause crash if the server is not running.

Please add a check to be sure the server is running before trying to stop it. I fixed by myself adding this in the file ModbusServerTCPasync.cpp:

bool ModbusServerTCPasync::stop() {

  // required check!!!
  if (!server) {
    LOG_W("Server not running.\n");
    return false;
  }

  // stop server to prevent new clients connecting
  server->end();
......

Expected behavior stop() should return false if server it's not running and not crash.

To Reproduce Steps to reproduce the behavior: stop the server when it's not running using the MBserver.stop() function

dsilletti commented 1 year ago

created PR #246 to fix the issue

Miq1 commented 1 year ago

@bertmelis currently is reworking the async code anyway, so he will take care of this.

Thanks for the find.