microsoft/garnet · error · ArgumentException

Elements collection cannot be empty.

Error message

Elements collection cannot be empty.

What it means

ListLeftPush(key, IEnumerable<string> elements, ...) builds arrElem = {key} ∪ elements and throws ArgumentException when the union has length 1 — meaning elements contributed zero items. This rejects an empty LPUSH (Redis semantics: LPUSH requires at least one value). The key-only array is detected as the degenerate case.

Solutions

  1. Check elements.Any() before calling ListLeftPush, or skip the call entirely when there is nothing to push.
  2. Use the async ListLeftPushAsync(params string[]) overload only if you have at least one element.
  3. Guard the caller to never construct an empty push.

Example fix

// before
client.ListLeftPush(key, values, callback);

// after — skip empty pushes
if (values is null || !values.Any()) return;
client.ListLeftPush(key, values, callback);
Defensive patterns

Strategy: validation

Validate before calling

if (elements is null || !elements.Any()) return; // or throw a clearer error at the caller
client.ListLeftPush(key, elements, callback);

Type guard

bool HasElements(IEnumerable<string> e) => e is not null && e.Any();

Prevention

When it happens

Trigger: Calling ListLeftPush with an empty IEnumerable<string>, or one whose Union with {key} yields only the key (e.g. elements is an empty list, or contains only items already equal to key that get de-duplicated by Union).

Common situations: Passing an empty collection variable (e.g. from a query that returned no rows); a default/empty List<string>; a code path that pushes only when data exists but the guard is missing.

Related errors


AI-assisted analysis of microsoft/garnet@951b0fc683 (2026-08-13). Data as JSON: /api/errors/90f0cd7929cd1d4d. Report an issue: GitHub.

Appendix: source

Thrown at libs/client/GarnetClientAPI/GarnetClientListCommands.cs:47

        /// <summary>
        /// Add the specified elements to the head of the list stored at key.
        /// </summary>
        /// <param name="key">The key of the list.</param>
        /// <param name="elements">The elements to be added.</param>
        /// <param name="callback">The callback function when operation completes.</param>
        /// <param name="context">An optional context to correlate request to callback.</param>
        public void ListLeftPush(string key, IEnumerable<string> elements, Action<long, long, string> callback, long context = 0)
        {
            ArgumentNullException.ThrowIfNull(key);
            ArgumentNullException.ThrowIfNull(elements);
            ArgumentNullException.ThrowIfNull(callback);

            var arrElem = new[] { key }.Union(elements).ToArray();

            if (arrElem.Length == 1)
            {
                throw new ArgumentException("Elements collection cannot be empty.", nameof(elements));
            }

            ExecuteForLongResult(callback, context, nameof(LPUSH), arrElem);
        }

        /// <summary>
        /// Asynchronously add the specified elements to the head of the list stored at key.
        /// </summary>
        /// <param name="key">The key of the list.</param>
        /// <param name="elements">The elements to be added.</param>
        /// <returns>The number of list elements after the addition.</returns>
        public async Task<long> ListLeftPushAsync(string key, params string[] elements)
        {
            ArgumentNullException.ThrowIfNull(key);
            ArgumentNullException.ThrowIfNull(elements);

            if (elements.Length == 0)
            {

View on GitHub (pinned to 951b0fc683)