0

I am trying to copy a file to a NFS mounted directory using nodejs (basic working code below). While the file is successfully copied to the NFS mount, the promise chain breaks without throwing any error (nothing else in the promise chain executes). However, the web server shows a 502 bad gateway indicating that some error has occurred.

As basic precautions, I have ensured that the UID and GID of the user on the NAS are the same for the client.

const fs = require('fs');

function myFunction(fileToCopy) {
    return Promise.resolve()
        .then(() => new Promise((resolve, reject) => {
            let newFileName = uuid() + '.txt';
            let fileReadStream = fs.createReadStream(fileToCopy),
            fileWriteStream = fs.createWriteStream('/mnt/' + newFileName, { flags: 'w+', mode: 0o664 });

            fileReadStream
                .pipe(
                    fileWriteStream,
                    {end: false}
                );

            fileReadStream
                .on('end', () => {
                    console.log('end event copying file (logged)');
                    fileWriteStream.end();

                })
                .on('error', error => {
                    console.error('error reading image stream (not logged)');
                    fileWriteStream.end();
                    reject(error);
                });

            fileWriteStream
                .on('finish', () => {
                    console.log('finished copying file (logged)');
                    resolve(fullFilename);
                })
                .on('error', error => {
                    console.log('error writing file (not logged)');
                    reject(error);
                })
        }))
}

Promise.resolve()
    .then(() => {
        let array = ['file1', 'file2', 'file3'];
        return Promise.all(
            array.map(file => myFunction(file))
        );
    })
    .then(() => console.log('never gets logged'))
    .catch(error => console.error('error never gets logged'));

Edit 1: As a bit of additional information, if the NFS NAS directory is unmounted, leaving /mnt on the local system, the above code works (the promise chain finishes, logging never gets logged).

Edit 2: modified code to include forced 'end' event and added some additional code to the minimum working example to show more of the context.

Informagician
  • 305
  • 3
  • 15
  • And you see `finished copying file` in the console? – t.niese Dec 22 '19 at 05:33
  • Yes, that is the weirdest part, everything behaves as if there shouldn't be an error. Right up until there is. – Informagician Dec 22 '19 at 05:35
  • Does it make a difference if you return `return new Promise((resolve, reject) =>` from `myFunction` instead? – lagerone Dec 22 '19 at 08:08
  • Then your code is highly misleading because that indicates that implies that those two logs at the end are **never** called. Anyhow, if NFS is mounted you get none of the four logs in the shown code? Does the app keep running in that case or is it crashing/restarting? Do you see any other error message in the console? – t.niese Dec 22 '19 at 13:34
  • @lagerone There is actually a large promise chain at the top with error checking that works. There are several self-creates promises above. This is a minimum working example – Informagician Dec 22 '19 at 14:09
  • Are you sure there's a "finish" event? Try "end". – Roamer-1888 Dec 22 '19 at 14:14
  • @t.niese I do get the `finished copying file log` every time, and the file does make it to the destination. However, it is true that I never received the other logs (the next promise in the code has a `console.log` before anything else). The app crashes from the perspective of the 502 error web server as mentioned in the OP, but no other error. I can provide some of the NFS settings if that would help. – Informagician Dec 22 '19 at 14:16
  • Thanks @Roamer1888! I'll give that a shot. Would you put that in the `finish` callback? – Informagician Dec 22 '19 at 14:17
  • Also, the documentation doesn't make it clear what `.pipe()` returns so you might try rearranging the chain to `fs.createReadStream().on(...).on(...).pipe(...);`. Probably no difference but worth a try. – Roamer-1888 Dec 22 '19 at 14:19
  • If you get `console.log('finished copying file')` every time but in some cases neither `console.log('never gets logged')` nor `console.error('error never gets logged', error)` then the problem is at a different point in the code, this is at least not a behavior that the shown code could produce. There has to be some code between the `.then(() => new Promise((resolve, reject) => { … })` and the `.then(() => console.log('never gets logged'))` that is responsible for this. (Or someting in the `.then(() => new Promise((resolve, reject) => { … })` itself that you don't show here. … – t.niese Dec 22 '19 at 14:20
  • Just replace "finish" with "end". – Roamer-1888 Dec 22 '19 at 14:20
  • … or even some other code interleaving, that causes an error/crash. – t.niese Dec 22 '19 at 14:24

0 Answers0