Skip to content

Hangs forever on multiple simultaneous calls #79

Description

@paragi

Thank you for at very nice extension!
I have a critical issue I need to have solved:

The following example code, updates and returns a counter, stored in a file.
If I call the function more than a handful of times, it hangs forever, apparently in the fs.read function.
Would you please look into this?

var fs = require('fs-ext');
const counter = (callback) => {
  fs.open('counter.txt',"r+", (err,fd) => {
    if(err) throw err;
    fs.flock(fd, 'ex', (err) => {
      if(err) throw err;
      let buffer = Buffer.alloc(20);
      fs.read(fd, buffer,0, buffer.length, 0 , (err, readCount, buffer) => {
        if(err) throw err;
        var count = 1 + (parseInt(buffer.toString('utf8',0,readCount)) || 0);
        fs.write(fd,count + " ",0,(err) => {
          if (err) throw err;
          fs.close(fd, () =>{
            if(typeof callback == 'function')  callback(count);
          });
        });
      });
    });
  });
};

fs.writeFile('counter.txt',' '.repeat(20),(err) => {
  for(let i=0; i<100; i++)
    counter(console.log);
});

A appreciate the effort and would like to suggest some other useful add-ons.
My understanding of the node framework and windows is minimal. But if you are interested, I might find time to write some useful extension in C++. Would you be interested in integrating that?

Best regrads
Simon Rigèt

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions