The lockfiles in the lock subdirectory in the moodledata may have missing permissions, such as rw-r--r-- and owned by another user, such as root (!!)
The way I solved this problem is to change the ownership of the lock directory to www-data by the following command.
$ chown -R www-data:www-data /var/www/moodledata/lock
(Please note that the moodledata in my case is under /var/www)