Skip to content

Conversation

@varundeepsaini
Copy link

@varundeepsaini varundeepsaini commented Dec 22, 2025

  • The Jira issue number for this PR is: MDEV-16335

Description

Currently, deadlock information is only available globally via SHOW ENGINE INNODB STATUS, so a client can never be certain that the information shown is for their own session's deadlock.

This patch stores deadlock information in the victim transaction's structure when a deadlock is detected, making it accessible via SHOW WARNINGS.

Implementation

  • Added deadlock_info fields to trx_lock_t to store per-session deadlock information
  • In Deadlock::report(), copy deadlock info to the victim transaction (only when srv_print_all_deadlocks is set)
  • In convert_error_code_to_mysql(), push deadlock info as a warning via push_warning_printf()
  • Clear deadlock info when a new transaction starts or when the transaction is freed

Release Notes

When innodb_print_all_deadlocks=ON, deadlock detail information is now available per-session via SHOW WARNINGS after a deadlock error (ER_LOCK_DEADLOCK).

How can this PR be tested?

./mtr --suite=innodb deadlock_session_info

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the main branch.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@CLAassistant
Copy link

CLAassistant commented Dec 22, 2025

CLA assistant check
All committers have signed the CLA.

@varundeepsaini varundeepsaini marked this pull request as draft December 22, 2025 19:46
@varundeepsaini varundeepsaini force-pushed the MDEV-16335-deadlock-info-show-warnings branch 2 times, most recently from cc12800 to 67920e9 Compare December 22, 2025 21:07
Signed-off-by: Varun Deep Saini <varun.23bcs10048@ms.sst.scaler.com>
@varundeepsaini varundeepsaini force-pushed the MDEV-16335-deadlock-info-show-warnings branch from 67920e9 to 19592c6 Compare December 22, 2025 21:23
@varundeepsaini varundeepsaini force-pushed the MDEV-16335-deadlock-info-show-warnings branch from 63a7517 to 0cf376f Compare December 24, 2025 05:04
@varundeepsaini varundeepsaini marked this pull request as ready for review December 24, 2025 05:22
static_cast<size_t>(len),
lock_latest_err_file);
if (deadlock_info_len && deadlock_info[deadlock_info_len - 1] == '\n')
--deadlock_info_len;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot read more than ftell returned: deadlock_info is a temporary file and we access it under lock.
We can put assert

ut_ad(len >= deadlock_info_len);

maybe +1 somewhere -- please check.

Then this if is not needed, as well as zero assignment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we know is that fread can return only less bytes than ftell did:
https://en.cppreference.com/w/c/io/fread#:~:text=stream%20to%20read-,Return%20value,-Number%20of%20objects

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Dec 29, 2025
Copy link
Contributor

@FooBarrior FooBarrior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Contributor

@dr-m dr-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test for this functionality is only executed in debug-instrumented builds. I think that new functionality had better tested with all builds, so that the functionality will be better covered. For example, GNU/Linux distributions that package MariaDB themselves will likely not use CMAKE_BUILD_TYPE=Debug at all. We should test what we ship.

Comment on lines +7322 to +7325
deadlock_info= static_cast<char*>(ut_malloc_nokey(
static_cast<size_t>(len) + 1));
if (!deadlock_info)
goto func_exit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory will be leaked if !deadlock_info.

Why would we do all this busy work if !current_trx || victim != trx || !trx->mysql_thd?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory will be leaked if !deadlock_info.

Which memory?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean in case of !deadlock_info it's not allocated, and besides ut_free is called unconditionally after func_exit. So do you mean some other memory?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I take my words back regarding deadlock_info == nullptr.


if (!srv_print_all_deadlocks)
goto func_exit;
long len= ftell(lock_latest_err_file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing, why you do these file operations? IMHO it would be better to change current print functions to produce string that is then written to both to the deadlock error file and pushed to warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporary file is a relic from MySQL 5.5 or earlier when InnoDB was written in C and there was no formatted output facility into a heap-allocated buffer. That could be fixed separately. I seemed to remember that there is an option to expose the lock_latest_err_file in the file system, but there only is the Boolean option innodb_status_file that covers the entire SHOW ENGINE INNODB STATUS output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janlindstrom the nearly same is done from the side of SHOW INNODB STATUS -- the file is opened, ftell'd, etc. I think this work may remain local, wihtout the requirement to refactor.

static_cast<size_t>(len),
lock_latest_err_file);
ut_ad(deadlock_info_len <= static_cast<size_t>(len));
if (deadlock_info[deadlock_info_len - 1] == '\n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will crash if deadlock_info_len is 0

@FooBarrior
Copy link
Contributor

You didn't check

I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.

Signed-off-by: Varun Deep Saini <varun.23bcs10048@ms.sst.scaler.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

7 participants