was: enable WebSocket destroy on exit in was_init #361
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The WAS WebSocket client sometimes fails to reconnect after receiving a WEBSOCKET_EVENT_CLOSED event due to it not being able to create the WebSocket client task:
I (13:15:28.958) WILLOW/WAS: WebSocket closed
I (13:15:28.959) WILLOW/WAS: initializing WebSocket client (ws://was.dev.willow:8502/ws)
E (13:15:28.961) websocket_client: Error create websocket task
E (13:15:28.967) WILLOW/WAS: failed to start WebSocket client: ESP_FAIL
When this happens, Willow stops trying to reconnect to WAS, and the user has to reset or power-cycle their device. As this is terrible UX, this should be avoided.
The root cause is not freeing resources of the WebSocket client before we reinitialize it in init_was. This eventually causes us to run out of IRAM, causing the WebSocket client task creation to fail.
To fix this, enable WebSocket destroy on exit in was_init, rather than in was_deinit, so it is always enabled. This is needed to avoid leaking resources due to not calling esp_websocket_client_destroy before calling esp_websocket_client_init. We don't want to call
esp_websocket_client_destroy, as that has proven to be very prone to crashes in the past.
Similarly, we don't want to call esp_websocket_client_start when the WAS WebSocket client receives a WEBSOCKET_EVENT_CLOSED event. This would avoid the resource leak, but testing has shown this is also very prone to crashes.
See the following commits for extra information:
985375c ("was: drop esp_websocket_client_destroy()") 1f3d298 ("was: drop esp_websocket_client_destroy() completely")