{"record":{"id":"d49a9f3092cb9301","repo":"stride3d/stride","slug":"unable-to-have-two-sessions-at-the-same-time","errorCode":null,"errorMessage":"Unable to have two sessions at the same time","messagePattern":"Unable to have two sessions at the same time","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/ViewModel/SessionViewModel.cs","lineNumber":557,"sourceCode":"            }\n        }\n\n        private void LoadDocumentation()\n        {\n            // Load documentation into cache\n            var documentationService = ServiceProvider.Get<UserDocumentationService>();\n            foreach (var packageAssembly in LocalPackages.SelectMany(p => p.LoadedAssemblies))\n            {\n                if (packageAssembly.Assembly != null)\n                    Task.Run(() => documentationService.CacheAssemblyDocumentation(packageAssembly.Assembly));\n            }\n        }\n\n        private SessionViewModel(IViewModelServiceProvider serviceProvider, ILogger logger, [NotNull] PackageSession session, [NotNull] EditorViewModel editor)\n            : base(serviceProvider)\n        {\n            if (editor == null) throw new ArgumentNullException(nameof(editor));\n            if (editor.Session != null) throw new InvalidOperationException(\"Unable to have two sessions at the same time\");\n\n            Editor = editor;\n            this.session = session;\n\n            if (Instance != null)\n                throw new InvalidOperationException(\"The SessionViewModel class can be instanced only once.\");\n\n            Instance = this;\n\n            // Initialize the dirtiable manager for all our dirtiable objects\n            undoRedoService = ServiceProvider.Get<IUndoRedoService>();\n\n            // Gather all data from plugins\n            PluginService.RegisterSession(this, logger);\n\n            // Initialize the undo/redo debug view model\n            undoRedoStackPage = EditorDebugTools.CreateUndoRedoDebugPage(undoRedoService, \"Undo/redo stack\");\n            ActionHistory = new ActionHistoryViewModel(this);","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/ViewModel/SessionViewModel.cs#L539-L575","documentation":"The private SessionViewModel constructor validates that the EditorViewModel passed in is not already attached to another session. If editor.Session is non-null, it throws InvalidOperationException because two SessionViewModels cannot share one editor. This protects the editor-to-session one-to-one relationship.","triggerScenarios":"Constructing a SessionViewModel with an EditorViewModel whose Session property is already set, i.e. reusing an editor that already has a session attached.","commonSituations":"Opening a second session/editor window reusing a cached EditorViewModel; reloading a session without detaching the editor from the old SessionViewModel first.","solutions":["Detach the editor from its current session (set editor.Session = null via the proper teardown) before constructing a new SessionViewModel.","Create a fresh EditorViewModel for each new SessionViewModel.","Verify lifecycle order: dispose the old SessionViewModel before creating a new one with the same editor.","Catch InvalidOperationException during session reload and fall back to creating a new editor instance."],"exampleFix":"// before\nvar session = SessionViewModel.CreateInstance(provider, logger, packageSession, existingEditor);\n// after\nexistingEditor.Session = null; // detach from previous session\nvar session = SessionViewModel.CreateInstance(provider, logger, packageSession, existingEditor);","handlingStrategy":"validation","validationCode":"if (editor.Session != null)\n    throw new InvalidOperationException(\"Editor already attached to a session; detach first\");","typeGuard":null,"tryCatchPattern":"try { session = SessionViewModel.CreateInstance(sp, log, ps, editor); }\ncatch (InvalidOperationException) { editor = new EditorViewModel(...); session = SessionViewModel.CreateInstance(sp, log, ps, editor); }","preventionTips":["One editor per session","Detach editor.Session before rebuilding a session","Dispose old SessionViewModel before creating a new one"],"tags":["csharp","viewmodel","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}