You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@YurySolovyov We can't always make a synchronous API asynchronous. In this case, the platform specific API's are synchronous on all platforms; if trashing the item is slowing your process down I'd recommend offloading it to a new process.
@MarshallOfSound thanks, I think I'll be fine by doing it in main process instead of renderer.
But out of curiosity, shouldn't chrome's scheduler have a way to spawn a thread for such things?
It seems like a valid approach to make most sync-designed APIs async-friendly
I guess we could post it to another thread to handle, not sure if it's worth the complexity or not. How much of a slowdown do you see in the renderer process calling this API and on what type of file?
TBH, I didn't do any measurements yet, but I'd expect them to be significant because I'm going to allow users to delete arbitrary folders they'll select. In case I'll do it in renderer, that will block thread for amount of time proportional to the amount of files being deleted, but at least if I do it in main I'll free my UI thread and keep app responsive regardless of amount of files.
@MarshallOfSound side question, docs do not specify if path should be a file path or dir or if it matters at all - am I getting it right that both file paths and dir paths are supported?
Obviously deletion itself takes pretty much the same amount of time, difference is that IPC version returns control faster, but...
...weird thing I noticed is that even tho -async version seems faster, UI still gets frozen, not sure why, maybe blocking call in main blocks IPC or something, but I used async IPC variant, so at least that's not something obvious.
am I getting it right that both file paths and dir paths are supported?
@MarshallOfSound I was trying to get it working, but I have troubles running the actual callback.
When I'm trying to invoke it, nothing happens, I tried several ways and I either can't make type checker pass or nothing happens. My guess is that I'm doing memory management wrong.
There is a _test dir I used to test it and it works like this:
build electron
./script/build.py -c D
run test script
out/D/electron _test/
In the test, I'm using an interval to make sure event loop is not blocked, working run will print "Done: true" in the end and close the app.
I updated my branch with (kinda) working Promise-based implementation.
"Kinda" because it works for success (.then) case, and hangs on fail (.catch) case.
I'd be glad if someone can take a look, but I'm also going to investigate that.
const{ app, shell }=require('electron');constfs=require('fs');constpath=require('path');varcount=0;setInterval(()=>{console.log(count++);if(count===100){app.quit();}},50);app.on('ready',function(){constfilepath=path.join(__dirname,'test.md');fs.writeFileSync(filepath,'# Hello');setTimeout(()=>app.quit(),4000);// this thing is only called if/when I hit Ctrl+C// + '123' to emulate an attempt to delete file that does not existshell.moveItemToTrash(filepath+'123').then(function(){// this works perfectly fine, and removes the file// shell.moveItemToTrash(filepath).then(function() {console.log('Done: success');app.quit();}).catch(function(error){console.log('Error: ',error);app.quit();});});
On Windows, deleting a large (200 MB) node_modules folder takes about 15 seconds on a reasonably powerful machine with an SSD. The issue is that Windows is fairly slow at moving folders with many files to the trash (it iterates over them all individually) and node_modules folders are notoriously file count heavy.
VSCode uses this for its delete functionality, and currently it runs in the renderer thread which means the entire UI locks up for up to minutes depending on the size of node_modules. While VSCode can move this call to a background thread, this is one of those calls that feels like it should never be run in the renderer thread because it is sync at has unbounded time cost (depending on parameters passed in) and there are normal situations where it can take seconds to minutes to complete.
While VSCode can move this call to a background thread
To clarify: I do not think VSCode can or should do that. We could move it to the main process or even our shared process, but that one would lock up for a long time as well. Electron must fix this on their end by possibly moving this call onto a thread.
I agree we should fix this! Unfortunately fixing it will be a breaking change, as the API currently returns a boolean indicating success/failure, so it can't be changed to return a promise.
I propose:
Deprecate and eventually remove the moveItemToTrash API.
Create an improved, asynchronous replacement API, trashItem:
### `shell.trashItem(path[, options])`*`path` String - path to the item to be moved to the trash.
*`options` Object (optional)
*`deleteOnFail` Boolean (optional) - Whether or not to unilaterally remove
the item if the Trash is disabled or unsupported on the volume. _macOS_
Returns `Promise<void>` - Resolves when the operation has been completed.
Rejects if there was an error while deleting the requested item.
This moves a path to the OS-specific trash location (Trash on macOS, Recycle
Bin on Windows, and a desktop-environment-specific location on Linux).
[EDIT]: It might make sense to remove the deleteOnFail option entirely, as it's Mac-specific and the app can implement that behavior themselves relatively easily.
Reacted by Benjamin Pasero, Fabio Spampinato and Saul Fautley
Activity
YurySolovyov commentedon Jan 3, 2018
I think it could be done in a backwards-compatible way via looking at callback function presence.
MarshallOfSound commentedon Jan 3, 2018
@YurySolovyov We can't always make a synchronous API asynchronous. In this case, the platform specific API's are synchronous on all platforms; if trashing the item is slowing your process down I'd recommend offloading it to a new process.
YurySolovyov commentedon Jan 3, 2018
@MarshallOfSound thanks, I think I'll be fine by doing it in main process instead of renderer.
But out of curiosity, shouldn't chrome's scheduler have a way to spawn a thread for such things?
It seems like a valid approach to make most sync-designed APIs async-friendly
MarshallOfSound commentedon Jan 3, 2018
I guess we could post it to another thread to handle, not sure if it's worth the complexity or not. How much of a slowdown do you see in the renderer process calling this API and on what type of file?
YurySolovyov commentedon Jan 3, 2018
TBH, I didn't do any measurements yet, but I'd expect them to be significant because I'm going to allow users to delete arbitrary folders they'll select. In case I'll do it in renderer, that will block thread for amount of time proportional to the amount of files being deleted, but at least if I do it in main I'll free my UI thread and keep app responsive regardless of amount of files.
I'll try to measure the impact this week.
YurySolovyov commentedon Jan 3, 2018
Note for myself: https://chromium.googlesource.com/chromium/src/+/lkcr/docs/threading_and_tasks.md
YurySolovyov commentedon Jan 7, 2018
@MarshallOfSound side question, docs do not specify if path should be a file path or dir or if it matters at all - am I getting it right that both file paths and dir paths are supported?
YurySolovyov commentedon Jan 7, 2018
@MarshallOfSound I did some simple testing with making the call to
shellin renderer and usingshellfrom main and calling it via IPC:As delete target I made a dir and ran:
Then I wrapped calls with
console.time/.timeEndto get for how long the call takes up the thread:Direct calls in renderer:
Calls to main via IPC:
Obviously deletion itself takes pretty much the same amount of time, difference is that IPC version returns control faster, but...
...weird thing I noticed is that even tho
-asyncversion seems faster, UI still gets frozen, not sure why, maybe blocking call in main blocks IPC or something, but I used async IPC variant, so at least that's not something obvious.Seems like so, BTW.
YurySolovyov commentedon Jan 8, 2018
@MarshallOfSound I was trying to get it working, but I have troubles running the actual callback.
When I'm trying to invoke it, nothing happens, I tried several ways and I either can't make type checker pass or nothing happens. My guess is that I'm doing memory management wrong.
There is a
_testdir I used to test it and it works like this:build electron
run test script
In the test, I'm using an interval to make sure event loop is not blocked, working run will print "Done: true" in the end and close the app.
Branch is here: https://github.com/YurySolovyov/electron/tree/move-to-trash-task-runner
YurySolovyov commentedon Feb 13, 2018
@MarshallOfSound I got it working and I can submit a PR if anyone can review it and suggest doing it better somehow.
YurySolovyov commentedon Mar 7, 2018
I updated my branch with (kinda) working Promise-based implementation.
"Kinda" because it works for success (
.then) case, and hangs on fail (.catch) case.I'd be glad if someone can take a look, but I'm also going to investigate that.
YurySolovyov commentedon Mar 8, 2018
@MarshallOfSound or maybe @deepak1556
These branches look mostly symmetrical to me, I have no idea why rejection does not work.
I'm using test code like this:
YurySolovyov commentedon Mar 13, 2018
Found the issue: basically rejection does not work cause it hangs on this line.
I'm not sure what causes it to hang, cause if I run
manually from terminal it just prints
and exits with code status 1
MicahZoltu commentedon Oct 18, 2019
On Windows, deleting a large (200 MB)
node_modulesfolder takes about 15 seconds on a reasonably powerful machine with an SSD. The issue is that Windows is fairly slow at moving folders with many files to the trash (it iterates over them all individually) andnode_modulesfolders are notoriously file count heavy.VSCode uses this for its delete functionality, and currently it runs in the renderer thread which means the entire UI locks up for up to minutes depending on the size of
node_modules. While VSCode can move this call to a background thread, this is one of those calls that feels like it should never be run in the renderer thread because it is sync at has unbounded time cost (depending on parameters passed in) and there are normal situations where it can take seconds to minutes to complete.bpasero commentedon Oct 18, 2019
To clarify: I do not think VSCode can or should do that. We could move it to the main process or even our shared process, but that one would lock up for a long time as well. Electron must fix this on their end by possibly moving this call onto a thread.
@deepak1556 would that be easy to do?
jonigl commentedon May 8, 2020
any updates on this?
guoyunhe commentedon Jul 9, 2020
Another related "freeze when move to trash" issue microsoft/vscode#101920
nornagon commentedon Aug 24, 2020
I agree we should fix this! Unfortunately fixing it will be a breaking change, as the API currently returns a boolean indicating success/failure, so it can't be changed to return a promise.
I propose:
moveItemToTrashAPI.trashItem:[EDIT]: It might make sense to remove the
deleteOnFailoption entirely, as it's Mac-specific and the app can implement that behavior themselves relatively easily.