Skip to content
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

Ensure rename cancellation is synchronous #74948

Merged
merged 19 commits into from
Aug 30, 2024

Conversation

CyrusNajmabadi
Copy link
Member

Alternate approach to #74946.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner August 29, 2024 19:09
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 29, 2024
@@ -44,7 +44,8 @@ internal InlineRenameSessionInfo(IInlineRenameSession session)
internal interface IInlineRenameSession
{
/// <summary>
/// Cancels the rename session, and undoes any edits that had been performed by the session.
/// Cancels the rename session, and undoes any edits that had been performed by the session. Must be called on the
/// UI thread.
Copy link
Member Author

Choose a reason for hiding this comment

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

this UI affinity was already asserted in the impl.

await finalCommitAction().ConfigureAwait(true);
}
if (finalSolution != null)
await ApplyChangesAsync(finalSolution).ConfigureAwait(true);
Copy link
Member Author

Choose a reason for hiding this comment

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

this function is now only async for the 'commit' codepath, for teh 'cancel' codepath, it is entirely synchronous.

@@ -728,6 +737,19 @@ void DismissUIAndRollbackEdits()

RenameService.ActiveSession = null;
}

async Task ApplyChangesAsync(Solution finalSolution)
Copy link
Member Author

Choose a reason for hiding this comment

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

just a move.

{
// ConfigureAwait(true) so we come back to the UI thread to finish work.
await finalCommitAction().ConfigureAwait(true);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

CommitAsync now owns doing this.

RenameTrackingDismisser.DismissRenameTracking(Workspace, Workspace.GetOpenDocumentIds());

// Log how long the full rename took.
_inlineRenameSessionDurationLogBlock.Dispose();
Copy link
Member Author

Choose a reason for hiding this comment

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

this moved into EndRenameSessions_MustBeCalledOnUIThread

@@ -867,84 +881,51 @@ private async Task CommitCoreAsync(IUIThreadOperationContext operationContext, b
}

// The user hasn't canceled by now, so we're done waiting for them. Off to rename!
using var _ = operationContext.AddScope(allowCancellation: false, EditorFeaturesResources.Updating_files);
using var _1 = operationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Updating_files);
Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi Aug 29, 2024

Choose a reason for hiding this comment

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

changed this logic around slightly (to also allow more time for user to cancel if they want). Stages are:

  1. compute tree/text changes to make to the workspace. (still cancellable)
  2. jump to ui thread.
  3. make things non-cancellable.
  4. pre-cleanup (rollback to before linked edits were made)
  5. try apply to workspace.
  6. postcleanup.

RenameLogMessage.UserActionOutcome outcome,
bool previewChanges,
Func<Task> finalCommitAction = null)
Action applyChangesOpt)
Copy link
Member Author

Choose a reason for hiding this comment

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

  1. the applyChanges callback was made synchronous.
  2. the function now required being on the UI therad.

these two things now make this entirely safe to call, without any sort of ugly .Wait from Cancel.

Inside CommitAsync, we do all the actual async work we need, on the BG, prior to calling into this once we've switched back to the UI thread,

DismissUIAndRollbackEditsAndEndRenameSessionAsync(
RenameLogMessage.UserActionOutcome.Canceled, previewChanges: false).Wait();
DismissUIAndRollbackEditsAndEndRenameSession_MustBeCalledOnUIThread(
RenameLogMessage.UserActionOutcome.Canceled, previewChanges: false);
Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi Aug 29, 2024

Choose a reason for hiding this comment

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

no need for Wait anymore. This funciton is no longer async. It no longer attempts to get back to the UI thread (it requires it is already there from callers). And it has no async callback function to execute. So it's fully synchronous and simple.

/// <summary>
/// Dismisses the UI, rolls back any edits, and ends the rename session.
/// </summary>
private void DismissUIAndRollbackEditsAndEndRenameSession_MustBeCalledOnUIThread(
Copy link
Member

Choose a reason for hiding this comment

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

How about call it DismissUIAndRollbackEditsAndEndRenameSessionOnUIThread?

Copy link
Member

@Cosifne Cosifne left a comment

Choose a reason for hiding this comment

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

One name suggestion, other than that LGTM 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants