Skip to content

Instantly share code, notes, and snippets.

@Voltara
Created March 11, 2022 20:49
Show Gist options
  • Save Voltara/3034ebc9efb79c78463a27a247996e7d to your computer and use it in GitHub Desktop.
Save Voltara/3034ebc9efb79c78463a27a247996e7d to your computer and use it in GitHub Desktop.
StackExchange.Redis #1294 Reproduction
using StackExchange.Redis;
void WriteToRedis(IDatabase db, string key, int len, int fillByte)
{
byte[] value = Enumerable.Repeat((byte) fillByte, len).ToArray();
Console.WriteLine($"\n\nWriting key '{key}', length {len}...");
try
{
db.StringSet(key, value);
}
catch (Exception ex)
{
Console.WriteLine($"WriteToRedis caught exception: {ex.Message}");
}
}
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase db = redis.GetDatabase();
/* Magic length 555 will simulate an OutOfMemoryException
* in the middle of writing the value. The command will fail
* by timing out after 5000ms
*/
db.KeyDelete("key1");
WriteToRedis(db, "key1", 555, 'x');
/* This command will break protocol encapsulation, and will
* be partially stored in "key1"
*/
WriteToRedis(db, "key2", 222, 'y');
// Force a reconnect, then print the value of "key1"
redis.Dispose();
redis = ConnectionMultiplexer.Connect("localhost");
db = redis.GetDatabase();
var value = db.StringGet("key1");
Console.WriteLine($"\n\nStored value for key1 was: {value}");
diff --git a/src/StackExchange.Redis/Message.cs b/src/StackExchange.Redis/Message.cs
index c8fdf54f..939c898f 100644
--- a/src/StackExchange.Redis/Message.cs
+++ b/src/StackExchange.Redis/Message.cs
@@ -769,6 +769,7 @@ internal void WriteTo(PhysicalConnection physical)
}
catch (Exception ex) when (!(ex is RedisCommandException)) // these have specific meaning; don't wrap
{
+ Console.WriteLine($"Message.WriteTo caught {ex.Message}");
physical?.OnInternalError(ex);
Fail(ConnectionFailureType.InternalFailure, ex, null);
}
diff --git a/src/StackExchange.Redis/PhysicalConnection.cs b/src/StackExchange.Redis/PhysicalConnection.cs
index bbbccf4e..ff93a0ba 100644
--- a/src/StackExchange.Redis/PhysicalConnection.cs
+++ b/src/StackExchange.Redis/PhysicalConnection.cs
@@ -998,7 +998,15 @@ private static void WriteUnifiedSpan(PipeWriter writer, ReadOnlySpan<byte> value
int bytes = WriteRaw(span, value.Length, offset: 1);
writer.Advance(bytes);
- writer.Write(value);
+ if (value.Length == 555)
+ {
+ writer.Write(value.Slice(0, 400));
+ throw new OutOfMemoryException();
+ }
+ else
+ {
+ writer.Write(value);
+ }
WriteCrlf(writer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment