Skip to content

"Deadlock found when trying to get lock" in file locking #20555

Closed

Description

@LukasReschke

We have a ton of following entries, they appear dozens to hundreds of times per second:

{"reqId":"96M5ZtaObvQI\/q+FUTeB","remoteAddr":"93.202.76.110","app":"core","message":"Exception while scanning file \"msrex\/files_versions\/owncloud\/support\/XXXX\/XXXX/XXXX9\": An exception occurred while executing 'INSERT INTO `oc_file_locks` (`key`,`lock`,`ttl`) SELECT ?,?,? FROM `oc_file_locks` WHERE `key` = ? HAVING COUNT(*) = 0' with params [\"files\\\/7a4d0ec119d9e99086554694bf6fa2bc\", 1, 1447758814, \"files\\\/7a4d0ec119d9e99086554694bf6fa2bc\"]:\n\nSQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction","level":0,"time":"2015-11-17T10:13:34+00:00","method":"GET","url":"\/owncloud\/index.php\/apps\/files\/ajax\/scan.php?force=false&dir=&requesttoken=TOKEN"}

@cmonteroluque @karlitschek @PVince81 FYI - this is what is killing S3 right now. I'd vote for priority on this once.
cc @icewind1991 THX!
cc @danimo

Activity

  1. added this to the milestone on Nov 17, 2015
  2. LukasReschke commented on Nov 17, 2015

    @LukasReschke
    ContributorAuthor

    I had to hack the apps/files/ajax/scan.php file to not do anything to provide at least some kind of server uptime. This should not hurt the server but is a big hack.

    (somewhat related to #20545)

  3. nickvergessen commented on Nov 17, 2015

    @nickvergessen
    Contributor
    MariaDB [(none)]> SELECT @@GLOBAL.tx_isolation, @@tx_isolation
        -> ;
    +-----------------------+-----------------+
    | @@GLOBAL.tx_isolation | @@tx_isolation  |
    +-----------------------+-----------------+
    | REPEATABLE-READ       | REPEATABLE-READ |
    +-----------------------+-----------------+
    1 row in set (0.00 sec)
    

    As per our docs we need READ_COMMITED
    So running

    SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
    SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

    should fix the problem

  4. DeepDiver1975 commented on Nov 17, 2015

    @DeepDiver1975
    Contributor

    As per our docs we need READ_COMMITED
    So running

    SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
    SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

    should fix the problem

    We are setting the transaction level in the ctor of the connection - what's going wrong here?

  5. DeepDiver1975 commented on Nov 17, 2015

    @DeepDiver1975
    Contributor

    ctor of the connection

    parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED);

  6. nickvergessen commented on Nov 17, 2015

    @nickvergessen
    Contributor

    We are setting the transaction level in the ctor of the connection - what's going wrong here?

    maybe the account we use is not allowed to run this command?

  7. DeepDiver1975 commented on Nov 17, 2015

    @DeepDiver1975
    Contributor

    maybe the account we use is not allowed to run this command?
    @nickvergessen

    we explicit set the transaction level on each connection we open

  8. icewind1991 commented on Nov 17, 2015

    @icewind1991
    Contributor

    Try running

    <?php
    
    require('lib/base.php');
    
    $connection = \OC::$server->getDatabaseConnection();
    $result = $connection->executeQuery('SELECT @@GLOBAL.tx_isolation, @@tx_isolation');
    var_dump($result->fetchAll());

    To check that the transaction level is set correctly

  9. LukasReschke commented on Nov 17, 2015

    @LukasReschke
    ContributorAuthor
    array(1) {
      [0]=>
      array(2) {
        ["@@GLOBAL.tx_isolation"]=>
        string(15) "REPEATABLE-READ"
        ["@@tx_isolation"]=>
        string(14) "READ-COMMITTED"
      }
    }
    

    This is 5.5.45-MariaDB from OpenSUSE.

  10. 39 remaining items

  11. icewind1991 commented on Apr 22, 2016

    @icewind1991
    Contributor

    @PVince81 can you try 6881034

  12. PVince81 commented on Apr 22, 2016

    @PVince81
    Contributor

    Turns out I wasn't able to reproduce the original issue without this commit. It only happened once.

    But this variation of the steps where cadaver B (the move) runs first and implicitly scans followed by cadaver A produces the deadlock errors.

    Here are the updated steps:

    1. Check out v9.0.1
    2. Create a folder "test"
    3. Upload about 70 files into "test"
    4. Close the web UI to avoid heartbeat/getstoragestats interference
    5. Edit https://github.com/owncloud/core/blob/v9.0.1/lib/private/files/cache/scanner.php#L138 and add sleep(1) AFTER acquireLock to make sure the scanner lock takes longer. I also added a logging statement that logs $file so I can observe the progress.
    6. Open two cadaver sessions A and B
    7. In config.php, set "filesystem_check_changes" to 1 to enable update detection (as it would be the case on ext storages too)
    8. Directly in the data directory, "touch data/admin/files data/admin/test* data/admin/test_/_" to trigger update detection
    9. In cadaver B, rename: "mv test test2"
    10. While cadaver B is running, wait about 4 seconds, then go to cadaver A and do a "ls test"
    11. Observe owncloud.log

    For every file that is to be scanned I see the deadlock message appearing.
    And this is consistent!

    Even weirder is that at the end, cadaver A still fails with "423 locked" and in the end the folder did not get renamed.

    Next step: @icewind1991's commit

  13. PVince81 commented on Apr 22, 2016

    @PVince81
    Contributor

    @icewind1991 I tested with your commit with both steps and could not make the deadlock issue appear again:

    1. ls before mv ("Deadlock found when trying to get lock" in file locking #20555 (comment))
    • Before the fix: both operations stayed stuck, then one of them might time out. Sometime deadlock.
    • After the fix: the move always fails with "423 Locked" while the "ls" is still running. This is how I expect it to work.
    1. mv before ls ("Deadlock found when trying to get lock" in file locking #20555 (comment))
    • Before the fix: deadlock error on one or all files, mv still finished with "423 locked".
    • After the fix: mv continues undisturbed and ls directly returns the old file list and never blocks!

    I tried those several times (before and after).

    @icewind1991 it looks like your commit fixes the issue ! (or at least my version of it)
    So it looks like the scanner itself needs its own exclusive look to prevent double-scanning ?

  14. nickvergessen commented on Apr 25, 2016

    @nickvergessen
    Contributor

    In mysql client on the CLI:

    MariaDB [(none)]> SELECT @@GLOBAL.tx_isolation, @@tx_isolation
        -> ;
    +-----------------------+-----------------+
    | @@GLOBAL.tx_isolation | @@tx_isolation  |
    +-----------------------+-----------------+
    | REPEATABLE-READ       | REPEATABLE-READ |
    +-----------------------+-----------------+
    1 row in set (0.00 sec)
    

    Which is wrong and which is what I said last week already.

    #20555 (comment)

    As per our docs we need READ_COMMITED
    So running

    SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
    SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

    should fix the problem

    That should then cause an immediate return with "locked"

  15. nickvergessen commented on Apr 25, 2016

    @nickvergessen
    Contributor

    Please verify and then close the issue

  16. PVince81 commented on Apr 25, 2016

    @PVince81
    Contributor

    As advised, I now ran the commands in CLI mysql and after restarting the CLI session it seems that the values were kept (contrary to my previous belief).

    After following the steps from #20555 (comment) the "ls" call still blocks and the log still shows

    {"reqId":"f5Qq6fIDKMny4nuH7CV\/","remoteAddr":"127.0.0.1","app":"core","message":"Exception while scanning file \"files\/test\/P1100409.JPG\": An exception occurred while executing 'UPDATE `oc_file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0' with params [1461574469, \"files\\\/09915c88bfd09feb443462ab2efdaed9\"]:\n\nSQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction","level":0,"time":"2016-04-25T07:55:20+00:00","method":"PROPFIND","url":"\/owncloud\/remote.php\/webdav\/test\/","user":"admin"}
    
  17. PVince81 commented on Apr 25, 2016

    @PVince81
    Contributor

    Retested both cases with @icewind1991's PR #24205 and with the proper transaction mode, it still works fine in a non-blocking manner (returns 423 Locked directly)

  18. modified the milestones: , on Apr 25, 2016
  19. PVince81 commented on Apr 26, 2016

    @PVince81
    Contributor

    It looks like this deadlock was also reproducible with two concurrent "ls" aka PROPFIND on the to-be-rescanned folder: #24260 (comment).

    Two concurrent PROPFINDs is more likely to be the cause of this error happening often, since multiple clients might query for changes. This, more often than a MOVE + PROPFIND.

    The PR above fixes that too.

  20. lock commented on Aug 4, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  21. locked as resolved and limited conversation to collaborators on Aug 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions