Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 6a41d52

Browse files
shanaStanleyGoldman
authored andcommitted
Ensure environment is never initialized off the main thread (#789)
There's a potential for Environment to be initialized off the main thread in some cases (not sure exactly how, but #783 has an exception about it so it can definitely happen), so make sure everything depends on it being instantiated first. Also clean up some confusing properties.
1 parent b0a6525 commit 6a41d52

File tree

9 files changed

+79
-79
lines changed

9 files changed

+79
-79
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ public event Action<IProgress> OnProgress
2626
remove { progressReporter.OnProgress -= value; }
2727
}
2828

29-
public ApplicationManagerBase(SynchronizationContext synchronizationContext)
29+
public ApplicationManagerBase(SynchronizationContext synchronizationContext, IEnvironment environment)
3030
{
3131
SynchronizationContext = synchronizationContext;
3232
SynchronizationContext.SetSynchronizationContext(SynchronizationContext);
3333
ThreadingHelper.SetUIThread();
3434
UIScheduler = TaskScheduler.FromCurrentSynchronizationContext();
3535
ThreadingHelper.MainThreadScheduler = UIScheduler;
36+
37+
Environment = environment;
3638
TaskManager = new TaskManager(UIScheduler);
37-
progress.OnProgress += progressReporter.UpdateProgress;
39+
Platform = new Platform(Environment);
40+
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, TaskManager.Token);
41+
GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
3842
}
3943

4044
protected void Initialize()
4145
{
42-
// accessing Environment triggers environment initialization if it hasn't happened yet
43-
Platform = new Platform(Environment);
44-
4546
LogHelper.TracingEnabled = UserSettings.Get(Constants.TraceLoggingKey, false);
4647
ApplicationConfiguration.WebTimeout = UserSettings.Get(Constants.WebTimeoutKey, ApplicationConfiguration.WebTimeout);
47-
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
4848
Platform.Initialize(ProcessManager, TaskManager);
49-
GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
49+
progress.OnProgress += progressReporter.UpdateProgress;
5050
}
5151

5252
public void Run()
@@ -63,7 +63,7 @@ public void Run()
6363

6464
if (Environment.IsMac)
6565
{
66-
var getEnvPath = new SimpleProcessTask(CancellationToken, "bash".ToNPath(), "-c \"/usr/libexec/path_helper\"")
66+
var getEnvPath = new SimpleProcessTask(TaskManager.Token, "bash".ToNPath(), "-c \"/usr/libexec/path_helper\"")
6767
.Configure(ProcessManager, dontSetupGit: true)
6868
.Catch(e => true); // make sure this doesn't throw if the task fails
6969
var path = getEnvPath.RunWithReturn(true);
@@ -96,7 +96,7 @@ public void Run()
9696
}
9797

9898

99-
var installer = new GitInstaller(Environment, ProcessManager, CancellationToken);
99+
var installer = new GitInstaller(Environment, ProcessManager, TaskManager.Token);
100100
installer.Progress.OnProgress += progressReporter.UpdateProgress;
101101
if (state.GitIsValid && state.GitLfsIsValid)
102102
{
@@ -132,7 +132,7 @@ public void Run()
132132
progress.UpdateProgress(90, 100, "Initialization failed");
133133
}
134134

135-
new ActionTask<bool>(CancellationToken, (s, gitIsValid) =>
135+
new ActionTask<bool>(TaskManager.Token, (s, gitIsValid) =>
136136
{
137137
InitializationComplete();
138138
if (gitIsValid)
@@ -358,12 +358,10 @@ public void Dispose()
358358
Dispose(true);
359359
}
360360

361-
public abstract IEnvironment Environment { get; }
362-
361+
public IEnvironment Environment { get; private set; }
363362
public IPlatform Platform { get; protected set; }
364363
public virtual IProcessEnvironment GitEnvironment { get; set; }
365364
public IProcessManager ProcessManager { get; protected set; }
366-
public CancellationToken CancellationToken { get { return TaskManager.Token; } }
367365
public ITaskManager TaskManager { get; protected set; }
368366
public IGitClient GitClient { get; protected set; }
369367
public ISettings LocalSettings { get { return Environment.LocalSettings; } }

src/GitHub.Api/Application/IApplicationManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace GitHub.Unity
66
{
77
public interface IApplicationManager : IDisposable
88
{
9-
CancellationToken CancellationToken { get; }
109
IEnvironment Environment { get; }
1110
IPlatform Platform { get; }
1211
IProcessEnvironment GitEnvironment { get; }

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class ApplicationManager : ApplicationManagerBase
1414

1515
private FieldInfo quitActionField;
1616

17-
public ApplicationManager(IMainThreadSynchronizationContext synchronizationContext)
18-
: base(synchronizationContext as SynchronizationContext)
17+
public ApplicationManager(IMainThreadSynchronizationContext synchronizationContext,
18+
IEnvironment environment)
19+
: base(synchronizationContext as SynchronizationContext, environment)
1920
{
2021
FirstRun = ApplicationCache.Instance.FirstRun;
2122
InstanceId = ApplicationCache.Instance.InstanceId;
@@ -31,7 +32,7 @@ protected override void InitializeUI()
3132

3233
isBusy = false;
3334
LfsLocksModificationProcessor.Initialize(Environment, Platform);
34-
ProjectWindowInterface.Initialize(Environment.Repository);
35+
ProjectWindowInterface.Initialize(this);
3536
var window = Window.GetWindow();
3637
if (window != null)
3738
window.InitializeWindow(this);
@@ -100,6 +101,5 @@ protected override void Dispose(bool disposing)
100101
}
101102

102103
public override IProcessEnvironment GitEnvironment { get { return Platform.GitEnvironment; } }
103-
public override IEnvironment Environment { get { return EnvironmentCache.Instance.Environment; } }
104104
}
105105
}

src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static void Initialize()
3333
EditorApplication.update -= Initialize;
3434

3535
// this will initialize ApplicationManager and Environment if they haven't yet
36-
var logPath = Environment.LogPath;
36+
var logPath = ApplicationManager.Environment.LogPath;
3737

3838
if (ApplicationCache.Instance.FirstRun)
3939
{
@@ -58,20 +58,20 @@ private static void Initialize()
5858
LogHelper.Error(ex, "Error rotating log files");
5959
}
6060

61-
Debug.LogFormat("Initialized GitHub for Unity version {0}{1}Log file: {2}", ApplicationInfo.Version, Environment.NewLine, logPath);
61+
Debug.LogFormat("Initialized GitHub for Unity version {0}{1}Log file: {2}", ApplicationInfo.Version, ApplicationManager.Environment.NewLine, logPath);
6262
}
6363

6464
LogHelper.LogAdapter = new MultipleLogAdapter(new FileLogAdapter(logPath)
6565
#if DEBUG
6666
, new UnityLogAdapter()
6767
#endif
6868
);
69-
LogHelper.Info("Initializing GitHubForUnity:'v{0}' Unity:'v{1}'", ApplicationInfo.Version, Environment.UnityVersion);
69+
LogHelper.Info("Initializing GitHubForUnity:'v{0}' Unity:'v{1}'", ApplicationInfo.Version, ApplicationManager.Environment.UnityVersion);
7070

7171
ApplicationManager.Run();
7272

7373
if (ApplicationCache.Instance.FirstRun)
74-
UpdateCheckWindow.CheckForUpdates();
74+
UpdateCheckWindow.CheckForUpdates(ApplicationManager);
7575
}
7676

7777
internal static void Restart()
@@ -92,14 +92,10 @@ public static IApplicationManager ApplicationManager
9292
{
9393
if (appManager == null)
9494
{
95-
appManager = new ApplicationManager(new MainThreadSynchronizationContext());
95+
appManager = new ApplicationManager(new MainThreadSynchronizationContext(), EnvironmentCache.Instance.Environment);
9696
}
9797
return appManager;
9898
}
9999
}
100-
101-
public static IEnvironment Environment { get { return ApplicationManager.Environment; } }
102-
103-
public static IUsageTracker UsageTracker { get { return ApplicationManager.UsageTracker; } }
104100
}
105101
}

src/UnityExtension/Assets/Editor/GitHub.Unity/ScriptObjectSingleton.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public LocationAttribute(string relativePath, Location location)
2121
if (location == Location.PreferencesFolder)
2222
filepath = InternalEditorUtility.unityPreferencesFolder + "/" + relativePath;
2323
else if (location == Location.UserFolder)
24-
filepath = EntryPoint.Environment.UserCachePath.Combine(relativePath).ToString(SlashMode.Forward);
24+
filepath = EntryPoint.ApplicationManager.Environment.UserCachePath.Combine(relativePath).ToString(SlashMode.Forward);
2525
else if (location == Location.LibraryFolder)
26-
filepath = EntryPoint.Environment.UnityProjectPath.Combine("Library", "gfu", relativePath);
26+
filepath = EntryPoint.ApplicationManager.Environment.UnityProjectPath.Combine("Library", "gfu", relativePath);
2727
}
2828
}
2929

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/GitPathView.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ public override void OnGUI()
173173
{
174174
GUI.FocusControl(null);
175175
isBusy = true;
176-
new FuncTask<GitInstaller.GitInstallationState>(Manager.CancellationToken, () =>
176+
new FuncTask<GitInstaller.GitInstallationState>(TaskManager.Token, () =>
177177
{
178-
var gitInstaller = new GitInstaller(Environment, Manager.ProcessManager, Manager.CancellationToken);
178+
var gitInstaller = new GitInstaller(Environment, Manager.ProcessManager, TaskManager.Token);
179179
return gitInstaller.FindSystemGit(new GitInstaller.GitInstallationState());
180180
})
181181
{ Message = "Locating git..." }
@@ -226,7 +226,7 @@ private void ValidateAndSetGitInstallPath()
226226
{
227227
new FuncTask<GitInstaller.GitInstallationState>(TaskManager.Token, () =>
228228
{
229-
var gitInstaller = new GitInstaller(Environment, Manager.ProcessManager, Manager.CancellationToken);
229+
var gitInstaller = new GitInstaller(Environment, Manager.ProcessManager, TaskManager.Token);
230230
var state = new GitInstaller.GitInstallationState();
231231
state = gitInstaller.SetDefaultPaths(state);
232232
// on non-windows we only bundle git-lfs

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/LocksView.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class LocksControl
5757
[SerializeField] public GitLockEntryDictionary assets = new GitLockEntryDictionary();
5858
[SerializeField] public GitStatusDictionary gitStatusDictionary = new GitStatusDictionary();
5959
[SerializeField] private GitLockEntry selectedEntry;
60+
[SerializeField] public NPath projectPath;
61+
6062
public bool IsEmpty { get { return gitLockEntries.Count == 0; } }
6163

6264
public GitLockEntry SelectedEntry
@@ -69,8 +71,8 @@ public GitLockEntry SelectedEntry
6971
{
7072
selectedEntry = value;
7173

72-
var activeObject = selectedEntry != null && selectedEntry.GitLock != GitLock.Default
73-
? AssetDatabase.LoadMainAssetAtPath(selectedEntry.GitLock.Path.MakeAbsolute().RelativeTo(EntryPoint.Environment.UnityProjectPath))
74+
var activeObject = selectedEntry != null && selectedEntry.GitLock != GitLock.Default && projectPath.IsInitialized
75+
? AssetDatabase.LoadMainAssetAtPath(selectedEntry.GitLock.Path.MakeAbsolute().RelativeTo(projectPath))
7476
: null;
7577

7678
lastActivatedObject = activeObject;
@@ -235,7 +237,7 @@ public void Load(List<GitLock> locks, List<GitStatusEntry> gitStatusEntries)
235237

236238
var gitLockEntry = new GitLockEntry(gitLock, gitFileStatus);
237239
LoadIcon(gitLockEntry, true);
238-
var path = gitLock.Path.MakeAbsolute().RelativeTo(EntryPoint.Environment.UnityProjectPath);
240+
var path = gitLock.Path.MakeAbsolute().RelativeTo(projectPath);
239241
var assetGuid = AssetDatabase.AssetPathToGUID(path);
240242
if (!string.IsNullOrEmpty(assetGuid))
241243
{
@@ -611,6 +613,7 @@ private void BuildLocksControl()
611613
locksControl = new LocksControl();
612614
}
613615

616+
locksControl.projectPath = Environment.UnityProjectPath;
614617
locksControl.Load(lockedFiles, gitStatusEntries);
615618
}
616619
public override void OnSelectionChange()

0 commit comments

Comments
 (0)