Skip to content

[ESP32] After Power.RebootDevice() managed network calls randomly hang or crash until a hard reset (S3 + C3) #1937

Open

Description

@Tonmyn

Target name(s)

ESP32_S3_QUAD (board: ESP32-S3 Zero) and ESP32_C3

Firmware version

1.17.0.365 (stable channel) — identical behavior on both targets

Was working before? On which version?

N/A — we are not aware of any version where a managed soft reboot kept the network stack working.

Device capabilities

No response

Description

After calling Power.RebootDevice() (managed soft reboot), the network stack ends up in an
unreliable state. Managed network calls randomly hang forever (no exception, no return,
no debugger output) — and the hang point differs from boot to boot:

  • WifiNetworkHelper.Reconnect(requiresDateTime: true, token) — hangs; the CancellationToken
    is never honored
  • WebServer.Start() — hangs after being called
  • TcpListener(IPAddress.Any, 81).Start(1) — verified OK in some boots, so it is not
    port-specific

Some boots never return from Reconnect at all: the boot-counter file is incremented but no
further log entry is written, and the next boot starts by itself — which suggests the runtime
died and was reset during the native call (possibly the task watchdog).

When the calls do return, the WiFi connection itself is fine (Reconnect == true,
Status == Success, correct IP). Only a hard reset (RST button, power cycle, or
re-deploying from VS) reliably restores normal behavior.

Instrumented bootlog samples (each step appends a marker to a file, read back after a
re-deploy):

Hang inside WebServer.Start():

#1 | A entered Main
#1 | B Reconnect started
#1 | C Reconnect returned ok=True Status=2 IP=192.168.3.54
#1 | E WebServer starting
(nothing more — Start() never returns, no exception thrown)

Death inside Reconnect (next boot starts by itself):

#1 | A entered Main
#1 | B Reconnect started
#2 | A entered Main          <- no user action in between

Everything we tried before rebooting — none of it changed the outcome (tested on BOTH targets):

  • WebServer.Stop() before rebooting + 2 s delay
  • STA disconnect before reboot + delays up to 3 s
  • WirelessAPConfiguration.Options = None + SaveConfiguration() before reboot
  • Reconnect on a worker thread with an external hard timeout (timeout fires, the stuck
    native call remains)
  • Consecutive (double) soft reboot
  • Different HTTP port (80 / 8080)
  • Different phone / router

After a hard reset, the very same code always works on the first try.

How to reproduce

  1. Flash firmware 1.17.0.365 for ESP32_S3_QUAD (or ESP32_C3).
  2. Store WiFi credentials via Device Explorer > Network configuration > Wireless 80211
    (no credentials are hardcoded in the app).
  3. Deploy the minimal app from "Additional information" below.
  4. Open http:/// and click the reboot button.
  5. Wait ~20 s, then try to open the page again - usually unreachable.
  6. Re-deploy from VS, open the page again and read the boot history
    (I:\bootlog.txt) - it shows exactly where each boot stopped.

Expected behaviour

After a managed soft reboot the network stack should be re-initialized exactly like after a
hard reset; managed network calls should either work or throw - never hang forever.

Screenshots

No response

Aditional information

  • Visual Studio 2022, nanoFramework extension v2022.14.1.15
  • Packages: nanoFramework.System.Device.Wifi 1.5.152, nanoFramework.WebServer 1.2.162,
    nanoFramework.System.Net 1.11.64, nanoFramework.System.Net.Sockets.TcpClient 1.1.137,
    nanoFramework.Runtime.Native 1.7.11

Minimal repro app (full Program.cs):

using nanoFramework.Networking;
using nanoFramework.Runtime.Native;
using nanoFramework.WebServer;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace NFApp_reboot_test
{
    public class Program
    {
        private const string BootCountFile = "I:\\boots.txt";
        private const string BootLogFile   = "I:\\bootlog.txt";
        private static int _boots;

        public static void Main()
        {
            int boots = 0;
            try { if (File.Exists(BootCountFile)) boots = int.Parse(File.ReadAllText(BootCountFile)); } catch { }
            boots++; _boots = boots;
            try { File.WriteAllText(BootCountFile, boots.ToString()); } catch { }

            Log("A entered Main");

            Log("B Reconnect started");
            bool ok = false; string err = null;
            try
            {
                var cts = new CancellationTokenSource(25000);
                ok = WifiNetworkHelper.Reconnect(requiresDateTime: true, token: cts.Token);
            }
            catch (Exception ex) { err = ex.Message; }
            Log("C Reconnect returned ok=" + ok
                + " Status=" + (int)WifiNetworkHelper.Status
                + " IP=" + GetIp() + (err != null ? " Ex=" + err : ""));

            Log("D TcpListener probe");
            try
            {
                TcpListener probe = new TcpListener(IPAddress.Any, 81);
                probe.Start(1);
                probe.Stop();
            }
            catch (Exception ex) { Log("D failed: " + ex.Message); }

            Log("E WebServer starting");
            var server = new WebServer(80, HttpProtocol.Http);
            server.CommandReceived += OnCommand;
            server.Start();
            Log("F WebServer started");

            Thread.Sleep(Timeout.Infinite);
        }

        private static void OnCommand(object sender, WebServerEventArgs e)
        {
            string url = e.Context.Request.RawUrl;

            if (url == "/reboot")
            {
                new Thread(() =>
                {
                    Thread.Sleep(1000);
                    Log("R calling Power.RebootDevice()");
                    Power.RebootDevice();
                }).Start();
                return;
            }

            // serve a simple page: boot count + full bootlog content
            string bootCount = ReadFile(BootCountFile);
            string bootLog = ReadFile(BootLogFile);
            string html = "<html><body><h2>Soft reboot test</h2>"
                + "<p>IP: " + GetIp() + "</p>"
                + "<p>Boots: " + bootCount + "</p>"
                + "<p><a href=\"/reboot\"><button>Soft reboot</button></a></p>"
                + "<pre>" + bootLog + "</pre>"
                + "</body></html>";

            byte[] bytes = Encoding.UTF8.GetBytes(html);
            e.Context.Response.ContentType = "text/html";
            e.Context.Response.ContentLength64 = bytes.Length;
            e.Context.Response.OutputStream.Write(bytes, 0, bytes.Length);
            e.Context.Response.Close();
        }

        private static string ReadFile(string path)
        {
            try { return File.Exists(path) ? File.ReadAllText(path) : ""; }
            catch { return ""; }
        }

        private static void Log(string line)
        {
            try
            {
                string old = File.Exists(BootLogFile) ? File.ReadAllText(BootLogFile) : "";
                File.WriteAllText(BootLogFile, old + "#" + _boots + " | " + line + "\r\n");
            }
            catch { }
        }

        private static string GetIp()
        {
            try
            {
                NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
                for (int i = 0; i < nis.Length; i++)
                    if (nis[i].NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                        return nis[i].IPv4Address;
            }
            catch { }
            return "0.0.0.0";
        }
    }
}

Question: is Power.RebootDevice() + WiFi + sockets expected to work on ESP32 targets?
Is there a supported sequence (e.g., deinit calls before rebooting) that makes a managed
soft reboot safe for the network stack?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions