File is locked - how to unlock

I had this issue when I tried to delete a file, but it resulted in an Apache segmentation fault. I fixed this bug and tried to delete the file again but this time it was locked.

So I think the issue is that Nextcloud locks the file before trying delete it and then, if thereā€™s a crash or other error, the lock is never released.

I think the UI should make it clearer what file is locked and provide some way to unlock it. Or, better, have some mechanism to prevent files from being locked and never unlocked afterwards.

1 Like

If this is your own server then you should set up Redis like @tflidd recommends.

Before following this suggestions, I would like to knowā€¦

For what purpose file locking was created ?

I have 40.000 entries in my oc_file_locs table, that seems a little too much for me!
Also wiping out all of this locks, seems a bit dangerous to me just to fix 1 problem (I canā€™t delete a folderā€¦)

1 Like

As far as i know its there, so that you no other process can access the file while it is written, or otherwise processed.

The issue is, that there is maybe a logical inconsistency, since i could not find an ā€œ1ā€ entries under the lock column. If you delete all entries, they will be rebuild automatically when you exit maintenance mode, but this time in a correct order. I tried this on a testsystem, before applying this on our production mashine, and it worked just fine.

Keep in mind, i drew some assoumtions here. I would be happy if someone could either confirm this, or correct me if nessecary.

Thank you for your answer.

I would also like to have some confirmationā€¦ likeā€¦ is it normal to have 40.000 locks in a database?
Is my locking system working good? I have around 100.000 files.

I also have 5 main directories owned by a ā€œadminā€ owner and shared with the others, so everyone uploads files in that directories.
I made this choice so that the ā€œadminā€ user still owns the files if someone go rogue.

Is this the reason why I have 50% of files locked ?

If you have that many locks in your database you have a serious problem. Use Redis for transactional file locking as said in the first post by @tflidd.

If they are stored in your database you donā€™t use transactional locking as described here: : Transactional file locking ā€” Nextcloud 13 Administration Manual 13 documentation

I get the same error message when doing a ā€œocc files:scanā€ for a specific user. The error starts with:

Exception during scan: File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.

Then 21 lines with more specific errors. WhatĀ“s wrong here? How can I perform a ā€œfile system vs. database nc_filecacheā€ comparison?

I get that same error too for one of my users, tried to do occ files:cleanup but it did not help (0 orphaned file cache entries deleted)
How do we go about this issue?

Instructions is in the first post :wink:

Yes of course, and I already did manually disable the locking state beforehand. I had this a while ago already, thatā€™s where I started using redis, but the problem seems to persist
MariaDB says: Query OK, 0 rows affected (0.01 sec)
fsck.ext4 says that everything is fine

Report it on github, seems this is a bug :wink:

crazy, and nextcloud even seems to show files on the web-ui that are not there on the disk - even after a occ files:scan --all
ā€¦ok, I guess Iā€™ll report it. Thanks though!

Hi amo13

I had the same Problems a few times. But I could always fix it with this procedure (retain the order!):

  • occ ā€˜files:scan --allā€™
  • occ ā€˜files:cleanupā€™
  • occ ā€˜maintenance:mode --onā€™
  • mysql -u root -pā€™inheregoesyourpasswordā€™ -D ocdb -e ā€˜delete from oc_file_locks where 1ā€™
  • occ ā€˜maintenance:mode --offā€™
  • restart your Nextcloud webserver and try again -> the files in your web-ui should be gone.

Good Luck, Cheers zisuu

4 Likes

This works for me

1 Like

@tflidd, your manual does not work for me on Ubuntu 16.04.1 server x64 with Nextcloud 13.0.2

Yes, i found FileLocked in nextcloud.log.

Yes, i have redis and apcu installed

config.php:

  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'redis' => array (
              'host' => 'localhost',
              'port' => 6379,
            ),

Yes, i have emptied the table oc_file_locks

ā€˜maintenanceā€™ => true,
DELETE FROM oc_file_locks WHERE 1
ā€˜maintenanceā€™ => false,

Yes, i executed the cron job manually after disabling maintainance mode without any errors

sudo -u www-data php -f /var/www/nextcloud/cron.php

Nevertheless those files are still locked and oc_file_locks is empty.

Can you please explain that behavior or what is to do?

Thank you!

BTW: Why donā€™t add an admin option in the GUI for deleting the file lock?

2 Likes

Give your services/server a bounce (restart). May just be a locked process or something untoward.

The steps outlined by @tflidd will manually force any file locks to be removed by dropping them from the database forcibly. This should normally be taken care of by the system but as is clearly demonstrated throughout the thread it isnā€™t foolproof.

Sorry i forgot to mention that i did a restart of redis and apache already:

service redis restart
service apache2 restart

But this did not unlock the files.

Bounce the server. Could it be theyā€™re locked by something else? Another process on the server accessing the file for some reason or somethingā€¦

I donā€™t think so because iā€™m the only user of the server. Itā€™s hosted on localhost in a virtual machine.

Meanwhile i could remove the file lock by setting

'filelocking.enabled' => false,

temporarely, with the success that the Windows desktop sync client canā€™t sync the folder in question anymore.

Strange!

Provide full screenshot to see the ending of the file :wink: then we might help you more :wink: