Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a4065ea

Browse files
authored
Merge pull request #234 from gertgoeman/master
Exception in pipeline callback breaks RedisClient
2 parents e8a63bc + 3133e10 commit a4065ea

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/ServiceStack.Redis/Pipeline/RedisAllPurposePipeline.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ public void Flush()
3535
{
3636
// flush send buffers
3737
RedisClient.FlushAndResetSendBuffer();
38-
39-
//receive expected results
40-
foreach (var queuedCommand in QueuedCommands)
38+
39+
try
40+
{
41+
//receive expected results
42+
foreach (var queuedCommand in QueuedCommands)
43+
{
44+
queuedCommand.ProcessResult();
45+
}
46+
}
47+
catch (Exception)
4148
{
42-
queuedCommand.ProcessResult();
49+
// The connection cannot be reused anymore. All queued commands have been sent to redis. Even if a new command is executed, the next response read from the
50+
// network stream can be the response of one of the queued commands, depending on when the exception occurred. This response would be invalid for the new command.
51+
RedisClient.DisposeConnection();
52+
throw;
4353
}
54+
4455
ClosePipeline();
45-
4656
}
4757

4858
protected void Execute()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Redis.Tests.Issues
5+
{
6+
[TestFixture]
7+
public class PipelineIssueTests
8+
: RedisClientTestsBase
9+
{
10+
[Test]
11+
public void Disposing_Client_Clears_Pipeline()
12+
{
13+
var clientMgr = new PooledRedisClientManager(TestConfig.SingleHost);
14+
15+
using (var client = clientMgr.GetClient())
16+
{
17+
client.Set("k1", "v1");
18+
client.Set("k2", "v2");
19+
client.Set("k3", "v3");
20+
21+
using (var pipe = client.CreatePipeline())
22+
{
23+
pipe.QueueCommand(c => c.Get<string>("k1"), p => { throw new Exception(); });
24+
pipe.QueueCommand(c => c.Get<string>("k2"));
25+
26+
try
27+
{
28+
pipe.Flush();
29+
}
30+
catch (Exception)
31+
{
32+
//The exception is expected. Swallow it.
33+
}
34+
}
35+
}
36+
37+
using (var client = clientMgr.GetClient())
38+
{
39+
Assert.AreEqual("v3", client.Get<string>("k3"));
40+
}
41+
}
42+
}
43+
}

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
<Compile Include="ConfigTests.cs" />
196196
<Compile Include="CustomCommandTests.cs" />
197197
<Compile Include="Issues\AuthIssue.cs" />
198+
<Compile Include="Issues\PipelineIssueTests.cs" />
198199
<Compile Include="LuaCachedScripts.cs" />
199200
<Compile Include="Examples\TestData.cs" />
200201
<Compile Include="Issues\RedisCharacterizationTests.cs" />

0 commit comments

Comments
 (0)