mgth/LittleBigMouse · error · InvalidOperationException

The VIDAA connection is not open.

Error message

The VIDAA connection is not open.

What it means

The private Open() accessor returns the current connection object but throws InvalidOperationException if _connection is null, i.e. the session is not currently open. PublishAsync/SubscribeAsync/UnsubscribeAsync rely on it to guarantee they never operate on a closed connection.

Solutions

  1. Call EnsureOpenAsync(ct) (or await ReopenAsync) before any publish/subscribe call
  2. Check the Connected property before publishing and reopen when false
  3. Wrap publishes in try/catch on InvalidOperationException and reopen+retry once
  4. Investigate why the connection dropped (device standby, network) if it recurs

Example fix

// before
session.PublishAsync(topic, payload, ct); // may throw if closed
// after
await session.EnsureOpenAsync(ct);
await session.PublishAsync(topic, payload, ct);
Defensive patterns

Strategy: retry

Validate before calling

if (!session.Connected)
    await session.EnsureOpenAsync(ct);
await session.PublishAsync(topic, payload, ct);

Type guard

bool CanPublish(IVidaaSession session) => session.Connected;

Try / catch

try
{
    await session.PublishAsync(topic, payload, ct);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("VIDAA connection is not open"))
{
    await session.ReopenAsync(ct);
    await session.PublishAsync(topic, payload, ct);
}

Prevention

When it happens

Trigger: Calling PublishAsync, SubscribeAsync or UnsubscribeAsync on a VidaaSession before EnsureOpenAsync succeeded, or after CloseAsync/the connection dropped (device offline, socket closed, exception in the message loop).

Common situations: Fire-and-forget publish after a network blip killed the connection; subscribing to response topics before opening; a background worker keeps publishing while the UI closed the session; race between reconnect and publish.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of mgth/LittleBigMouse@7a42f01d47 (2026-09-16). Data as JSON: /api/errors/8eac6296f15fcc5e. Report an issue: GitHub.

Appendix: source

Thrown at LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp.Avalonia/HisenseVidaa/VidaaSession.cs:111

        connection.MessageReceived -= Dispatch;
        await connection.DisposeAsync().ConfigureAwait(false);
    }

    /// <summary>
    /// Tells a session the broker dropped from a command the device refused, so that only the
    /// former is worth sending again.
    /// </summary>
    public static bool IsConnectionFailure(Exception exception)
        => exception is IOException or SocketException or ObjectDisposedException;

    async Task ReopenAsync(CancellationToken cancellationToken)
    {
        await CloseAsync().ConfigureAwait(false);
        await EnsureOpenAsync(cancellationToken).ConfigureAwait(false);
    }

    IVidaaConnection Open()
        => _connection ?? throw new InvalidOperationException("The VIDAA connection is not open.");

    void Dispatch(string topic, byte[] payload, bool retained)
        => MessageReceived?.Invoke(topic, payload, retained);
}

View on GitHub (pinned to 7a42f01d47)