Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fs.readFile sometimes returns bad data #25510

Closed
jhihn opened this issue Jun 11, 2015 · 2 comments
Closed

fs.readFile sometimes returns bad data #25510

jhihn opened this issue Jun 11, 2015 · 2 comments

Comments

@jhihn
Copy link

jhihn commented Jun 11, 2015

handleJson: 033ec1fb-e5db-4649-b0fe-546cb4a8a74e.json
 , 0.54491000000000001], [0.216, 0.55078000000000005]], [[0.23200000 0.54503000
                                                                     ^
 SyntaxError: Unexpected number
   at Object.parse (native)
    at /home/jason/p4/server/node/watcher/watcher.js:149:20
    at fs.js:336:14
    at FSReqWrap.oncomplete (fs.js:99:15)

I am having an issue where when I read in a JSON file, it fails to parse. Note that "[[0.23200000 0.54503000" is not valid JSON, it is missing a comma. When I grep the file for that content, it is not found. Calling my handleJson function again on the same file will work. The file size is 282kB.

The handleJson function is called from a fs.watch event, However when the file is created, it is first written out to another directory, closed, then moved to this directory. Both locations are on the same filesystem so the move is atomic. Therefore, it's not a partial file issue. (Even so, node missing a comma in all that should never happen. It should not skip a byte and keep reading.) The json is being written by a well-tested JSON serializer.

function handleJson(jsonFile, next) { 
    // jsonFile = 'AA.json'
    console.log("handleJson:", jsonFile);
    if (fs.existsSync(jsonFile)) {
        fs.readFile(jsonFile, {encoding: 'utf8'}, function (err, data){
            if (err) {
                console.log(err);
            } else {
                var obj = JSON.parse(data);
...

This is node v12.0

@jasnell
Copy link
Member

jasnell commented Jun 24, 2015

Are you able to provide a complete test case the reliably reproduces the problem (even if only periodically)

@jhihn
Copy link
Author

jhihn commented Jun 24, 2015

The snippet above is the relevant code. Remarkably simple. I will work on something that can produce it. But it only happens about out of 100 invocations. Might be related to SSD or kernel version or something.... I'll update here wheb i have more.

@Trott Trott closed this as completed Apr 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants