Skip to content

Fix endless recursion on webui_malloc in debug mode. - #655

Merged
AlbertShown merged 4 commits into
webui-dev:mainfrom
hdijkema:recursion-fix
Oct 21, 2025
Merged

Fix endless recursion on webui_malloc in debug mode.#655
AlbertShown merged 4 commits into
webui-dev:mainfrom
hdijkema:recursion-fix

Conversation

@hdijkema

Copy link
Copy Markdown
Contributor

Fix endless recursion on webui_malloc in debug mode.
We're sure the length of the debug message of _webui_malloc < 256,
so in that case we have enough room to print the debug message
without allocating memory with _webui_malloc, which prevents
an endless recursion to _webui_log.

We're sure the length of the debug message of _webui_malloc < 256,
so in that case we have enough room to print the debug message
without allocating memory with _webui_malloc, which prevents an
endless recursion to _webui_log.


Signed-off-by: Hans Dijkema <hans@dijkewijk.nl>
@AlbertShown

Copy link
Copy Markdown
Member

which prevents an endless recursion to _webui_log

Can you please show me where is that? because I can't find it.
if dynamic allocation failed then it will pass "Memory allocation failed for log" once only.

Another main reason is that char buf[256]; not thread-safe approach, especially when ui_event_blocking = false.

@hdijkema

hdijkema commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

So, when _webui_malloc is called, _webui_malloc will log this:

afbeelding

Thus, the log handler gets called, which tries to allocate a buffer, which calls _webui_malloc, which debugs that it allocates memory, etc, etc, etc. So, this fix effectively prevents _webui_malloc from being called, and because _webui_malloc only needs some 50 bytes or so to write it's debug message, there won't be any recursion.

@AlbertShown

Copy link
Copy Markdown
Member

I see, good catch. To fix this endless recursion and at the same time be thread-safe I suggest keeping dynamic allocation but using malloc() directly instead of _webui_malloc.

@hdijkema

hdijkema commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

That is fine by me, although I think stack allocation of a character array may have some advantages over heap allocation.

  • It's less costly (no heap allocation/deallocation).
  • It's faster to access (not really a point in this use case).

And allocating on the stack is afaik thread safe.

By using a local array of decent size, the majority of debug cases there's probably no malloc/free needed

@AlbertShown

Copy link
Copy Markdown
Member

And allocating on the stack is afaik thread safe.

Sorry I read the modification quick, and I taught the buffer was static but it's not.

@AlbertShown
AlbertShown merged commit d1d2b49 into webui-dev:main Oct 21, 2025
2 of 20 checks passed
@hdijkema
hdijkema deleted the recursion-fix branch October 22, 2025 10:48
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