Description:
With global_connection_memory_tracking=ON and the binary log enabled, concurrent commits leave memory charged to Global_connection_memory that is never released while the connection stays open. COM_RESET_CONNECTION does not release it either; only disconnecting does. The memory is not actually in use.
On a busy server with long-lived pooled connections, the counter grows until it reaches global_connection_memory_limit, and the server then closes connections with error 4081 (ER_DA_GLOBAL_CONN_LIMIT). We hit this in production on Percona Server 8.4.11 (the code involved is identical in MySQL): the counter went from about 0 to 25 GiB in 8 minutes during a burst of small transactions, while mysqld RSS stayed flat.
Cause (line numbers from 8.4.13):
1. In process_flush_stage_queue() the group commit leader calls flush_thread_caches(head) for each follower under Thd_backup_and_restore (sql/binlog.cc:8559).
2. Thd_backup_and_restore calls set_mem_cnt_THD(follower) (sql/binlog.cc:661), so memory allocated in the leader's OS thread is charged to the follower's THD counter. The PFS thread stays the leader's.
3. While flushing, THD::set_trans_pos() allocates FN_REFLEN + 1 bytes from the follower's main_mem_root (sql/sql_class.h:2688). When that needs a new mem_root block, pfs_memory_alloc_vc() charges the block to the follower's counter (storage/perfschema/pfs.cc:8421) but records the leader's PFS thread as the owner (storage/perfschema/pfs.cc:8445).
4. The follower later frees the block in its own thread. pfs_memory_free_vc() only calls mem_cnt_free() (storage/perfschema/pfs.cc:8737) when the current PFS thread is the owner (storage/perfschema/pfs.cc:8715), so the charge is never removed.
The leak happens only when the 513-byte allocation needs a new mem_root block, so the rate depends on statement shape and query_alloc_block_size. The accounting involved was added with the connection memory limits in 8.0.28 (WL#13458).
How to repeat:
Start the server with --global-connection-memory-tracking=ON (binlog on, the default). Two ways to repeat, both attached.
1. repro.sh (mysql client only)
repro.sh sets query_alloc_block_size=1024 (BLOCK_SIZE= keeps the server's value), creates a regular user and opens 64 connections as that user. Each runs TXS transactions "BEGIN; INSERT ... AS new ON DUPLICATE KEY UPDATE ...; COMMIT" as one request on a 15-column table, sends COM_RESET_CONNECTION, and then stays idle. While the connections are idle it prints Global_connection_memory, the memory/sql/THD::main_mem_root bytes in use according to performance_schema, and the result of one query from a new connection with global_connection_memory_limit lowered to 256 MiB. It then disconnects the connections and prints the same again. It restores the settings and drops its user and schema at the end.
MYSQL_ARGS="-uroot -pPASSWORD -h127.0.0.1 -P3306" TXS=1000 ./repro.sh
Official mysql:8.4 (8.4.13) docker image in a container limited to 1.5 GiB of memory, started with only --global-connection-memory-tracking=ON. Values while the connections are idle, after COM_RESET_CONNECTION:
Transactions Global_connection_memory main_mem_root in use Container memory New query, limit 256 MiB
16,000 153 MiB 1.4 MiB 588 MB ok
32,000 304 MiB 1.4 MiB 672 MB connection closed
64,000 603 MiB 1.4 MiB 684 MB connection closed
128,000 1,205 MiB 1.4 MiB 689 MB connection closed
256,000 2,410 MiB 1.4 MiB 692 MB connection closed
The counter grows linearly with the number of commits and reaches more than the container's memory limit, while the memory it is charging for is not in use. The server logs, for the 256,000 case:
[ERROR] [MY-013832] [Server] Connection closed. Global connection memory limit 268435456 bytes exceeded. Consumed 2526679952 bytes.
After the connections disconnect the counter returns to 0 and the same query succeeds, in every case.
Other official docker images, 64,000 transactions, Global_connection_memory while the connections are idle:
MySQL 8.4.11 553.5 MiB
MySQL 9.7.4 264.2 MiB
MySQL 8.0.46 36.7 MiB with the default query_alloc_block_size (1.5 MiB with 1024)
Which statement shapes and block sizes trigger it differs between versions. With a single connection (no commit group) it does not leak: 0.1 MiB after 20,000 transactions on 8.4.13.
2. MTR test
mem_cnt_binlog_group_commit.test (with -master.opt and .result, for mysql-test/t and mysql-test/r) sets query_alloc_block_size=1024 and uses binlog_group_commit_sync_delay and binlog_group_commit_sync_no_delay_count=4 to make 4 sessions commit as one group, 100 times. It asserts that Global_connection_memory does not grow by more than 64 KiB. On 8.4.13 it fails 10 of 10 runs without a fix, with about 460 KiB left charged.
Suggested fix:
Give THD a fixed buffer for the binlog file name used by set_trans_pos() (m_trans_fixed_log_file) instead of allocating it from main_mem_root, so nothing is allocated from the follower's mem_root during the leader's flush. THD::cleanup_after_query() still resets m_trans_fixed_log_file to nullptr, so readers behave as before. This adds 513 bytes per THD. Because nothing is allocated at all, the stored procedure case from Bug #75879 (memory growth from this allocation in long loops) stays fixed. A patch with this change and the MTR test will follow through the Contributions tab.
Tested on MySQL 8.4.13 (Release and Debug builds):
- repro.sh: 445 MiB without the fix, 1.5 MiB with it.
- The MTR test fails 10 of 10 runs without the fix and passes 60 of 60 with it.
- A stored procedure inserting 20,000 rows in a loop holds the same main_mem_root (18 KiB) as one inserting 100, with and without the fix.
- The binlog, binlog_gtid, binlog_nogtid and perfschema suites, main.mem_cnt_* and rpl*.rpl_semi_sync* give the same results with and without the fix, apart from the new test.
Workaround: global_connection_memory_tracking=OFF, or periodically reconnect pooled connections.
Description: With global_connection_memory_tracking=ON and the binary log enabled, concurrent commits leave memory charged to Global_connection_memory that is never released while the connection stays open. COM_RESET_CONNECTION does not release it either; only disconnecting does. The memory is not actually in use. On a busy server with long-lived pooled connections, the counter grows until it reaches global_connection_memory_limit, and the server then closes connections with error 4081 (ER_DA_GLOBAL_CONN_LIMIT). We hit this in production on Percona Server 8.4.11 (the code involved is identical in MySQL): the counter went from about 0 to 25 GiB in 8 minutes during a burst of small transactions, while mysqld RSS stayed flat. Cause (line numbers from 8.4.13): 1. In process_flush_stage_queue() the group commit leader calls flush_thread_caches(head) for each follower under Thd_backup_and_restore (sql/binlog.cc:8559). 2. Thd_backup_and_restore calls set_mem_cnt_THD(follower) (sql/binlog.cc:661), so memory allocated in the leader's OS thread is charged to the follower's THD counter. The PFS thread stays the leader's. 3. While flushing, THD::set_trans_pos() allocates FN_REFLEN + 1 bytes from the follower's main_mem_root (sql/sql_class.h:2688). When that needs a new mem_root block, pfs_memory_alloc_vc() charges the block to the follower's counter (storage/perfschema/pfs.cc:8421) but records the leader's PFS thread as the owner (storage/perfschema/pfs.cc:8445). 4. The follower later frees the block in its own thread. pfs_memory_free_vc() only calls mem_cnt_free() (storage/perfschema/pfs.cc:8737) when the current PFS thread is the owner (storage/perfschema/pfs.cc:8715), so the charge is never removed. The leak happens only when the 513-byte allocation needs a new mem_root block, so the rate depends on statement shape and query_alloc_block_size. The accounting involved was added with the connection memory limits in 8.0.28 (WL#13458). How to repeat: Start the server with --global-connection-memory-tracking=ON (binlog on, the default). Two ways to repeat, both attached. 1. repro.sh (mysql client only) repro.sh sets query_alloc_block_size=1024 (BLOCK_SIZE= keeps the server's value), creates a regular user and opens 64 connections as that user. Each runs TXS transactions "BEGIN; INSERT ... AS new ON DUPLICATE KEY UPDATE ...; COMMIT" as one request on a 15-column table, sends COM_RESET_CONNECTION, and then stays idle. While the connections are idle it prints Global_connection_memory, the memory/sql/THD::main_mem_root bytes in use according to performance_schema, and the result of one query from a new connection with global_connection_memory_limit lowered to 256 MiB. It then disconnects the connections and prints the same again. It restores the settings and drops its user and schema at the end. MYSQL_ARGS="-uroot -pPASSWORD -h127.0.0.1 -P3306" TXS=1000 ./repro.sh Official mysql:8.4 (8.4.13) docker image in a container limited to 1.5 GiB of memory, started with only --global-connection-memory-tracking=ON. Values while the connections are idle, after COM_RESET_CONNECTION: Transactions Global_connection_memory main_mem_root in use Container memory New query, limit 256 MiB 16,000 153 MiB 1.4 MiB 588 MB ok 32,000 304 MiB 1.4 MiB 672 MB connection closed 64,000 603 MiB 1.4 MiB 684 MB connection closed 128,000 1,205 MiB 1.4 MiB 689 MB connection closed 256,000 2,410 MiB 1.4 MiB 692 MB connection closed The counter grows linearly with the number of commits and reaches more than the container's memory limit, while the memory it is charging for is not in use. The server logs, for the 256,000 case: [ERROR] [MY-013832] [Server] Connection closed. Global connection memory limit 268435456 bytes exceeded. Consumed 2526679952 bytes. After the connections disconnect the counter returns to 0 and the same query succeeds, in every case. Other official docker images, 64,000 transactions, Global_connection_memory while the connections are idle: MySQL 8.4.11 553.5 MiB MySQL 9.7.4 264.2 MiB MySQL 8.0.46 36.7 MiB with the default query_alloc_block_size (1.5 MiB with 1024) Which statement shapes and block sizes trigger it differs between versions. With a single connection (no commit group) it does not leak: 0.1 MiB after 20,000 transactions on 8.4.13. 2. MTR test mem_cnt_binlog_group_commit.test (with -master.opt and .result, for mysql-test/t and mysql-test/r) sets query_alloc_block_size=1024 and uses binlog_group_commit_sync_delay and binlog_group_commit_sync_no_delay_count=4 to make 4 sessions commit as one group, 100 times. It asserts that Global_connection_memory does not grow by more than 64 KiB. On 8.4.13 it fails 10 of 10 runs without a fix, with about 460 KiB left charged. Suggested fix: Give THD a fixed buffer for the binlog file name used by set_trans_pos() (m_trans_fixed_log_file) instead of allocating it from main_mem_root, so nothing is allocated from the follower's mem_root during the leader's flush. THD::cleanup_after_query() still resets m_trans_fixed_log_file to nullptr, so readers behave as before. This adds 513 bytes per THD. Because nothing is allocated at all, the stored procedure case from Bug #75879 (memory growth from this allocation in long loops) stays fixed. A patch with this change and the MTR test will follow through the Contributions tab. Tested on MySQL 8.4.13 (Release and Debug builds): - repro.sh: 445 MiB without the fix, 1.5 MiB with it. - The MTR test fails 10 of 10 runs without the fix and passes 60 of 60 with it. - A stored procedure inserting 20,000 rows in a loop holds the same main_mem_root (18 KiB) as one inserting 100, with and without the fix. - The binlog, binlog_gtid, binlog_nogtid and perfschema suites, main.mem_cnt_* and rpl*.rpl_semi_sync* give the same results with and without the fix, apart from the new test. Workaround: global_connection_memory_tracking=OFF, or periodically reconnect pooled connections.