stride3d/stride · error · InvalidOperationException

Cannot detach a project that was not attached to this…

Error message

Cannot detach a project that was not attached to this session

What it means

Thrown by PackageSession.UnRegisterProject when the project being detached is not registered with this session (project.Session != this). Detachment is only valid for projects owned by the session, otherwise internal collections (packages, solution projects) would be corrupted.

Solutions

  1. Only call detach for projects where project.Session equals the target session.
  2. Guard the call: skip detachment when project.Session is null or foreign.
  3. Re-attach the project to this session first if it genuinely needs to be removed from this one.

Example fix

// before
session.UnRegisterProject(project); // project belongs to another session
// after
if (project.Session == session)
    session.UnRegisterProject(project);
Defensive patterns

Strategy: validation

Validate before calling

if (project.Session == session)
    session.UnRegisterProject(project);

Type guard

static bool CanDetach(PackageContainer project, PackageSession session) => project.Session == session;

Try / catch

try { session.UnRegisterProject(project); }
catch (InvalidOperationException ex) when (ex.Message.Contains("was not attached"))
{
    // skip: nothing to detach
}

Prevention

When it happens

Trigger: Calling a detach/unregister API for a project whose Session is null (never attached) or points to a different PackageSession instance.

Common situations: Detaching a project that was loaded by another session; double-detaching the same project; attempting cleanup on a fresh project object that was never attached.

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/c017bfeb7d723617. Report an issue: GitHub.

Appendix: source

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

            // 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);
            }
        }

        packages.Add(project.Package);
    }

    private void UnRegisterProject(PackageContainer project)
    {
        if (project.Session != this)
        {
            throw new InvalidOperationException("Cannot detach a project that was not attached to this session");
        }

        if (project.Package is not null)
            packages.Remove(project.Package);
        if (project is SolutionProject solutionProject)
        {
            VSSolution.Projects.Remove(solutionProject.VSProject);
        }

        project.SetSessionInternal(null);
    }

    private void PackagesCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                {

View on GitHub (pinned to 96fad776d2)