Skip to content

Retry open file operations when attempting to save source files / source database files - #7823

Merged
kfeinauer merged 15 commits into
masterfrom
bugfix/google-drive-dropbox-save-enhancements
Sep 29, 2020
Merged

kfeinauer merged 15 commits into
masterfrom
bugfix/google-drive-dropbox-save-enhancements

Conversation

@kfeinauer

Copy link
Copy Markdown
Contributor

Intent

When dropbox or other file syncing solutions are present and attempting to sync source files that are active in the editor, save operations on said files can fail due to the inability to open the file (as it is being read in exclusive mode by the backup software).

We should attempt to retry these operations so that they do not fail often.

Approach

This change makes it so that now we retry these operations up to a certain amount of seconds, specified by a new user preference save_retry_timeout, defaulting to 15. This amount of time was chosen for it to be sufficiently long to avoid nearly all exclusive locks held by backup software, while still being short enough to not seem like the operation is stalled.

Instead of doing a retry on all file operations via modifying of the FilePath class, I scoped this down to only target the helper writeStringToFile, and its invocations in the source database. This is to reduce impact and risk of inadvertently slowing down some other processes in other parts of the code base that might not expect it.

Closes #7131

QA Notes

See repro steps in #7131. The user pref save_retry_timeout can be lowered to increase the likelihood of reproducing the original issue.

…rce database files.

When dropbox or other file syncing solutions are present and attempting to sync source files that are active in the editor, save operations on said files can fail due to the inability to open the file (as it is being read in exclusive mode by the backup software).

This change makes it so that now we retry these operations up to a certain amount of seconds, specified by a new user preference save_retry_timeout.
@kfeinauer kfeinauer added this to the v1.4 milestone Sep 17, 2020
Comment thread src/cpp/core/FileSerializer.cpp Resolved
break;

// wait a moment before retrying
boost::this_thread::sleep(milliseconds(500));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually pause the whole RPC thread? I'm a little concerned that if (a) we auto backup changes every 500ms (the default), and (b) every (other) attempt to write backups pauses the RPC thread for 500+ms as we wait for GDrive (etc) to release the lock, the RPC thread is going to spend a pathologically large time locked.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does since it is sleeping. I did my testing with auto save on 500 ms and the performance seemed good. The vast majority of saves are non conflicting - I'd say one in every 15-20 saves produced a stall waiting for the file to become available in my testing. I didn't notice any ill effects from a UX perspective.

This also reduces a bunch of complexity as opposed to retrying the operations in the front end and having to re-queue operations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was your testing on a system with a fast SSD and internet connection? I think that GDrive and friends can have an exclusive lock for much longer on some environments, leading to more save conflicts.

I think we need to give up faster on autosaves. The user generally has another chance to save the file (and will get warned if they try to exit with changes that have never been backed up); it feels wrong that there's a possibility of blocking the entire RPC thread for 15 seconds for an operation the user didn't initiate.

15s isn't unreasonable for a change that must succeed, so I'd suggest a second pref for autosave (auto_save_retry_timeout?) as both a shorter fuse and an escape hatch; it could give up after a second or two, and setting it to 0 would result in a best-effort autosave that just attempts to write the file if it can (the front end could read this value and avoid popping up "file is in use" errors).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about not retrying for autosaves period? The whole point of autosaves is that it saves continuously and periodically. Since most operations are successful, we could simply ignore this particular failure for autosaves entirely, with the knowledge that future autosaves will almost assuredly work (since the exclusive file lock will be given up as soon as syncing is done).

We can keep everything as is currently for manual saves, since they are a user initiated action.

@kevinushey

Copy link
Copy Markdown
Collaborator

One worry I have: what if another application locks something in the source database, and for whatever reason refuses to release that lock? It feels like in that case the user could get stuck -- e.g. what if they start mashing the save button (queueing up new save RPC requests) and each of those takes 15 seconds to eventually fail to save and otherwise locks up the IDE for that duration.

@kfeinauer

Copy link
Copy Markdown
Contributor Author

@kevinushey I think what we have is reasonable for manual saves because it's a user initiated action, and it's certainly not a common occurrence for these files to be permanently locked by some other application.

I do agree that we should only allow one pending save operation in flight at any given time though. Is this something we are already preventing?

… action. Do not retry writes for autosaves / lints. If not retrying the operation (because of autosave semantics), supress errors indicating that another process is using the file (as it is a transient issue).
…ccessfully saved by the source database file properties were not. This would cause annoying prompts asking the user to reload the file as it hasd changed on disk, even though it really had not.
@kfeinauer

Copy link
Copy Markdown
Contributor Author

@jmcphers This is ready for another review.

These changes continue with the philosophy that autosave transient failures should not produce a user dismissible error - these errors should only show for user initiated actions.

Made the following changes:

  1. Only attempt a retry operation if it was the result of a manual user operation - autosaves will now no longer stall the pipeline by continuously retrying
  2. Hide the autosave failure if it is due to transient file lock
  3. Fix issue where lastKnownWriteTime could get out of sync if the file data is written but its source database metadata is not - this was directly observable in the original bug report
  4. Restart the autosave operation if it failed silently

@kfeinauer
kfeinauer requested a review from jmcphers September 21, 2020 22:02
std::map<std::string, std::string> s_idToPath;

// cached mapping of document last write times
std::map<std::string, std::time_t> s_lastWriteTimes;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a threadsafe map?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - all of these routines execute only on the main thread (thread 0)

Comment thread src/cpp/core/FileSerializer.cpp Resolved
@kfeinauer

Copy link
Copy Markdown
Contributor Author

@jmcphers @kevinushey Updated

message);
// do not show the error if it is a transient autosave related issue - this can occur fairly frequently
// when attempting to save files that are being backed up by external software
if (message.contains("The process cannot access the file because it is being used by another process") && suppressFileLockError_)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking this based on the message rather than an error property or code seems fragile. Would it be feasible for us to send a proper error object (e.g. with an error code) that we could check here or something similar?

@kfeinauer kfeinauer Sep 28, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided against that because we are using the ProgressIndicator interface here which is tied to error messages and is a very fundamental interface in the code. I didn't think extending this interface was worth it to propagate this one error code here as this would affect a sizable portion of the code base.

…ditors/text/TextEditingTarget.java

Co-authored-by: Kevin Ushey <[email protected]>

@jmcphers jmcphers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's definitely add a NEWS entry for this one!

@kfeinauer
kfeinauer merged commit 7850cfc into master Sep 29, 2020
@valerie-rstudio
valerie-rstudio deleted the bugfix/google-drive-dropbox-save-enhancements branch January 21, 2022 17:36
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.

Constant errors when editing a file synced to Google Drive

3 participants