homesteadnfsvagrant-windows

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


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

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.


Solution

  • 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.