roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Fix NRE in rename

Open Cosifne opened this issue 5 months ago • 1 comments

NRE might happen when typing using rename.

image (Note: NRE is going to happen on line 83, I use the debugger so it's easier to get repro)

I have experienced this most in cases when I open a solution first, use rename. Then open a second solution, use rename again and VS would crash in this case.

Look into the call stack: image It shows that the identifier typed in UI propagates to InlineRenameUndoManager but currentState is null at that moment.

The sequence is: ActiveSession change -> InlineRenameService_ActiveSessionChanged -> Register & Unregister InlineRenameSession_ReplacementTextChanged event handler (Where the NRE happens)

Moreover, the active InlineRenameSevice is referencing a different UndoManager (comparing to this) image

So the problem is: UndoManager is registered in a workspace factory

Later InlineRenameService_ActiveSessionChanged hooks to ActiveSession changed event in ctor and it never gets unplug.

So in this case, when another solution is open, sometimes another UndoManager is created and two managers are hooked to the ActiveSession changed event. But one of them is clean after Disconnect is called so NRE is thrown.

Solution: Register InlineRenameService_ActiveSessionChanged in CreateInitialState() method. CreateInitialState() is only called when RenameSession get created Then unregister when UndoManager gets disconnected. It will be called when DismissUIAndRollbackEditsAndEndRenameSessionAsync is called (only happen when commit & cancel)

Cosifne avatar Aug 27 '24 04:08 Cosifne