{"record":{"id":"d0152a3b4849b78f","repo":"Unity-Technologies/UnityCsReference","slug":"nameof-unitysourcefileupdatersresulthandler-can","errorCode":null,"errorMessage":"{nameof(UnitySourceFileUpdatersResultHandler)} can only be created on the main thread","messagePattern":"(.+?) can only be created on the main thread","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Scripting/ScriptCompilation/BeeDriver/UnitySourceFileUpdatersResultHandler.cs","lineNumber":32,"sourceCode":"using UnityEditorInternal.APIUpdating;\nusing UnityEngine;\nusing System.Threading.Tasks;\nusing UnityEditorInternal;\n\nnamespace UnityEditor.Scripting.ScriptCompilation\n{\n    class UnitySourceFileUpdatersResultHandler : SourceFileUpdatersResultHandler\n    {\n        bool m_HaveConsentToOverwriteUserScripts;\n\n        readonly UnityScriptUpdaterConsentAPI ConstentAPI;\n\n        public UnitySourceFileUpdatersResultHandler() : base(captureSynchronizationContext: true)\n        {\n            ConstentAPI = new UnityScriptUpdaterConsentAPI();\n\n            if (!InternalEditorUtility.CurrentThreadIsMainThread())\n                throw new Exception($\"{nameof(UnitySourceFileUpdatersResultHandler)} can only be created on the main thread\");\n        }\n\n\n        protected override bool ProcessUpdaterResults(SourceFileUpdaterBase.Update[] updates)\n        {\n            var problemUpdates = new List<(SourceFileUpdaterBase.Update update, Exception exception)>();\n            bool didUpdate = false;\n            void ExecuteUpdates(IEnumerable<SourceFileUpdaterBase.Update> updates)\n            {\n                foreach (var update in updates)\n                {\n                    didUpdate = true;\n                    try\n                    {\n                        Console.WriteLine(update.originalFileWithError);\n                        new NPath(update.tempFileWithNewContents).Copy(update.originalFileWithError);\n                    }\n                    catch (Exception e)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Scripting/ScriptCompilation/BeeDriver/UnitySourceFileUpdatersResultHandler.cs#L14-L50","documentation":"Thrown from the UnitySourceFileUpdatersResultHandler constructor: it must be created on the main thread because it captures the synchronization context and interacts with consent UI. Creating it on a worker thread is rejected via InternalEditorUtility.CurrentThreadIsMainThread().","triggerScenarios":"Instantiating UnitySourceFileUpdatersResultHandler on a non-main thread (e.g. inside a Task or thread pool callback).","commonSituations":"Driving the script updater from a background task/thread, or constructing the handler inside an async continuation that did not marshal back to the main thread.","solutions":["Construct the handler on the main thread (e.g. queue the creation to the main thread / EditorApplication update).","Marshal background continuations back to the main thread before creating main-thread-only objects."],"exampleFix":"// before\nTask.Run(() => { var h = new UnitySourceFileUpdatersResultHandler(); ... });\n// after\nEditorApplication.delayCall += () => { var h = new UnitySourceFileUpdatersResultHandler(); ... };","handlingStrategy":"validation","validationCode":"if (!InternalEditorUtility.CurrentThreadIsMainThread())\n    throw new InvalidOperationException(\"Construct this handler on the main thread.\");","typeGuard":"static bool IsMainThread() => InternalEditorUtility.CurrentThreadIsMainThread();","tryCatchPattern":"try { var h = new UnitySourceFileUpdatersResultHandler(); }\ncatch (Exception ex) when (ex.Message.Contains(\"main thread\"))\n{ /* re-run the construction via EditorApplication.delayCall / main thread dispatch */ }","preventionTips":["Construct main-thread-only objects from EditorApplication.delayCall or the update loop.","Marshal Task continuations back to the main thread before touching editor APIs."],"tags":["script-updater","threading","main-thread","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}