Skip to content

esp_websocket_client: bump default task stack size to 8192 bytes #42

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

Merged
merged 1 commit into from
Jun 17, 2025

Conversation

JCBird1012
Copy link

This can alleviate crashes with wss:// addresses - 4KB might not be sufficient for the WebSocket client with TLS overhead - this change bumps the task stack size to match that of the main thread (8KB).

I was seeing crashes when trying to use wss:// addresses -

(8966) main: Using websockets with URL: wss://<snipped domain>/<snipped device ID>/ws
(8974) websocket_client: `reconnect_timeout_ms` is not set, or it is less than or equal to zero, using default time out 10000 (milliseconds)
(8988) websocket_client: `network_timeout_ms` is not set, or it is less than or equal to zero, using default time out 10000 (milliseconds)
(9001) main: WebSocket not connected. Attempting to reconnect..
(9007) websocket_client: Client was not started
(9017) websocket_client: Started
(9017) main: Reconnected to WebSocket server.
(9025) wifi:<ba-add>idx:1 (ifx:0, 9c:05:d6:f3:cb:a7), tid:6, ssn:2, winSize:64
(9464) gfx: Loaded new webp

***ERROR*** A stack overflow in task websocket_task has been detected.


Backtrace: 0x40082b58:0x3ffd6ad0 0x4008d69d:0x3ffd6af0 0x4008e84e:0x3ffd6b10 0x4008f9f3:0x3ffd6b90 0x4008e958:0x3ffd6bb0 0x4008e90a:0x00000000 |<-CORRUPTED




ELF file SHA256: 88a66a478441edb0

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

After doing some testing by bumping the task_stack to 8KB and adding:

UBaseType_t highwater = uxTaskGetStackHighWaterMark(NULL);
ESP_LOGI(TAG, "websocket_task min free stack: %u bytes", highwater);

at the end of the WEBSOCKET_EVENT_DATA case, I discovered that we were using excess of 4KB - (64965) main: websocket_task min free stack: 3956 bytes (8192 − 3956 ≈ 4236 bytes) - roughly 200 bytes too much.

After the bump, everything works as expected.

This can alleviate crashes with wss addresses - 4KB might not be sufficient for the WebSocket client with TLS overhead - this change bumps the task stack size to match that of the main thread (8KB)
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @JCBird1012, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request aims to resolve stack overflow issues encountered when using the WebSocket client with TLS (wss://) connections. The change increases the default stack size allocated to the WebSocket task, ensuring enough memory is available for operations involving TLS overhead, thereby improving the stability of secure WebSocket connections.

Highlights

  • Fix Stack Overflow: Increased the default stack size for the esp_websocket_client task to prevent stack overflows, particularly when using wss:// (TLS) connections.
  • Improved Stability: Addresses observed crashes during wss:// connections by providing sufficient stack space for TLS overhead.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request increases the task stack size to resolve a stack overflow issue for WebSocket connections using TLS. Defining the new stack size as a named constant would improve code clarity and maintainability.

@@ -215,6 +215,7 @@ void app_main(void) {
// setup ws event handlers
const esp_websocket_client_config_t ws_cfg = {
.uri = url_to_use,
.task_stack = 8192,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The value 8192 for task_stack is a "magic number". It's good practice to define this as a named constant (e.g., WEBSOCKET_TASK_STACK_SIZE_FOR_TLS) at the top of the file or in a relevant configuration header. This improves code readability and maintainability.

      .task_stack  = WEBSOCKET_TASK_STACK_SIZE_FOR_TLS,

@tavdog tavdog merged commit 1625b52 into tronbyt:main Jun 17, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants