stride3d/stride · error · InvalidOperationException

Unable to unload the current profile.

Error message

Unable to unload the current profile.

What it means

UnloadSettingsProfile refuses to unload the profile that is currently active (CurrentProfile) with InvalidOperationException, since removing the active profile would leave SettingsKey reads without a valid resolution target. This is a state check that runs after the root-profile check.

Solutions

  1. Set CurrentProfile to another profile (e.g. the RootProfile) before unloading
  2. Do not unload the current profile; keep it until shutdown
  3. If the intent is to replace a loaded file profile, load the replacement first, then unload the old one

Example fix

// before
container.UnloadSettingsProfile(container.CurrentProfile);
// after
container.CurrentProfile = container.RootProfile;
container.UnloadSettingsProfile(oldProfile);
Defensive patterns

Strategy: validation

Validate before calling

if (profile == container.CurrentProfile)
    container.CurrentProfile = container.RootProfile;
container.UnloadSettingsProfile(profile);

Try / catch

try { container.UnloadSettingsProfile(profile); }
catch (InvalidOperationException) { /* still current: switch profile first */ }

Prevention

When it happens

Trigger: Calling UnloadSettingsProfile(profile) while profile == container.CurrentProfile, e.g. trying to dispose the settings profile that the application is actively using.

Common situations: Shutdown/teardown code unloading profiles in arbitrary order; switching between build configs where the loaded override file is also the current profile; calling unload immediately after CreateSettingsProfile(setAsCurrent: true).

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

Appendix: source

Thrown at sources/core/Stride.Core.Design/Settings/SettingsContainer.cs:207

        catch (Exception e)
        {
            Logger.Error($"Error while loading settings file [{filePath}].", e);
        }

        var handler = SettingsFileLoaded;
        handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath));
    }

    /// <summary>
    /// Unloads a profile that was previously loaded.
    /// </summary>
    /// <param name="profile">The profile to unload.</param>
    public void UnloadSettingsProfile(SettingsProfile profile)
    {
        if (profile == RootProfile)
            throw new ArgumentException("The default profile cannot be unloaded");
        if (profile == CurrentProfile)
            throw new InvalidOperationException("Unable to unload the current profile.");
        lock (SettingsLock)
        {
            profileList.Remove(profile);
        }
    }

    /// <summary>
    /// Saves the given settings profile to a file at the given path.
    /// </summary>
    /// <param name="profile">The profile to save.</param>
    /// <param name="filePath">The path of the file.</param>
    /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns>
    public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath)
    {
#if NET6_0_OR_GREATER
        ArgumentNullException.ThrowIfNull(profile);
#else
        if (profile is null) throw new ArgumentNullException(nameof(profile));

View on GitHub (pinned to 96fad776d2)