Skip to content

Commit

Permalink
fix: freezes encountered by some users (#508)
Browse files Browse the repository at this point in the history
Previously, `TaskUtil.OnMainThread` was asynchronous, allowing for the
editor to remain responsive even if something was spamming
invalidations. However, with the prior change fdaee91, this was
accidentally changed to be synchronous. This seems to cause editor
freezes in some situations - particularly, if OnMainThread was invoked
early in editor initialization, before we know which thread is the main
thread.

This change reverts this logic to be asynchronous.

NOTE: This also reverts the accidental addition of a public API in 1.6.3

Closes: #507
  • Loading branch information
bdunderscore authored Jan 18, 2025
1 parent 55e043b commit 64b5c43
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed
- [#507] Fixed a potential editor freeze bug

### Changed

### Removed
- [#507] Removed the `NDMFSyncContext.RunOnMainThread` API which was accidentally addded in 1.6.3.
This might be re-added in 1.7.0.

### Security

Expand Down
2 changes: 1 addition & 1 deletion Editor/PreviewSystem/ComputeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public ComputeContext(string description)
arg0: this
).EventId;

TaskUtil.OnMainThread(this, DoInvalidate);
NDMFSyncContext.RunOnMainThread(target => DoInvalidate((ComputeContext)target), this);
};
Description = description;

Expand Down
16 changes: 10 additions & 6 deletions Editor/PreviewSystem/Task/NDMFSyncContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;

Expand All @@ -15,20 +16,23 @@ public static class NDMFSyncContext
{
public static SynchronizationContext Context = new Impl();
private static Impl InternalContext = (Impl)Context;

/// <summary>
/// Runs the given function on the unity main thread - synchronously if possible.
/// Runs the given function on the unity main thread, passing the given target as an argument.
/// The function will be invoked synchronously if called from the main thread; otherwise, it will be
/// run asynchronously.
/// </summary>
/// <param name="cb"></param>
public static void RunOnMainThread(EditorApplication.CallbackFunction cb)
/// <param name="receiver"></param>
/// <param name="target"></param>
internal static void RunOnMainThread(SendOrPostCallback receiver, object target)
{
if (Thread.CurrentThread.ManagedThreadId == InternalContext.unityMainThreadId)
{
cb();
receiver(target);
}
else
{
Context.Send(_ => cb(), null);
Context.Post(receiver, target);
}
}

Expand Down
5 changes: 0 additions & 5 deletions Editor/PreviewSystem/TaskUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,5 @@ static void Init()
{
_mainThread = Thread.CurrentThread;
}

internal static void OnMainThread<T>(T target, Action<T> receiver)
{
NDMFSyncContext.RunOnMainThread(() => receiver(target));
}
}
}

0 comments on commit 64b5c43

Please sign in to comment.