Skip to content
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

WPEWebprocess running out of memory causing cog to crash and display Out of Memory #241

Open
himanshugaur2610 opened this issue Nov 25, 2024 · 0 comments

Comments

@himanshugaur2610
Copy link

Monitoring WPEWebProcess when running libwebsockets-test-server and starting cog at http://localhost:7681/ shows that the memory associated with the process kept on linearly increasing eventually causing a crash. Detailed logs below. This is consistently reproducible by just running this patch provided and happens on webkit version 2.44.4. Haven't seen this happening in 2.38.6 wpewebkit version.

The whole browser presents a white screen with the error when it happens, and the journal log:
cog[19091]: Loaded successfully.
cog[19091]: Out of memory!: The renderer process ran out of memory. You may try reloading the page to restart it.

To strip this to a reproducible test case, the steps are as follows:

  • start "libwebsockets-test-server"
  • start cog at "http://localhost:7681/"
  • Then you can stop/restart libwebsockets-test-server while leaving cog running to see if memory frees or not.

Monitor and log resource usage:
Use system tools (e.g., top, htop, or specific memory profiling tools) to track memory consumption during these tests.

To reproduce this better, there is a patch that I have attached which can demonstrate this memory leak better.

Summary of the patch that helps to create the problem:
To increase the leak, apply 0001-debug-lws-memory-leak.patch to libwebsockets repo and rebuild. it allows to configure the ws period via /tmp/lws_period.txt and allow stopping/starting test-server without restarting webkit.

The following part only pertains to the patch and provides details about the same patch I have provided that can help reproduce this leak better:

This patch introduces several changes across two files: protocol_dumb_increment.c and test.js.
The main focus is to debug a memory leak in a WebSocket-based application by addressing the following key areas:

Changes to protocol_dumb_increment.c (WebSocket Protocol Handler)
Dynamic Period Adjustment:
The protocol_dumb_increment.c file has been modified to allow for dynamic adjustment of the "dumb increment period".
Previously, a fixed period (DUMB_PERIOD_US) was used, but now the period can be changed by writing a new value (in microseconds) to a file at /tmp/lws_period.txt. The change is read by the server when a WebSocket connection is established,
providing more flexibility to control the timing without modifying the code.
A new function get_period() was added, which checks for the value in /tmp/lws_period.txt. If the file exists and contains a valid value, it updates the period_us variable. This period is then passed to the lws_set_timer_usecs function, which is responsible for setting the timer period for the dumb increment protocol.

0001-debug-lws-memory-leak (1).patch:
The previous fixed DUMB_PERIOD_US was replaced by the dynamic value returned by get_period() during WebSocket connection setup and writes.

Changes to test.js (WebSocket Client)
Reconnect WebSocket on Lost Connection:
The client-side JavaScript was updated to improve how WebSocket connections are handled. Specifically, if the WebSocket connection (socket_di) is lost or closed, the script now attempts to reconnect automatically. This behavior is controlled via a setInterval that tries to reopen the WebSocket connection every second (1000ms).

Set up the environment:
Ensure that the server is running the patched version of the WebSocket server (protocol_dumb_increment.c) and that the client (test.js) is set up to connect to it.

Simulate WebSocket disconnection:
Initially open a WebSocket connection from the client. Then, simulate a WebSocket disconnect or close the connection manually (e.g., by disabling the network or closing the browser tab).

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

No branches or pull requests

1 participant