Bug 57104 - UMC-Server: memory leaks
UMC-Server: memory leaks
Status: CLOSED FIXED
Product: UCS
Classification: Unclassified
Component: UMC (Generic)
UCS 5.0
Other Linux
: P5 normal (vote)
: UCS 5.0-7-errata
Assigned To: Florian Best
Marius Meschter
https://git.knut.univention.de/univen...
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2024-03-01 17:55 CET by Florian Best
Modified: 2024-03-20 17:18 CET (History)
3 users (show)

See Also:
What kind of report is it?: ---
What type of bug is this?: ---
Who will be affected by this bug?: ---
How will those affected feel about the bug?: ---
User Pain:
Enterprise Customer affected?:
School Customer affected?:
ISV affected?:
Waiting Support:
Flags outvoted (downgraded) after PO Review:
Ticket number:
Bug group (optional):
Max CVSS v3 score:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Best univentionstaff 2024-03-01 17:55:55 CET
There are memory leaks in the UMC-server:

1. univention.management.console.resource.Resource.get_current_user() assigns a fake translation function for the session, which holds a reference the the Resource/RequestHandler. The RequestHandler is therefore never garbage collected.

     def get_current_user(self):
         session = Session.get_or_create(self.get_session_id())  # FIXME: this creates a Session instance even if the session does not exists
-        session._ = lambda *args: self._(*args)  # hack to prevent circular use, when "_" is not yet set but only in prepare()  # noqa: PLW0108
+        weakself = weakref.ref(self)
+        session._ = lambda *args: weakself()._(*args)  # hack to prevent circular use, when "_" is not yet set but only in prepare()  # noqa: PLW0108
         if not session.user.ip:
             session.user.ip = self.get_ip_address()
         return session


2. univention.management.console.message.Request.get_user_ldap_connection() adds a "self.bind_user_connection" reference to a LDAP connection cache (in .ldap.LDAP.__ldap_connections), which references the Request object, so that this is never garbage collected.

     def get_user_ldap_connection(self, no_cache=False, **kwargs):
         if not self.user_dn:
             return  # local user (probably root)
+
+        wheakself = weakref.ref(self)
+
+        def bind_user_connection(lo):
+            return wheakself().bind_user_connection(lo)
+
         try:
-            lo, _po = get_user_connection(bind=self.bind_user_connection, write=kwargs.pop('write', False), follow_referral=True, no_cache=no_cache, **kwargs)
+            lo, _po = get_user_connection(bind=bind_user_connection, write=kwargs.pop('write', False), follow_referral=True, no_cache=no_cache, **kwargs)


3. univention.management.console.session.Session passes "self" to .IACLs and .Processes, which holds them so that a circular reference exists.
Note that Session.renew() throws away the IACLs and Processes instances, without e.g. unassigning the .session reference, which will cause that they never get garbage collected.
Comment 2 Florian Best univentionstaff 2024-03-04 14:41:53 CET
The memory leaks have been addressed.
A additional signal handler SIGUSR2 has been added, which (if python3-objgraph) prints the amount of most used univention.management.console objects into the logfile, so one can compare pre/post states.
Additionally the README.md has been adjusted on strategies how to analyze memory leaks.

univention-management-console.yaml
61ba40525d8b | fix(umc): fix memory leaks / circular references

univention-management-console (12.0.32-21)
61ba40525d8b | fix(umc): fix memory leaks / circular references

univention-management-console (12.0.32-20)
5abccd9ecf3a | feat(umc): Add SIGUSR2 signal handler to analyze memory
Comment 3 Marius Meschter univentionstaff 2024-03-05 18:00:35 CET
OK: changelog and YAML
OK: logging of memory information using objgraph
OK: jenkins tests
OK: using the new memory information functionality, various memory leaks are now gone
OK: UMC functionality

TODO: univention.uldap.access and univention.admin.uldap.access still appear to leak, however this is not new in this bug and will be fixed separately
Comment 4 Dirk Wiesenthal univentionstaff 2024-03-06 12:28:43 CET
As discussed, we would like to postpone the release until the other leaks are fixed as well.
Comment 5 Florian Best univentionstaff 2024-03-07 08:53:45 CET
reverted and moved into https://git.knut.univention.de/univention/ucs/-/merge_requests/1088.
Comment 6 Florian Best univentionstaff 2024-03-14 23:45:15 CET
UCS 5.2:
univention-management-console (14.0.6)
931256fbf68a | fix(umc): fix memory leaks / circular references

UCS 5.1:
univention-management-console (13.0.11)
dd95941614c2 | fix(umc): fix memory leaks / circular references

UCS 5.0-7:
univention-management-console (12.0.33-1)
cb6a9ca914d5 | fix(umc): fix memory leaks / circular references
Comment 7 Marius Meschter univentionstaff 2024-03-19 17:18:24 CET
OK: changelog and YAML
OK: logging of memory information using objgraph
OK: jenkins tests
OK: using the new memory information functionality, various memory leaks are now gone
OK: UMC functionality
OK: using the memory information, no more objects stay behind after session ends