Bug #119574 Component Command Service leaks memory
Submitted: 18 Dec 13:47 Modified: 22 Dec 10:59
Reporter: Dmitry Lenev (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Components / Services Severity:S2 (Serious)
Version:9.5.0 OS:Any
Assigned to: CPU Architecture:Any

[18 Dec 13:47] Dmitry Lenev
Description:
Implementation of Command Service in 9.5.0 leaks memory which it allocates to store metadata for command/query result.

This memory leak is easy to see by running MTR test_services suite under ASAN.

This is likely to be a regression from recent commit which removed freeing of this memory:

commit 42a067ce55032b1d093023275ce5ee98e29226dd
Date:   Fri Sep 12 18:21:34 2025 +0100

    BUG#38425487 mysql_command_factory->connect() failure when running in explicit mode leaks session due to premature cleanup
    

How to repeat:
1) Prepare Debug + ASAN build of MySQL Server 9.5.0. 
   cmake -DWITH_DEBUG=1 -DWITH_ASAN=1 ...
   make ...
2) Run a few tests in test_services MTR suite:

./mtr --mem --sanitize test_services.test_event_tracking test_services.test_mysql_command_services_component test_services.test_table_access

3) Observe:
Logging: .../mysql-server/mysql-test/mysql-test-run.pl  --mem --sanitize test_services.test_event_tracking test_services.test_mysql_command_services_component test_services.test_table_access
MySQL Version 9.5.0
Checking supported features
 - Binaries are debug compiled
Using 'all' suites
Collecting tests
Checking leftover processes
Removing old var directory
Creating var directory '.../mysql-server/bld_asan/mysql-test/var'
 - symlinking 'var' to '/dev/shm/var_auto_mchH'
Installing system database
Using parallel: 1
ports_per_thread:30

==============================================================================
                  TEST NAME                       RESULT  TIME (ms) COMMENT
------------------------------------------------------------------------------
[ 20%] test_services.test_event_tracking         [ pass ]     23
[ 40%] test_services.test_mysql_command_services_component  [ pass ]    387
[ 60%] test_services.test_table_access           [ pass ]   1257
worker[1] Shutdown report from .../mysql-server/bld_asan/mysql-test/var/log/mysqld.1.err after tests:
 test_services.test_event_tracking test_services.test_mysql_command_services_component test_services.test_table_access

...

==187976==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1712 byte(s) in 1 object(s) allocated from:
    #0 0x745131cb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5abf5f5cd4da in redirecting_allocator mysql-server/mysys/my_malloc.cc:278
    #2 0x5abf5f5cd976 in my_raw_malloc<redirecting_allocator> mysql-server/mysys/my_malloc.cc:321
    #3 0x5abf5f5cdac4 in my_internal_malloc<redirecting_allocator> mysql-server/mysys/my_malloc.cc:371
    #4 0x5abf5f5cdbc1 in my_malloc(unsigned int, unsigned long, int) mysql-server/mysys/my_malloc.cc:385
    #5 0x5abf5f5bbcdc in MEM_ROOT::AllocBlock(unsigned long, unsigned long) mysql-server/mysys/my_alloc.cc:92
    #6 0x5abf5f5bbfac in MEM_ROOT::AllocSlow(unsigned long) mysql-server/mysys/my_alloc.cc:125
    #7 0x5abf5c79c464 in MEM_ROOT::Alloc(unsigned long) mysql-server/include/my_alloc.h:165
    #8 0x5abf60f39fac in mysql_command_consumer_dom_imp::start_result_metadata(SRV_CTX_H_imp*, unsigned int, unsigned int, char const*) mysql-server/sql/server_component/mysql_command_consumer_imp.cc:96
    #9 0x5abf60f8b479 in Callback_command_delegate::start_result_metadata(unsigned int, unsigned int, CHARSET_INFO const*) mysql-server/sql/server_component/mysql_command_delegates.cc:49
    #10 0x5abf60f3ce76 in Command_delegate::call_start_result_metadata(void*, unsigned int, unsigned int, CHARSET_INFO const*) mysql-server/sql/server_component/mysql_command_delegates.h:300
    #11 0x5abf5d9a5d20 in Protocol_callback::start_result_metadata(unsigned int, unsigned int, CHARSET_INFO const*) mysql-server/sql/protocol_callback.cc:336
    #12 0x5abf5c94c0e1 in THD::send_result_metadata(mem_root_deque<Item*> const&, unsigned int) mysql-server/sql/sql_class.cc:2944
    #13 0x5abf5d81b492 in Query_result_send::send_result_set_metadata(THD*, mem_root_deque<Item*> const&, unsigned int) mysql-server/sql/query_result.cc:75
    #14 0x5abf5cd7f64a in Query_expression::ExecuteIteratorQuery(THD*) /home/dlenev/src/git/mysql-server/sql/sql_union.cc:1058
    #15 0x5abf5cd800cd in Query_expression::execute(THD*) mysql-server/sql/sql_union.cc:1200
    #16 0x5abf5cc20a6a in Sql_cmd_dml::execute_inner(THD*) mysql-server/sql/sql_select.cc:1153
    #17 0x5abf5cc2abc0 in Sql_cmd_dml::execute(THD*) mysql-server/sql/sql_select.cc:798
    #18 0x5abf5cb0e190 in mysql_execute_command(THD*, bool) mysql-server/sql/sql_parse.cc:4754
    #19 0x5abf5cb11cf6 in dispatch_sql_command(THD*, Parser_state*, bool) mysql-server/sql/sql_parse.cc:5433
    #20 0x5abf5cb14863 in dispatch_command(THD*, COM_DATA const*, enum_server_command) mysql-server/sql/sql_parse.cc:2148
    #21 0x5abf5cf759b2 in Srv_session::execute_command(enum_server_command, COM_DATA const*, CHARSET_INFO const*, st_command_service_cbs const*, cs_text_or_binary, void*) mysql-server/sql/srv_session.cc:1183
    #22 0x5abf5cf3d797 in command_service_run_command(Srv_session*, enum_server_command, COM_DATA const*, CHARSET_INFO const*, st_command_service_cbs const*, cs_text_or_binary, void*) mysql-server/sql/command_service.cc:84
    #23 0x5abf60f3c121 in cs::csi_advanced_command(MYSQL*, enum_server_command, unsigned char const*, unsigned long, unsigned char const*, unsigned long, bool, MYSQL_STMT*) mysql-server/sql/server_component/mysql_command_backend.cc:310
    #24 0x5abf5cec7e50 in mysql_send_query mysql-server/sql-common/client.cc:8101
    #25 0x5abf5cec8146 in mysql_real_query mysql-server/sql-common/client.cc:8214
    #26 0x5abf60f35b56 in mysql_command_services_imp::query(MYSQL_H_imp*, char const*, unsigned long) mysql-server/sql/server_component/mysql_command_services_imp.cc:690
    #27 0x74510ea659c9 in test_mysql_command_services_udf mysql-server/components/test/test_mysql_command_services.cc:123
    #28 0x5abf5d417e06 in udf_handler::val_str(String*, String*) mysql-server/sql/item_func.cc:4998
    #29 0x5abf5d4189f3 in Item_func_udf_str::val_str(String*) mysql-server/sql/item_func.cc:5263

SUMMARY: AddressSanitizer: 1712 byte(s) leaked in 1 allocation(s).