8000 Delete stale dircache entry during rename by joelim-work · Pull Request #1138 · gokcehan/lf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Delete stale dircache entry during rename #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2023
Merged

Delete stale dircache entry during rename #1138

merged 3 commits into from
Mar 12, 2023

Conversation

joelim-work
Copy link
Collaborator

Fixes #914

To reproduce the issue:

  1. Create folderA (with some files inside) and folderB (empty)
  2. Delete folderB (or alternatively rename it to something else)
  3. Rename folderA to folderB

Because there is still an entry in the directory cache for folderB for when it previously existed, the renamed directory will be displayed as empty even though it has files inside it. This change deletes newPath from the directory cache before loading it during a rename command, in case such a stale entry exists.

Some other observations I found:

  • nav.checkDir (and by extension the period setting) reloads a directory if its mtime is later than loadTime. But it won't help in this scenario because although folderA is renamed, it isn't actually modified (i.e. its contents haven't changed).
  • This issue doesn't occur if you copy folderA to folderB, or create a new folderB with some files in it. This is because the mtime of the newly created folderB will be new enough to ensure that it will be reloaded via nav.checkDir.

@ilyagr
Copy link
Collaborator
ilyagr commented Mar 8, 2023

Thank you very much for getting to the bottom of this weird bug! This is great!

I can trigger the same bug for the preview cache (for files with text preview instead of dirs), even with this patch. Could you clear that cache as well?

Do we need to clear the cache for the source files in the rename, or only for the target? Update: I suppose this was a silly question, as the source file is gone after the rename.

@joelim-work
Copy link
Collaborator Author
joelim-work commented Mar 8, 2023

I can trigger the same bug for the preview cache (for files with text preview instead of dirs), even with this patch. Could you clear that cache as well?

@ilyagr Thanks for pointing this out, I didn't consider previews for files, only directories. I added a new commit to clear the file cache as well, could you please test this to see if it works for you?

@ilyagr
Copy link
Collaborator
ilyagr commented Mar 9, 2023

Yes, the new commit works as expected. 🎉

Also, while this patch improves things greatly, I found a more complicated way to still see a similar bug:

  1. Start two instances of lf simultaneously. I'll call them A and B.
  2. Scroll through all the files in lf instance A to populate the cache
  3. Do the delete + rename trick in instance B for either a directory or a file with text preview.
  4. Instance A will still show stale previews.

One way to solve that would be to index these caches by the file name & inode number (or some stable id). I'm not sure if that can be done portably, though, or if we can think of an easier solution. One reason to think that another solution might exist is that using external mv instead of the builtin rename command doesn't cause the bug.

Update: Actually, the two-lf-instance version of the bug might show up even if an external mv is used.

In any case, we can use this PR to fix the bug for a single instance of lf first and then think about further fixes.

@joelim-work
Copy link
Collaborator Author

Hi @ilyagr

I managed to reproduce the issue you described above. The reason this happens is although the current instance clears its caches, it only sends a load command to the other instances. The load command doesn't clear any caches, so other instances will end up with a stale preview.

I think using inodes as the cache index makes more sense, since a preview belongs to a file, and not a path (plus whatever file happens to be located at that path). But I am concerned about the following:

  • Finding an equivalent for Windows, and any other issues relating to portability.
  • The fact that this change will blow up in scope as it now means changing the existing design.
  • Introducing new bugs as a result of the above points.

However if we continue to use paths for indexing, stale entries will always be a potential issue because lf cannot keep track of changes made by other instances or external programs. Even without starting another instance of lf, it is possible to create a similar issue:

  1. Create folderA (with some files inside) and folderB (empty)
  2. Select folderB to add an entry to the cache
  3. Start a new terminal, and run rmdir folderB && mv folderA folderB
  4. Go back to lf and reselect folderB (e.g. run load, or navigate to the parent directory and back)

At this point, I'm not really sure what should happen to this pull request. As much as I want to fix this issue, I don't want to contribute a change that is somewhat hacky and doesn't cover every single case.

@ilyagr
Copy link
Collaborator
ilyagr commented Mar 9, 2023

IMO, this is a good fix for a large part of the problem. We can file a separate bug for the more complicated and likely less common problem. I'm not sure it's solvable without some performance sacrifices (at least a new Stat call on the directory entry every time we check the cache)

Update: My last sentence mainly refers to the issue when the action of an external program makes the cache stale. I still haven't reproduced that issue. We could, in principle, resolve the issue for multiple instances of lf more easily, since they can communicate between each other.

@joelim-work
Copy link
Collaborator Author

@ilyagr I agree with your assessment on this issue, thanks for your input! I have also now added a comment in the code to explain this situation better.

@ilyagr
Copy link
Collaborator
ilyagr commented Mar 9, 2023

I meant to ask you to add that comment, but forgot. Thanks for reading my mind!

@gokcehan
Copy link
Owner

@joelim-work Thanks for the patch and @ilyagr thanks for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Very serious - Folders being displayed as empty after being renamed
3 participants
0