Skip to content

async shell.moveItemToTrash ? #11555

Closed

Description

@YurySolovyov
  • Electron version: 1.7.10
  • Operating system: All

Expected behavior

shell.moveItemToTrash API should have async version

Actual behavior

shell.moveItemToTrash API only has sync version which is bad for Renderer process responsiveness

Activity

  1. YurySolovyov commented on Jan 3, 2018

    @YurySolovyov
    ContributorAuthor

    I think it could be done in a backwards-compatible way via looking at callback function presence.

  2. MarshallOfSound commented on Jan 3, 2018

    @MarshallOfSound
    Member

    @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.

  3. YurySolovyov commented on Jan 3, 2018

    @YurySolovyov
    ContributorAuthor

    @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

  4. MarshallOfSound commented on Jan 3, 2018

    @MarshallOfSound
    Member

    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?

  5. YurySolovyov commented on Jan 3, 2018

    @YurySolovyov
    ContributorAuthor

    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.

  6. YurySolovyov commented on Jan 7, 2018

    @YurySolovyov
    ContributorAuthor

    @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?

  7. YurySolovyov commented on Jan 7, 2018

    @YurySolovyov
    ContributorAuthor

    @MarshallOfSound I did some simple testing with making the call to shell in renderer and using shell from main and calling it via IPC:

    As delete target I made a dir and ran:

    npm i webpack
    

    Then I wrapped calls with console.time/.timeEnd to get for how long the call takes up the thread:

    Direct calls in renderer:

    delete-sync: 493.988037109375ms
    delete-sync: 562.8349609375ms
    delete-sync: 609.74609375ms
    

    Calls to main via IPC:

    delete-async: 0.156982421875ms
    delete-async: 0.13916015625ms
    delete-async: 0.172119140625ms
    

    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?

    Seems like so, BTW.

  8. YurySolovyov commented on Jan 8, 2018

    @YurySolovyov
    ContributorAuthor

    @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:

    1. build electron

      ./script/build.py -c D
    2. 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.

    Branch is here: https://github.com/YurySolovyov/electron/tree/move-to-trash-task-runner

  9. YurySolovyov commented on Feb 13, 2018

    @YurySolovyov
    ContributorAuthor

    @MarshallOfSound I got it working and I can submit a PR if anyone can review it and suggest doing it better somehow.

  10. YurySolovyov commented on Mar 7, 2018

    @YurySolovyov
    ContributorAuthor

    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.

  11. YurySolovyov commented on Mar 8, 2018

    @YurySolovyov
    ContributorAuthor

    @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:

    const { app, shell } = require('electron');
    const fs = require('fs');
    const path = require('path');
    
    var count = 0;
    setInterval(() => {
      console.log(count++);
      if (count === 100) {
        app.quit();
      }
    }, 50);
    
    app.on('ready', function() {
      const filepath = 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 exist
      shell.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();
      });
    });
  12. YurySolovyov commented on Mar 13, 2018

    @YurySolovyov
    ContributorAuthor

    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

    kioclient5 move /home/yury/test/test.md123 trash:/
    

    manually from terminal it just prints

    "The file or folder /home/yury/test/test.md123 does not exist."
    

    and exits with code status 1

  13. MicahZoltu commented on Oct 18, 2019

    @MicahZoltu

    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.

  14. bpasero commented on Oct 18, 2019

    @bpasero
    Contributor

    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.

    @deepak1556 would that be easy to do?

  15. jonigl commented on May 8, 2020

    @jonigl

    any updates on this?

  16. guoyunhe commented on Jul 9, 2020

    @guoyunhe

    Another related "freeze when move to trash" issue microsoft/vscode#101920

  17. nornagon commented on Aug 24, 2020

    @nornagon
    Contributor

    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:

    1. Deprecate and eventually remove the moveItemToTrash API.
    2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions