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.