Skip to content

Commit fdb8221

Browse files
committed
SENTINEL: defensive check around ROLE
1 parent bd2a816 commit fdb8221

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

docs/ReleaseNotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Current package versions:
88

99
## Unreleased
1010

11-
- (none)
11+
- Avoid sentinel issues if `ROLE` unavailable; fix #3064
1212

1313
## 2.13.10
1414

src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,28 @@ public ConnectionMultiplexer GetSentinelMasterConnection(ConfigurationOptions co
240240

241241
// verify role is primary according to:
242242
// https://redis.io/topics/sentinel-clients
243-
if (connection.GetServer(newPrimaryEndPoint)?.Role()?.Value == RedisLiterals.master)
243+
bool isPrimary;
244+
var server = connection.GetServer(newPrimaryEndPoint);
245+
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
246+
if (server is { })
244247
{
245-
success = true;
246-
break;
248+
try
249+
{
250+
isPrimary = connection.CommandMap.IsAvailable(RedisCommand.ROLE)
251+
? server.Role()?.Value == RedisLiterals.master
252+
: !server.IsReplica;
253+
}
254+
catch
255+
{
256+
// fallback if ROLE unavailable but not declared; see #3064
257+
isPrimary = !server.IsReplica;
258+
}
259+
260+
if (isPrimary)
261+
{
262+
success = true;
263+
break;
264+
}
247265
}
248266

249267
Thread.Sleep(100);

0 commit comments

Comments
 (0)