diff --git a/libs/server/Resp/Objects/SetCommands.cs b/libs/server/Resp/Objects/SetCommands.cs index 21167b9ebcc..30e07867a5b 100644 --- a/libs/server/Resp/Objects/SetCommands.cs +++ b/libs/server/Resp/Objects/SetCommands.cs @@ -535,8 +535,7 @@ private unsafe bool SetPop(ref TGarnetApi storageApi) if (countParameter == 0) { - while (!RespWriteUtils.TryWriteEmptyArray(ref dcurr, dend)) - SendAndReset(); + WriteEmptySet(); return true; } @@ -558,6 +557,13 @@ private unsafe bool SetPop(ref TGarnetApi storageApi) ProcessOutput(output.SpanByteAndMemory); break; case GarnetStatus.NOTFOUND: + if (parseState.Count == 2) + { + // SPOP key count replies with an empty set when the key does not exist + WriteEmptySet(); + break; + } + WriteNull(); break; case GarnetStatus.WRONGTYPE: diff --git a/test/standalone/Garnet.test.collections/RespSetTest.cs b/test/standalone/Garnet.test.collections/RespSetTest.cs index 0068f621edb..b5bcec88b42 100644 --- a/test/standalone/Garnet.test.collections/RespSetTest.cs +++ b/test/standalone/Garnet.test.collections/RespSetTest.cs @@ -1483,6 +1483,15 @@ public void CanDoSPOPCommandWhenKeyDoesNotExistLC() TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response); } + [Test] + public void CanDoSPOPWithCountCommandWhenKeyDoesNotExistLC() + { + using var lightClientRequest = TestUtils.CreateRequest(); + var response = lightClientRequest.SendCommand("SPOP fooset 3"); + var expectedResponse = "*0\r\n"; + TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response); + } + [Test] public void CanUseNotExistingSetwithSMembers() {