Skip to content

Fixed memory leak in the logging endpoint #8530

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
Mar 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/tribler/core/restapi/logging_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
from tribler.core.restapi.rest_endpoint import RESTEndpoint, RESTResponse


class RotatingMemoryHandler(MemoryHandler):
"""
A custom MemoryHandler flusher. Instead of delegating to the ``target`` and flushing the ``buffer``, simply keep
the last ``capacity`` records in the buffer.
"""

def flush(self) -> None:
"""
This is called when our buffer is at capacity and needs to be flushed.
We get the handler lock and rotate the buffer.
"""
self.acquire()
try:
self.buffer = self.buffer[-self.capacity:]
finally:
self.release()


class LoggingEndpoint(RESTEndpoint):
"""
This endpoint allows retrieval of the logs.
Expand All @@ -21,7 +39,7 @@ def __init__(self) -> None:

self.base_handler = logging.getLogger().handlers[0]

self.memory_logger = MemoryHandler(100000)
self.memory_logger = RotatingMemoryHandler(400)
logging.getLogger().addHandler(self.memory_logger)

self.app.add_routes([web.get("", self.get_logs)])
Expand Down