Bug #34789 drop server/create server leaks memory !
Submitted: 24 Feb 2008 13:49 Modified: 22 Apr 2008 14:15
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Federated storage engine Severity:S3 (Non-critical)
Version:5.1.23, 5.1.24-rc OS:Any
Assigned to: Sergey Vojtovich CPU Architecture:Any
Tags: memory leak

[24 Feb 2008 13:49] Shane Bester
Description:
a memory leak presents itself when drop server/create server is run.
on my small test, mysqld (default config) consumed 160M after 1.6 million iterations.

How to repeat:
delimiter ;
drop procedure if exists `p`;
delimiter //
create procedure `p`(`a` int)
begin
declare `c` int default 0;
repeat
   if `c` mod 25000 = 0 then
      select `c` as `iteration`;
   end if;
   drop server if exists ss;
   create server ss foreign data wrapper mysql options (user '', host '', database '');
   set `c` = `c`+1;
until `c` > `a`
end repeat;
end//
delimiter ;
call `p`(10000000);

Suggested fix:
find the leak.
[24 Feb 2008 14:50] MySQL Verification Team
bug #34790 will be easier to debug if you use this bugs leak to provoke the crash.
[25 Feb 2008 17:52] Valeriy Kravchuk
Thank you for a bug report. Verified just as described wit latest 5.1.24-BK on Linux.
[19 Mar 2008 13:49] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44224

ChangeSet@1.2569, 2008-03-19 17:45:11+04:00, svoj@mysql.com +1 -0
  BUG#34789 - drop server/create server leaks memory !
  
  When CREATE SERVER is issued, it allocates memory on memory root
  to store cached server structure. When DROP SERVER is issued,
  it doesn't release this memory, as it is impossible with the
  memory root.
  
  This is fine, as we use memory root at least for plugins, and very
  likely for acl. The problem here that there was no way (except for
  the server restart) to force 'servers' code to release this memory.
  
  With this fix it is possible to release unused server cache memory
  by FLUSH PRIVILEGES.
  
  No test case for this fix.
[19 Mar 2008 14:18] Alexander Barkov
(Reluctantly) http://lists.mysql.com/commits/44224  is ok to push.

I suggest to ask doc team to document this "feature"
that one needs to run "FLUSH PRIVILEGES" to free memory
created by "CREATE SERVER", "CREATE PLUGIN".
[20 Mar 2008 8:00] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44269

ChangeSet@1.2569, 2008-03-20 11:57:30+04:00, svoj@mysql.com +2 -0
  BUG#34789 - drop server/create server leaks memory !
  
  When CREATE SERVER is issued, it allocates memory on memory root
  to store cached server structure. When DROP SERVER is issued,
  it doesn't release this memory, as it is impossible with the
  memory root.
  
  We use the same allocation strategy for plugins and acl. The problem
  here that there was no way (except for the server restart) to force
  'servers' code to release this memory.
  
  With this fix it is possible to release unused server cache memory
  by FLUSH PRIVILEGES.
  
  No test case for this fix.
[20 Mar 2008 9:46] Ingo Strüwing
Approved with the agreed documentation and worklog/bug creation tasks.
[21 Mar 2008 9:21] Sergey Vojtovich
Docs note: currently I didn't find anything relevant about grant tables cache handling in the manual. Please update the manual, so it states that:

Frequent updates to grant tables, including servers table and plugin, via appropriate statements (e.g. GRANT/REVOKE, CREATE/DROP USER, CREATE/DROP SERVER, INSTALL/UNINSTALL plugin) may increase memory usage.

As not-freed memory chunks are relatively small, one would experience noticable memory increase only if there were thousands or millions updates to grant tables.

This happens because when removing a record (REVOKE/DROP/UNINSTALL) from cache, we do not release memory back to an operating system. This memory is freed on FLUSH PRIVILEGES or server restart.
[25 Mar 2008 13:42] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44391

ChangeSet@1.2572, 2008-03-25 17:37:53+04:00, svoj@mysql.com +3 -0
  BUG#34789 - drop server/create server leaks memory !
  BUG#34790 - 'create server' doesn't handle out of memory scenario
              well enough
  
  This is an addition to fixes for these bugs, which makes gcov
  happy.
[27 Mar 2008 11:18] Bugs System
Pushed into 5.1.24-rc
[27 Mar 2008 17:50] Bugs System
Pushed into 6.0.5-alpha
[3 Apr 2008 14:54] Jon Stephens
Pushed into 5.1.23-ndb-6.3.11.
[22 Apr 2008 14:15] Paul DuBois
Noted in 5.1.24, 6.0.5 changelogs.

DROP SERVER does not release memory cached for server structures
created by CREATE SERVER, so repeated iterations of these statements
resulted in a memory leak. FLUSH PRIVILEGES now releases the memory
allocated for CREATE SERVER.