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

Invalidate sockets when turning WiFi off #1639

Merged
merged 2 commits into from Dec 14, 2018

Conversation

@sergeuz
Copy link
Member

commented Dec 14, 2018

Problem

Socket operations, such as socket_close(), may cause a hard fault when performed after a WiFi reset.

Solution

Invalidate all sockets when WiFi is being turned off. It is important to keep socket handles allocated via socket_create() valid.

Steps to Test

The following application should not cause a hard fault or deadlock:

#include "application.h"

SYSTEM_MODE(SEMI_AUTOMATIC)

namespace {

const Serial1LogHandler logHandler(115200, LOG_LEVEL_WARN, {
    { "app", LOG_LEVEL_ALL }
});

system_tick_t lastMillis = 0;
bool done = false;

} // namespace

void setup() {
    Log.info("setup()");
}

void loop() {
    if (done) {
        if (millis() - lastMillis >= 3000) {
            Log.info("loop()");
            lastMillis = millis();
        }
        return;
    }
    done = true;

    Log.info("WiFi.connect()");
    WiFi.connect();

    Log.info("socket_create()");
    const sock_handle_t sock = socket_create(AF_INET, SOCK_STREAM, IPPROTO_TCP, 53, NIF_DEFAULT);
    if (!socket_handle_valid(sock)) {
        Log.error("socket_create() failed");
        return;
    }

    Log.info("socket_connect()");
    sockaddr_t addr = {};
    addr.sa_family = AF_INET;
    addr.sa_data[0] = 0;
    addr.sa_data[1] = 53;
    addr.sa_data[2] = 8;
    addr.sa_data[3] = 8;
    addr.sa_data[4] = 8;
    addr.sa_data[5] = 8;
    const auto r = socket_connect(sock, &addr, sizeof(addr));
    if (r != 0) {
        Log.error("socket_connect() failed");
        return;
    }

    Log.info("WiFi.off()");
    WiFi.off();

    Log.info("WiFi.connect()");
    WiFi.connect();

    Log.info("socket_close()");
    const auto r2 = socket_close(sock);
    if (r2 != 0) {
        Log.error("socket_close() failed");
        return;
    }
}

References

  • [ch25794]

Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

@sergeuz sergeuz requested a review from avtolstoy Dec 14, 2018

@sergeuz sergeuz merged commit 0140a25 into develop Dec 14, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@sergeuz sergeuz deleted the fix/invalidate_sockets branch Dec 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.