Yii2: YII in nas hard disk has file lock issue

Created on 13 Dec 2019  路  27Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

What is the expected result?

What do you get instead?

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.14
| PHP version | 7.2.19
| Operating system | CentOS

Yii in Nas hard disk has file lock stuck issue
The above problems will occur in the following file write operations
1.Log
2.Runtime
3.assets
Now moving the Runtime Log file directory through configuration items but assets did not find a temporary solution

need more info bug

Most helpful comment

Many NAS use the SMB protocol which is also known to have file locking issues:
https://serverfault.com/questions/204812/how-to-prevent-samba-from-holding-a-file-lock-after-a-client-disconnects

Actually I would question a setup where a NAS drive is used for logging. You'd never want to do something like that in a production setup. If space is limited you should better move the rotated logfiles to the NAS (e.g. in a cronjob) and keep logfiles on the machine.

All 27 comments

Do the directories have the correct permissions set? They must be writable for the webserver user (in most cases www-data).
Try chmod -R 775 <your_web_root>/runtime on the commandline (same with web/assets)

I now have three node serversPHP code is stored on NAS shared hard diskWe currently have the problem that when YII writes log file data, multiple computers write at the same time, the file is locked and cannot be recoveredOnly 504 timeout

Has nothing to do with file permissions
Instead, multiple nodes write a log file at the same time, causing the file to be locked

Then this seems to be an issue of your infrastructure configuration. As far as I know, Yii does not perform any file locking.
Can you confirm, @samdark ?
You could try to log to a dedicated DB/Schema instead of a single file.

Are you trying to write to a file from different processes at the same time without synchronization? What do you expect?

I mean
If multiple processes write logs
The flock in the yii log will be stuck and will not be restored! It will not be restored!

@xiaoyi510 would you please tell us more about your setup? How these nodes are configured? Is process manager used? Are these on separate IPs? What is the file system?

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

Code in
vendor/yiisoft/yii2/log/FileTarget.php:111

    /**
     * Writes log messages to a file.
     * Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
     * @throws InvalidConfigException if unable to open the log file for writing
     * @throws LogRuntimeException if unable to write complete log to file
     */
    public function export()
    {
        $logPath = dirname($this->logFile);
        FileHelper::createDirectory($logPath, $this->dirMode, true);

        $text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
        if (($fp = @fopen($this->logFile, 'a')) === false) {
            throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
        }
        @flock($fp, LOCK_EX);
        if ($this->enableRotation) {
            // clear stat cache to ensure getting the real current file size and not a cached one
            // this may result in rotating twice when cached file size is used on subsequent calls
            clearstatcache();
        }

@flock($fp, LOCK_EX);
Write log this line of code will be stuck in multiple processes

The code is not the best.

I suggessts

$fp = fopen(...);
if(!...) { throw }
try {
  @flock($fp, LOCK_EX);
  ... throw ... throw ...
} finally {
            @flock($fp, LOCK_UN);
            @fclose($fp);
}
if(...) {
  $this->roratelog() ...
}

Write log this line of code will be stuck in multiple processes

In product environment?

Write log this line of code will be stuck in multiple processes

In product environment?

yes
im in test environment not found this bug
This problem was only discovered in my prod environment with NAS hard drives
System on NAS disk Multiple PHP processes stuck at runtime / log and open / written lock files

slow IO, disable debug component in product environment.

@xiaoyi510 would you please answer my questions?

slow IO, disable debug component in product environment.

prod evn closing debug

@xiaoyi510 would you please answer my questions?
@samdark

For configuration items, YII_ENV = prod YII_DEBUG = false
runtime log file path default

I used a load balancer, a total of three nodes, using SSD nas hard disks,

NFS has known limitations regarding locking: https://stackoverflow.com/q/218451/5812455

@rob006 it's NAS, not NFS.

@samdark What do you mean by NAS? NAS is just a server which shares disk storage by network (probably using NFS protocol).

@rob006 it could use iscsi, but @xiaoyi510 I wouldn't use filetarget that way. yii\log\SyslogTarget would be much better solution for you. Better yet, look at Graylog

@rob006 it's NAS, not NFS.

@xiaoyi510 Does NAS run PHP directly or only use it for network storage?

What's the file system used?

What's the file system used?

if it is Centos, then probably XFS, but then again, if file system is mounted over network to NFS/ISCSI/SMB it could be literally anything

@xiaoyi510 sorry but I wasn't able to get/reproduce the problem :(

It could be FS issue but could be Yii code. If so, there should be something like "Unable to export log through file" in the log but it wasn't mentioned.

I encountered the same problem, CentOS 7.6, Kubernetes

Many NAS use the SMB protocol which is also known to have file locking issues:
https://serverfault.com/questions/204812/how-to-prevent-samba-from-holding-a-file-lock-after-a-client-disconnects

Actually I would question a setup where a NAS drive is used for logging. You'd never want to do something like that in a production setup. If space is limited you should better move the rotated logfiles to the NAS (e.g. in a cronjob) and keep logfiles on the machine.

I encountered the same problem, CentOS 7.6, Kubernetes

What PersistentVolume/StorageClass

Was this page helpful?
0 / 5 - 0 ratings