ESP32U wifi stops connecting...permanently

I have some ESP32u modules working OK in different projects. However occasionally one will refuse to connect any more. No amount of resetting, reloading, restarting etc will get it going again.
The only way I know of resurrecting the now useless module is to do a Flash erase (using esptool.py)
Then reload the sketch and everything works again (for weeks at a time !)
I have managed to simulate the problem at will by issuing a second connect whilst already connected. I am sure there are other possible causes.
I have loaded the latest firmware but problem still remains. Not sure if there is a resolution but any suggestions may help..
This is a sketch that can intentionally KILL's my ESP32's ..... but be prepared to use a Flash Erase !!!

#include <WiFi.h>
#include <esp_wifi.h>

const char* ssid = "xxxxx";
const char* password =  "xxxxx";
float kattempt = 0;
float strength = 0;

int kill=0; // Change this to kill=1 to issue 2nd connect and kill the ESP32

void setup() {
  // FIRST CONNECT
Serial.begin(9600); delay(500);
  
Serial.println("Start Konnect 1");
esp_wifi_set_protocol( WIFI_IF_STA, WIFI_PROTOCOL_LR );
WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(400);
    Serial.println("...");
    kattempt++;
   if (kattempt>20) {kattempt=0;WiFi.disconnect();
   Serial.print("Connect 1 FAILED");
   return; }
  }
 
  Serial.print("#1 WiFi connected with IP: ");
  
  strength=(WiFi.RSSI());
  Serial.println(WiFi.localIP());


if (kill==1) {
  Serial.println("Start Konnect 2");
esp_wifi_set_protocol( WIFI_IF_STA, WIFI_PROTOCOL_LR );
WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(400);
    Serial.println("///");
    kattempt++;
   if (kattempt>20) {kattempt=0;WiFi.disconnect();
   Serial.println("Connect 2 FAILED");
   return; }
  }
 
  Serial.print("#2 WiFi connected with IP: ");
  
  strength=(WiFi.RSSI());
  Serial.println(WiFi.localIP()); }
}


void loop() {
  // put your main code here, to run repeatedly:

} 

What got you thinking you need to use esp_wifi, which is for the ESPRESSIFF IDE and WiFi.h together? WiFi.h is a properly done ESP32 WiFi wrapper for the Arduino core to esp_wifi. In effect you are instantiating 2 of the same objects onto a single hardware core, expect issues.

Good point. As I recall I added the <esp_wifi.h> when I found I had to use the esp_wifi_set_protocol( WIFI_IF_STA, WIFI_PROTOCOL_LR ); to get any sort of distance with the wifi.
I thought I had tested the above sketch with the esp_wifi.h removed but will try again and add a reply.
I still think it a bit unusual that an odd ball sketch can be so destructive.
Thanks for the input

I retried the sketch with esp_wifi.h and esp_wifi_set_protocol removed and it runs without causing a problem.
You are correct, running WiFi.h and esp_wifi together is definitely the wrong thing to do ! Although I notice there are many example sketches online using both.

Now I just need to find out how to set LR using WiFi.h

jnogues/ESP32-Long-Range-WiFi (github.com)