fwrite(): write of XX bytes failed with errno=5 Input/output error

7.1k Views Asked by At

I had 2 similar questions before, however after more debugging I came to the conclusion the problem was (probably) not within my own code.

In my code I am trying to unzip a gzipped file, for this I wrote a small method;

<?php

namespace App\Helpers;

class Gzip
{

    public static function unzip($filePath)
    {
        $outFilePath = str_replace('.gz', '', $filePath);

        // Open our files (in binary mode)
        $file = gzopen($filePath, 'rb');
        $outFile = fopen($outFilePath, 'wb');

        // Keep repeating until the end of the input file
        while (!gzeof($file)) {
            // Read buffer-size bytes
            // Both fwrite and gzread and binary-safe
            fwrite($outFile, gzread($file, 4096));
        }

        // Files are done, close files
        fclose($outFile);
        gzclose($file);
    }
}

This should result in the unzipped file;

Gzip::unzip('path/to/file.csv.gz');

This is where it gets tricky, sometimes it will unzip the file and sometimes it will throw this exception; (keep in mind that this has nothing to do with the StreamHandler itself, this is a pure input/output error problem) exception fwrite

I can refresh the page as many times as I want but nothing will change, if I would try the gunzip command on the command line it will fail with sort off the same error; gunzip error

  • Which file I am unzipping does not matter, it randomly happens to a random file.

Now it also won't matter if I run the gunzip command multiple times, but like I said these exceptions / errors happen randomly so they also randomly "fix" them self.

The application is written in Laravel 8.0, PHP7.4 running on a Homestead environment (Ubuntu 18.04.5 LTS) my base laptop runs on Windows 10.

To me it's super weird that this exception / error happens randomly and also randomly out of nowhere "fixes" itself, so my question is: how does this happen, why does this happen and ultimately how can I fix it.

4

There are 4 best solutions below

0
frogeyedman On BEST ANSWER

The problem relied in me using Homestead (a Vagrant box) with NFS turned on, Vagrant + NFS + Windows = problems. There are many possible solutions to the problem, most exceptions regarding a errno5 come down to NFS + Vagrant.

The solution for me was to stop using NFS, for now this will be the accepted answer as this fixes my problem. However if someone manages to find a actual solution to this error I will accept that.

3
Been Kyung-yoon On

errno=5 Input/output error is a failure to read/write the Linux file system.

A real server, you need to check the disk with fsck, etc...

Homestead running on Windows, I think we should look for the windows 10 homestead errno -5 issue.

winnfsd - https://github.com/winnfsd/vagrant-winnfsd/issues/96#issuecomment-336105685

0
Maetti79 On

If your Vagrant on Windows is using VirtualBox, HyperV can course this.

Try to disable HyperV for VirtualBox on the powershell, and reboot Windows

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Hypervisor

regards

0
Yevgeniy Afanasyev On

I solved it and I keep using NFS.

This situation happens to me usually when I'm dropping a database schema or creating a new one and fill it with a lot of data on my virtual box. I'm talking about volumes like around 50 Megabytes. I guess this is enough for virtual box to start re-scaling the virtual hard disc and it makes Ubuntu crazy and Kernel panicking.

So the solution is to reboot vagrant as many times as it takes for it to fix the issue.

That is what usually work for me:

  • make vagrant halt - it would go with errors
  • then vagrant up - it probably would not work
  • them vagrant halt - it probably would go with errors again
  • then vagrant up --provision - it would take time and probably also give errors
  • then vagrant halt - it should work this time
  • then vagrant up --provision - because "why not provisioning it again" and it is usually enough.

In 9 out of 10 cases it is enough. When it is not enough then I just create a new homestead.