Skip to content

DYN-10167: Crash fix. Prevent close on Alt+F4 and hide instead.#16998

Merged
chubakueno merged 2 commits into
DynamoDS:masterfrom
chubakueno:DYN-10167
Apr 3, 2026
Merged

DYN-10167: Crash fix. Prevent close on Alt+F4 and hide instead.#16998
chubakueno merged 2 commits into
DynamoDS:masterfrom
chubakueno:DYN-10167

Conversation

@chubakueno

@chubakueno chubakueno commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Purpose

Several crash reports indicate that Dynamo may crash when trying to Show the node autocomplete window. This happened because we did not consider that the user could close the autocomplete window by focusing it and pressing Alt+F4. since there is no UI to really close the window (we hide it for later reuse instead).

Reproduction steps of the crash fixed by this ticket:

  • Create a node
  • Click the autocomplete sparkle
  • By focusing the autocomplete popup (say, by clicking on it) and pressing Alt+F4, we forcefully close the window, and leave dangling transient nodes.
  • We then try to open the autocomplete window from the port again. This will result in a crash.

This PR fixes both bugs.

Declarations

Check these if you believe they are true

Release Notes

Fix crash when closing node autocomplete window with Alt+F4.

Reviewers

@johnpierson
@BogdanZavu

FYIs

(FILL ME IN, Optional) Names of anyone else you wish to be notified of

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See the ticket for this pull request: https://jira.autodesk.com/browse/DYN-10167

@zeusongit

Copy link
Copy Markdown
Contributor

It is weird that Alt+F4 is caught by the node autocomplete window rather than the Dynamo window itself, which usually what I would expect when pressing it.

@johnpierson johnpierson 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, thanks for catching this

@johnpierson

Copy link
Copy Markdown
Member

It is weird that Alt+F4 is caught by the node autocomplete window rather than the Dynamo window itself, which usually what I would expect when pressing it.

I think it is because our window is focusable for the keyboard shortcut interaction.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in the Node AutoComplete bar window lifecycle by preventing user-initiated window closure (e.g., Alt+F4) from leaving behind a closed/reused window instance and dangling transient nodes.

Changes:

  • Intercept Window.Closing to cancel user-initiated close and hide the window while discarding transient nodes (same behavior as Escape).
  • Add Window.Closed handling to clear the static singleton instance and unsubscribe events to avoid calling Show() on a closed window.
  • Wrap programmatic Close() during reset with an _allowClose guard to bypass the close-cancellation behavior.

{
if (_allowClose)
return;

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

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

OnNodeAutoCompleteBarClosing cancels all close attempts unless _allowClose is set. During application/host shutdown (e.g., owner window closing, dispatcher shutdown), this may prevent the window from closing and potentially keep the process alive. Consider allowing close when shutdown is in progress (e.g., ViewModel?.dynamoViewModel?.Model?.ShutdownRequested == true and/or Dispatcher.HasShutdownStarted / owner is closing) instead of always cancelling.

Suggested change
// During application/host shutdown, allow the window to close normally so that
// we do not keep the process alive by cancelling the close.
var isShuttingDown =
(Dispatcher != null && Dispatcher.HasShutdownStarted) ||
(ViewModel?.dynamoViewModel?.Model?.ShutdownRequested == true);
if (isShuttingDown)
return;

Copilot uses AI. Check for mistakes.
Comment on lines +120 to +139
try
{
UnsubscribeFromAppEvents();
}
catch
{
// ViewModel or model may be invalid during teardown.
}

try
{
if (ViewModel != null)
{
UnsubscribeFromOtherEvents();
}
}
catch
{
// PortViewModel may already be null.
}

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

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

OnNodeAutoCompleteBarClosed has broad catch { } blocks around the unsubscribe logic. This can silently hide teardown problems and make diagnosing event-leak/crash issues harder. Consider catching Exception ex and logging (or at least tracing) the exception message, while still keeping shutdown resilient.

Copilot uses AI. Check for mistakes.
@chubakueno

Copy link
Copy Markdown
Contributor Author

It is weird that Alt+F4 is caught by the node autocomplete window rather than the Dynamo window itself, which usually what I would expect when pressing it.

I think its because its a Window instead of a UserControl, which lets us have nifty things like it protruding away from the dynamo window but at the same time gives it a bit more responsibilities. I didnt know you could close it with Alt+F4 even if it had no buttons until today either.

/// (see <see cref="ResetNodeAutoCompleteBar"/>). Otherwise <see cref="PrepareAndShowNodeAutoCompleteBar"/>
/// would call <see cref="Window.Show"/> on a closed window and crash.
/// </summary>
private void OnNodeAutoCompleteBarClosed(object sender, EventArgs e)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we really need this. When this hits we are already unsubscribed and the control instance will be set to null - we are doing all this in
ResetNodeAutoCompleteBar which is the only place where the real Close() happens programmatically.

So wouldn't allowClose plus handling on Closing be enough ? - that should cover the ALT+F4 and Closed will not be triggered in this scenario that escaped.

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.

Ah yes you're right, since we will only arrive here when programatically closing it's not necessary to have it duplicated, fixed.

DataContext = viewModel;
InitializeComponent();
SubscribeToAppEvents();
Closing += OnNodeAutoCompleteBarClosing;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

need to unsubscribe

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.

Done

@BogdanZavu

Copy link
Copy Markdown
Contributor

Great find and fix! LGTM!

@sonarqubecloud

sonarqubecloud Bot commented Apr 2, 2026

Copy link
Copy Markdown

@chubakueno chubakueno merged commit 45ba46d into DynamoDS:master Apr 3, 2026
28 of 29 checks passed
github-actions Bot pushed a commit that referenced this pull request Apr 3, 2026
@github-actions

github-actions Bot commented Apr 3, 2026

Copy link
Copy Markdown

Successfully created backport PR for RC4.1.0_master:

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.

5 participants