MaxListenersExceededWarning in case I try to access to protectd directory of file
Hi everyone,
I'm developing in React on Electron and I encountered a problem, that is, once I obtained the list of directories with sftp.list, trying to iterate on the individual directories (data.map(async (el)=> await console.log(el.name))), If you encounter a protected directory "Permission denied" you will get the following error: "(node:49627) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added to [Client]. Use emitter.setMaxListeners() to increase limit". Questo è il mio codice:
try {
await sftp.connect({
host: args.host,
port: Number(args.port),
username: args.username,
password: args.password,
readyTimeout: 10000
});
const path = '/home/' + args.username;
const data = await sftp.list(path);
list = data.map(async (el)=> await console.log(el.name));
} catch (err) {
await sftp.end();
// await connectToDFTPServer(args, mainWindow);
}
Can you tell me what causes it?
Your problem description and example code don't make sense. You say you get back the listing data. The 'data' returned from the call to list() is just an array of data, it cannot generate a permission denied error as it is just an array of objects with string values and integer values.Also, iterating over that data does not add any listeners to any emitter, so itterating over the data structure and just doing a console.log cannot cause the errors your seeing. It isn't made up of directory handlers, just objects with information about each 'item' in a directory (file or directory).
Out of interest, what is connectToDFTPServer(args, mainWindow)? Could it actually be the source of the errors?
Note that array comprehension methods like map(), filter(), reduce() etc are not async/await safe. you have to take additional measures to use these methods with asynchronous APIs. Suggest you just use a standard for loop and ensure you do a await for each call to list().
There has to be somthing else going on to be generating the error your seeing. Suggest you dig a bit further. Perhaps create just a straight non-electron script using ssh2-ftp-client to query the remote sftp server to verify your basic script is correct. If that works fine, then you know the issue is something inside electron or how you are communicating inside electron.
Hi, thanks for the reply. @theophilusx
So as for connectToDFTPServer, I forgot to comment on it here in the GitHub Issue (in my code is commented). I also modified the code in the map by making it asynchronous and adding an await for each console.log to make sure that every element is printed.
The problem with the "simple" map is due to the fact that I access the "el" element to take in "name". He tries to access but since the folder he is accessing is protected, he returns that error to the console and does not continue with the other elements.
The problem with the "simple" map is due to the fact that I access the "el" element to take in "name". He tries to access but since the folder he is accessing is protected, he returns that error to the console and does not continue with the other elements.
This is the bit which does not add up. There is no folder or directory. The 'data' stgructure is just a list of objects i.e. el is just an object with various properties, one of which is 'name', whihc is just a string. There are no folders or directories associated with the 'data' object and therefore nothing that would generate a 'permission denied' error. To do that, you would need to be doing something like
const remotePath = '/path/to/dir';
const list = await sftp.list(remotePath);
for (el of data) {
if (el.type === 'd') {
const subData = await sftp.list(${remotePath}/${el.name});
// do soemthing with subData
}
}
The above could generate permission errors due to the inner call to sftp.list(). However, your sample code where you just do consol.log(el.name) cannot generate an access permission error as there is no permissions involved. The el.name is just a string.
you still have not stated version of ssh2-sftp-client or node you are using or the platform you are running on. At any rate, the errors you are seeing are due to something else wrong, nothing to do with the loop where you print out the item name.