-
Notifications
You must be signed in to change notification settings - Fork 183
esp_websocket_client: Add Kconfig options for PSRAM allocation (IDFGH-17011) #980
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
base: master
Are you sure you want to change the base?
esp_websocket_client: Add Kconfig options for PSRAM allocation (IDFGH-17011) #980
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
13277dd to
27c4e4b
Compare
27c4e4b to
8a75888
Compare
8a75888 to
b683518
Compare
b683518 to
6c381b9
Compare
6c381b9 to
e84c0eb
Compare
cbf6bad to
016bf62
Compare
016bf62 to
0ae9008
Compare
15cba04 to
9d9f1c9
Compare
- Added CONFIG_ESP_WS_CLIENT_TASK_STACK_IN_EXT_RAM to allow allocating WebSocket task stack in PSRAM. - Added CONFIG_ESP_WS_CLIENT_ENABLE_DYNAMIC_BUFFER to allow dynamic allocation of RX/TX buffers. - Fixed memory leak in esp_websocket_client_destroy by deferring destruction to a separate task when called from the WebSocket task itself. - Fixed stack size calculation for xTaskCreateStaticPinnedToCore (expecting words). - Fixed race conditions and use-after-free in task destruction by using vTaskSuspend + vTaskDelete pattern instead of self-deletion. - Fixed double-free race condition in destroy_on_exit where manual destroy could race with deferred destruction. - Fixed deferred destruction never completing when called from task. - Fixed suspended task leak when STOPPED_BIT is already set.
9d9f1c9 to
391fb0b
Compare
|
Hi @gabsuren, I wanted to follow up on this PR to see if there is any feedback or if any additional information is needed from my side to move this forward. These changes are quite important for memory-constrained applications using multiple concurrent TLS/HTTPS connections (like Spotify Connect implementations). By allowing the WebSocket task stack and client structure to be moved to PSRAM, we can significantly free up critical internal RAM. Beyond the memory allocation, this PR also introduces a more robust lifecycle management (using DESTRUCTION_IN_PROGRESS_BIT and deferred task deletion). This hardening has proven to be very stable and effectively prevents race conditions during rapid reconnect/destroy cycles that were previously an issue. The branch is currently conflict-free and has been extensively tested on ESP32-S3. Looking forward to your thoughts! |
Description
This PR introduces two new Kconfig options to the
esp_websocket_clientcomponent to allow memory allocation in external RAM (PSRAM):ESP_WS_CLIENT_TASK_STACK_IN_EXT_RAM: Enables allocation of the WebSocket task stack in PSRAM usingxTaskCreateStaticPinnedToCore.ESP_WS_CLIENT_ALLOC_IN_EXT_RAM: Enables allocation of theesp_websocket_clientstructure and its internal configuration storage in PSRAM.Motivation
In applications requiring multiple concurrent secure connections (e.g., TLS/HTTPS), internal RAM is a critical bottleneck. Each TLS session typically requires 16-20KB of internal RAM for buffers. By allowing the WebSocket client's task stack and internal structures to be moved to PSRAM, significant internal memory is freed for these critical network operations, improving overall system stability and preventing
ESP_ERR_NO_MEMfailures in memory-constrained scenarios.Implementation Details
heap_caps_callocwithMALLOC_CAP_SPIRAMfor the client structure and config.xTaskCreateStaticPinnedToCorefor the task stack when PSRAM is selected.Related
Testing
heap_caps_get_free_sizethat internal RAM usage decreased by the expected amount (~5KB for structure + stack size).Note
Enables optional PSRAM usage and improves lifecycle safety for the WebSocket client.
ESP_WS_CLIENT_TASK_STACK_IN_EXT_RAMandESP_WS_CLIENT_ALLOC_IN_EXT_RAMto allocate task stack and client/config in PSRAMxTaskCreateStaticPinnedToCorewith PSRAM stack and internal-RAM TCB; falls back to internal RAM if allocation failstask_stack_bufferandtask_bufferfields; ensures they are freed; clearsstatus_bitsafter deletionDESTRUCTION_IN_PROGRESS_BIT, defers self-destruction viaws_destroytask, suspends task before deletion, and prevents double-destroy;stop_wait_tasknow waits for suspension then deletes the taskWritten by Cursor Bugbot for commit 391fb0b. This will update automatically on new commits. Configure here.