Skip to content

Receiving rendered redis blob instead of string content on high memory pressure #2913

Closed

Description

@jcaspes

Hello,

While testing the StackExchange.Redis library (on HGETALL api) under high memory pressure to induce frequent OutOfMemory exceptions, I encountered cases where the returned string value was a hexadecimal representation of the Redis response bytes (e.g., "4C-E8-3A...") instead of valid data. Notably, the client API did not report any error in these scenarios.

This behavior originates from the RedisValue string conversion operator: https://github.com/StackExchange/StackExchange.Redis/blob/8a527830fd45b5eaef54e9b3b96f3f6ca7af3813/src/StackExchange.Redis/RedisValue.cs#L789

The conversion uses a try/catch block that, on any exception, returns the hex string. Is there a specific reason or intended purpose for this approach?

This forces us to check the validity of the values returned by the library, which makes things complicated.
Why not let the exception be thrown?

Activity

  1. mgravell commented on Jul 19, 2025

    @mgravell
    Member

    That is solving the wrong problem. It is fairly normal and expected for BLOBs to be Redis payloads, and it is meaningful and useful to have a tostring that renders that.

    The real problem here is that somehow we (meaning: me) screwed up the results and gave you corrupted data. I've seen multiple reports of this, and it is highly critical to fix this ASAP.

    Good news: I have now joined Redis on the client library team, so fixing this is literally my day job. I consider this to be top priority, so: if there's anything you can share to help me repro this, I really really want to fix the underlying problem.

  2. jcaspes commented on Jul 21, 2025

    @jcaspes
    ContributorAuthor

    Hello, ok, i undertand the need.

    Just to introduce my context, i'm migrating from ServiceStack.Redis to StackExchange.Redis on a webapp in .Net Framework.
    Some year ago, some user was seeing data from other ones in high memory pressure context, and i've found a bug in ServiceStack (old version) that make that socket buffer was badly cleanned on a OOM exception... so data was served to next redis query... (with connection pool mode).

    To fix / diagnose i've writed a small program that create multiple threads and load the memory to get OOM exceptions. I'm cleanning it so i will share it with you on my git account.

    So before migration to servicetack i need to be sure to not have the same problem on high memory pressure, so i've adapted the programme to use servicestack lib.
    => I found 3 problems:
    1- The one i've described here, i receive rendered blob in place of the excpected string.
    2- I receive the data asked from an other thread (did you prefer that i create an other issue for this one ?)
    3- Some time HashGetAll return a null or empty arry, it's not expected, it should throw

    The difficulty is that it generate random OOM exception so not always reproduce the tow problems.

    (I update the title of thi issue and come bak to you when the program is ready)

  3. changed the title [-]Invalid data on high memory pressure[/-] [+]Receiving rendered redis blob instead of string content on high memory pressure[/+] on Jul 21, 2025
  4. jcaspes commented on Jul 21, 2025

    @jcaspes
    ContributorAuthor

    Hey,
    I've added the program in a fork here:
    https://github.com/jcaspes/StackExchange.Redis/tree/MemoryTester/toys/MemoryTester

    I will add the two other cases i have detected in new issues

    Don't hesitate if you need more informations

  5. mgravell commented on Jul 21, 2025

    @mgravell
    Member

    Thanks, I've pulled that down; no luck with a repro yet, but I guess it may take some time...

    Image
  6. jcaspes commented on Jul 21, 2025

    @jcaspes
    ContributorAuthor

    Yes some time it take long other time not...
    For this issue with blob rendered, you must give the argument checkContent (donet run checkContent) to reproduce.

    Image
  7. mgravell commented on Jul 21, 2025

    @mgravell
    Member

    Well, I left my laptop alone for a few hours trying to repro this - penultimate time of checking: no repro, came back a bit later: I'd exploded the OS with BTRFS failures. So that was fun. Will try again tomorrow.

    Question: what OS are you seeing this on?

  8. jcaspes commented on Jul 21, 2025

    @jcaspes
    ContributorAuthor

    I'm in an Hyper-V VM, the hoster is a windows 2011 enterprise, and the VM where the test run is windows 2022 server (VM with 50G memory and 16 logical processors).
    It need to run quick, for example when i enable logs, it's more difficult to reproduce, all depends on where the random OutOfMemory occure...

  9. jcaspes commented on Jul 22, 2025

    @jcaspes
    ContributorAuthor

    An other thing, i build in 32bit so memory limit in windows is 1.5Gb, it should not hang your computer

  10. mgravell commented on Jul 22, 2025

    @mgravell
    Member

    Ah, so it turns out that linux really, really doesn't want to run in 32-bit mode:

    Image

    I'll dust off my Windows hardware...

  11. mgravell commented on Jul 22, 2025

    @mgravell
    Member

    Well, the "good" news is that it seems to fairly reliably break on Windows very quickly (repeatedly under 30s); that gives me something to work with, at least; thanks.

    My plan starts with hacking out any memory reuse in, for example, the deframing arena, network buffers, etc, to see if we can find a smoking gun. I'm cautiously hopeful that this is all my fault, which is fine: I can fix things - my worry is that there's an underlying framework snafu in, for example, pooling, GC or network-stack; that will be harder to rectify.

  12. jcaspes commented on Jul 22, 2025

    @jcaspes
    ContributorAuthor

    OK, cool.
    You can also add the log parameter (MemoryTester.exe checkcontent log) it will trace all different stack and exception type found, it can help

  13. jcaspes commented on Jul 22, 2025

    @jcaspes
    ContributorAuthor

    For this issue only in RedisValue class where you try/catch the GetString call, i can see that the GetString only call the Encoding.UTF8.GetString api.

    This api can only throw:

    ArgumentException
    The byte array contains invalid Unicode code points.
    ArgumentNullException
    bytes is null.
    DecoderFallbackException
    A fallback occurred (for more information, see Character Encoding in .NET)

    I expect that only the case ArgumentOutOfRangeException need to return the rendered blob because of not convertible to UTF8 string, and for other exceptions just re-throw it so caller is notified of a problem, like OutOfMemory?

    I've fixed in this way in a branch to continue testing without this render error:
    jcaspes@75b3013

  14. vverdier commented on Aug 14, 2025

    @vverdier

    Hello, I'm a colleague of @jcaspes.
    He's currently out of the office.
    Just checking if there's any update on this issue.
    Thanks!

  15. mgravell commented on Aug 14, 2025

    @mgravell
    Member

    Working on it, as a high priority.

  16. vverdier commented on Aug 20, 2025

    @vverdier

    Do you have any estimation for the delivery of the fix? It would really help us with our planning on our side.
    Thanks again for addressing this issue as a high priority 👍

  17. mgravell commented on Aug 20, 2025

    @mgravell
    Member

    It is basically my main priority at the moment; note that enabling high-integrity mode (see docs) seems to be an effective temporary mitigation, but: I fully acknowledge that this is not a fix.

    Short version: we fully know that something is broken here, that seems to be especially problematic in low-memory scenarios, but we have not fully isolated a cause. There's two streams of work:

    1. try to see if we can identify the exact cause, and mitigate - most likely culprits are the arena allocator and the pipelines code
    2. push ahead with separate unrelated proposed changes that remove both of the above entirely

    In a perfect world, we'd get both implemented, so "1" lands in 2.* and "2" lands in 3.*. My investigations into both 1 & 2 continue. Unfortunately, it is hard to give an ETA on finding an exotic bug.

  18. jcaspes commented on Sep 12, 2025

    @jcaspes
    ContributorAuthor

    Hi,
    If you look my memorytest commandline code, i've already tested the high-integrity mode, but unfortunately, that doesn't fix the problem, setting this parameter to true do not avoid errors.

    https://github.com/jcaspes/StackExchange.Redis/blob/fd470d1b5dc78900c84518b479a46ae84b20a442/toys/MemoryTester/Program.cs#L48

    By looking the highintegrity code, i see that it send an ECHO message prior to the asked command and check the response. if this do not fix, it certainly means that it's the bottom layer that's causing the issues.

    I will soon have more time to digg on theses issues, If you have any ideas to explore, feel free to share them :-)

  19. jcaspes commented on Sep 18, 2025

    @jcaspes
    ContributorAuthor

    Hello @mgravell,

    I've now time to work on these issues has my enterprise really want to migrate to StackExchange.Redis lib and gave us time to help on issues i've created and that are blocking the usage of the lib for security reason.

    For the current issue #2913 and not the 2 other one #2919 and #2920 I think that the fix i propose in this PR is necessary, here an explanation:

    Current is not a problem of corrupted data.
    Even if data is not corrupted, during high memory pressure the call to Encoding.UTF8.GetString can throw an OutOfMemory exception like everywhere where the code allocates memory. In this case we will receive the hex value because of the try/catch(*), i really think that it is not a good idea to catch all kind of exception here.

  20. mgravell commented on Sep 19, 2025

    @mgravell
    Member

    On the exceptions: agreed, and your PR looks like it is heading in the right direction.

  21. mgravell commented on Sep 26, 2025

    @mgravell
    Member

    I've confirmed this with the current merged main state; we'll get that released as soon as we can. Thanks for your input.

  22. mgravell commented on Sep 26, 2025

    @mgravell
    Member

    Closed via 2.9.24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions