Bug #121694 Binlog group commit leaks Global_connection_memory until disconnect

Submitted: 7 Nov 21:20 Modified: 20 Nov 21:26
Reporter: Kate Galieva (OCA) Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Logging Severity:S2 (Serious)
Version:8.4.13, 8.4.11, 9.7.4, 8.0.46 OS:Any
Assigned to: CPU Architecture:Any

[7 Nov 21:20] Kate Galieva
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.
[7 Nov 21:29] Kate Galieva
Repro script (mysql client only): concurrent commits leave Global_connection_memory high until disconnect. See How to repeat.

Attachment: repro.sh (text/x-sh), 6.42 KiB.

[7 Nov 21:30] Kate Galieva
MTR test (mysql-test/t): 4 sessions commit as one group 100 times; asserts Global_connection_memory does not grow.

Attachment: mem_cnt_binlog_group_commit.test (application/octet-stream, text), 2.57 KiB.

[7 Nov 21:30] Kate Galieva
Server options for the MTR test (mysql-test/t): enables global_connection_memory_tracking.

Attachment: mem_cnt_binlog_group_commit-master.opt (application/octet-stream, text), 36 bytes.

[7 Nov 21:31] Kate Galieva
Expected result for the MTR test (mysql-test/r).

Attachment: mem_cnt_binlog_group_commit.result (application/octet-stream, text), 915 bytes.

[16 Nov 17:59] Kate Galieva
Fix for mysql-8.4.13: fixed THD buffer for set_trans_pos() instead of a main_mem_root allocation, plus the MTR test.

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: mysql-8.4.13-trans-pos-fix.patch (application/octet-stream, text), 5.21 KiB.

[20 Nov 21:26] Kate Galieva
Pull request with the fix and the MTR test: https://github.com/mysql/mysql-server/pull/909. The same patch is attached on the Contributions tab."

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.043 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.