dice icon indicating copy to clipboard operation
dice copied to clipboard

fix: Handle multiple watch commands for same client and minor improvements

Open Kaushal-26 opened this issue 7 months ago • 0 comments

Running other watch command after killing first watch does not work

Steps to reproduce:

  • Kill watch connection between client and server using CTRL+C and restart another does not work properly

Example with dicedb-cli run on 2 terminals:

After changes:

>> make run
go run main.go
localhost:7379> SET a v1
OK
localhost:7379> SET k w1
OK
localhost:7379> GET.WATCH k
entered the watch mode for GET.WATCH k
OK [fingerprint=6645377565653310095] "w2" # SET k w2 (on Terminal 2)
OK [fingerprint=6645377565653310095] "w3" # SET k w3 (on Terminal 2)
^Cexiting the watch mode. back to command mode
localhost:7379> GET a
OK "v1"
localhost:7379> GET k
OK "w3"
localhost:7379> GET.WATCH k
entered the watch mode for GET.WATCH k
OK [fingerprint=6645377565653310095] "w3" # GET k (on Terminal 2)
OK [fingerprint=6645377565653310095] "e1" # SET k e1 (on Terminal 2)
OK [fingerprint=6645377565653310095] "e2" # SET k e2 (on Terminal 2)
^Cexiting the watch mode. back to command mode
localhost:7379> GET a
OK "v1"
localhost:7379> GET k
OK "e2"

Before changes:

>> make run
go run main.go
localhost:7379> SET a v1
OK
localhost:7379> SET k w1
OK
localhost:7379> GET.WATCH k
entered the watch mode for GET.WATCH k 
OK [fingerprint=6645377565653310095] "w3"
^Cexiting the watch mode. back to command mode
localhost:7379>  GET a
OK "v1"
localhost:7379> GET k
OK "w3"
localhost:7379> GET.WATCH k
entered the watch mode for GET.WATCH k
OK [fingerprint=6645377565653310095] "w3" 
^Cexiting the watch mode. back to command mode
localhost:7379> GET a
OK [fingerprint=6645377565653310095] "e1"
localhost:7379> GET k
OK [fingerprint=6645377565653310095] "e2"

Related PR's:

  • https://github.com/DiceDB/dicedb-cli/pull/45
  • https://github.com/DiceDB/dicedb-go/pull/17

Fixes: https://github.com/DiceDB/dice/issues/1696


  • Simplified ClientID assignment logic to ensure it is set directly without error checks after the initial loop.
  • Updated HANDSHAKE command handling to directly assign ClientID and Mode without redundant error checks.
  • Modified watch command handling to ensure proper notification of watchers without unnecessary conditions.
  • Enhanced cleanup logic in WatchManager to delete command entries when fingerprints are removed.

These changes improve code clarity and maintainability while ensuring correct behavior during command processing.

Kaushal-26 avatar May 17 '25 14:05 Kaushal-26