stride3d/stride · error · InvalidOperationException

Cannot attach a project to more than one session

Error message

Cannot attach a project to more than one session

What it means

Thrown by PackageSession.RegisterProject when a PackageContainer project already belongs to another session (its Session property is not null). A project package can only be attached to one session at a time because the session manages its registration and save/load flow.

Solutions

  1. Ensure the project's previous session is disposed/released before attaching it to a new one.
  2. Load a fresh PackageContainer instance for the new session instead of reusing one.
  3. Check project.Session before attaching: only attach when it is null or already equals the target session.

Example fix

// before
var sessionB = new PackageSession();
sessionB.Load(existingProject); // existingProject.Session == sessionA
// after
if (existingProject.Session is not null && existingProject.Session != sessionB)
    existingProject.Session.Release(); // or use a freshly loaded PackageContainer
sessionB.Load(existingProject);
Defensive patterns

Strategy: validation

Validate before calling

if (project.Session is not null && project.Session != targetSession)
    throw new InvalidOperationException("Project already belongs to another session.");

Type guard

static bool CanAttach(PackageContainer project, PackageSession session) => project.Session is null || project.Session == session;

Try / catch

try { session.Load(project); }
catch (InvalidOperationException ex) when (ex.Message.Contains("more than one session"))
{
    // release/detach from old session or load a fresh PackageContainer
}

Prevention

When it happens

Trigger: Calling a session API (Load, AttachProject, Save, etc.) that internally registers a project which was already loaded into, or attached to, a different PackageSession instance.

Common situations: Reusing a loaded Package across two sessions (e.g. opening the same project in two sessions); loading a package file into session B after session A already owns it; not disposing the old session before creating a new one.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/002927ae0aa98346. Report an issue: GitHub.

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/PackageSession.cs:1387

                foreach (var oldProject in e.OldItems?.OfType<PackageContainer>() ?? [])
                {
                    UnRegisterProject(oldProject);
                }

                foreach (var projectToCopy in Projects)
                {
                    RegisterProject(projectToCopy);
                }
                break;
        }
    }

    private void RegisterProject(PackageContainer project)
    {
        if (project.Session is not null)
        {
            throw new InvalidOperationException("Cannot attach a project to more than one session");
        }

        project.SetSessionInternal(this);

        if (project is SolutionProject solutionProject)
        {
            // Note: when loading, package might already be there
            // TODO CSPROJ=XKPKG: skip it in a proper way? (context info)
            if (!VSSolution.Projects.Contains(solutionProject.VSProject))
            {
                // Special case: let's put executable windows project first, so that Visual Studio use them as startup project (first project in .sln)
                var insertPosition = (solutionProject.Type == ProjectType.Executable && solutionProject.Platform == PlatformType.Windows)
                    ? 0
                    : VSSolution.Projects.Count;
                VSSolution.Projects.Insert(insertPosition, solutionProject.VSProject);
            }
        }

View on GitHub (pinned to 96fad776d2)