简体   繁体   中英

Node.js filesystem save file error 56 EROFS while saving every 2 seconds

I am running node.js on raspbian and trying to save/update a file every 2/3 seconds using the following code:

var saveFileSaving = false;

function loop() {
    mainLoop = setTimeout(function() {
        // update data

        saveSaveFile(data, function() {
            //console.log("Saved data to file");
            loop();
        });
    }, 1500);
}

function saveSaveFile(data, callback) {
    if(!saveFileSaving) {
        saveFileSaving = true;
        var wstream = fs.createWriteStream(path.join(__dirname, 'save.json'));

        wstream.on('finish', function () {
            saveFileSaving = false;
            callback(data);
        });

        wstream.on('error', function (error) {
            console.log(error);
            saveFileSaving = false;
            wstream.end();
            callback(null);
        });

        wstream.write(JSON.stringify(data));
        wstream.end();
    } else {
        callback(null);
    }
}

When I run this it works fine for an hour then starts spitting out:

[25/May/2016 11:3:4 am]  { [Error: EROFS, open '<path to file>']
  errno: 56,
  code: 'EROFS',
  path: '<path to file>' }

I have tried jsonfile plugin which also sends out a similiar write error after an hour.

I have tried both fileSystem.writeFile and fileSystem.writeFileSync both give the same error after an hour.

I was thinking it had to do with the handler not being let go before a new save occurs which is why I started using the saveFileSaving flag.

Resetting the system via hard reset fixes the issue (soft reset does not work as the system seems to be locked up).

Any suggestions guys? I have searched the web and so only found one other question slightly similar from 4 years ago which was left in limbo.

Note: I am using the callback function from the code to continue with the main loop.

I was able to get this working by unlinking the file and saving the file every time I save while it is not pretty it works and shouldn't cause too much overhead.

I also added a backup solution which saves a backup every 5 minutes in case the save file has issues.

Thank you for everyone's help.

Here is my ideas:

1) Check free space when this problem happens by typing in terminal:

df -h

2) Also check if file is editable when problem occurs. with nano or vim and etc.

3) Your code too complicated for simply scheduling data manipulation and writing it to file. Because of even Your file will be busy (saveFileSaving) You will lose data until next iteration, try to use that code:

var 
  async = require('async'),
  fs = require('fs'),
  path = require('path');

async.forever(function(next) {
  // some data manipulation

  try {
    fs.writeFileSync(path.join(__dirname, 'save.json'), JSON.stringify(data));
  }
  catch(ex) {
    console.error('Error writing data to file:', ex);
  }

  setTimeout(next, 2000);
});

4) How about keeping file descriptor open?

var 
  async = require('async'),
  fs = require('fs'),
  path = require('path');

var file = fs.createWriteStream(path.join(__dirname, 'save.json'));

async.forever(function(next) {
  // some data manipulation

  file.write(JSON.stringify(data));
  setTimeout(next, 2000);
});

var handleSignal = function (exc) {
  // close file
  file.end();

  if(exc) {
    console.log('STOPPING PROCESS BECAUSE OF:', exc);
  }
  process.exit(-1);
}

process.on('uncaughtException', handleSignal);
process.on('SIGHUP', handleSignal);

5) hardware or software problems (maybe because of OS drivers) with raspberry's storage controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM