#4545·valkey

[BUG] Atomic slot migration fails with "Protocol error: invalid CRLF in request" when the migrating slots are taking writes

Author: ben-kuglerCreated Aug 28, 2026Updated Sep 26, 2026

Describe the bug

CLUSTER MIGRATESLOTS fails during the snapshotting phase if client writes are landing on the slots being migrated. The import target reads a malformed command off the migration link, reports a protocol error back to the source, and both sides tear the migration down.

The failure is probabilistic at low write rates and deterministic at and above roughly 30,000 writes/sec aimed at the migrating range. Because the slots never move, an orchestrator that retries gets stuck permanently: we observed 268 consecutive failed attempts over five minutes on a cluster under sustained write load, with the shard count never advancing.

Only writes to the slots being migrated trigger it. Reads do not, and writes to other slots on the same source node do not.

Version

valkey_version:9.1.1

Also reproduced on 9.1.0 and on 9.0.5. See the version table below.

Environment

Two valkey-server processes on one host, cluster mode, no replicas, no TLS, no modules. Reproduced identically on separate EC2 instances over a real network and on loopback.

Relevant config on both nodes:

cluster-enabled yes
appendonly no
save ""
maxmemory 6gb
maxmemory-policy volatile-lru
io-threads 3
client-output-buffer-limit replica 3367mb 3367mb 112
repl-backlog-size 598mb

To reproduce

Start two nodes and join them:

bash
for p in 7001 7002; do
  mkdir -p /tmp/vk$p
  valkey-server --port $p --cluster-enabled yes --dir /tmp/vk$p \
                --cluster-config-file nodes.conf --appendonly no --save '' \
                --io-threads 3 --daemonize yes \
                --logfile /tmp/vk$p/valkey.log
done
valkey-cli -p 7001 cluster addslotsrange 0 8191
valkey-cli -p 7002 cluster addslotsrange 8192 16383
valkey-cli -p 7001 cluster meet 127.0.0.1 7002

Then run this script (https://gist.github.com/ben-kugler/e4fbb028cb0a9189736114f89b9547fb, stdlib Python only). It fills slots 0-199, starts writes aimed at exactly those slots, and issues the migration:

bash
python3 repro_slot_migration.py --source 127.0.0.1:7001 --target 127.0.0.1:7002
[repro] 256 hash tags covering slots 0-199
[repro] filling slots 0-199 for 30s ...
[repro] 512,000 keys in slots 0-199
[repro] starting 120,000 writes/sec at slots 0-199 ...
[repro] CLUSTER MIGRATESLOTS SLOTSRANGE 0 199 NODE <target-id>
[repro] -> OK
[repro] RESULT: failed  after 0.3s
[repro] source message: Connection lost to target. Check CLUSTER GETSLOTMIGRATIONS on the target node
[repro] target message: Failed to process command during slot migration. Check logs for more information

The script picks hash tags so that every generated key provably hashes into the migrating range, which is the essential condition. It also writes each key's own name into its value, so --verify can confirm data integrity whenever a migration does succeed.

Expected behaviour

The migration completes. Concurrent writes to migrating slots are an expected workload — the whole point of atomic slot migration is to reshard a live cluster.

Actual behaviour

Source:

* Slot migration initiated through CLUSTER MIGRATESLOTS command: {... slots: 0-199}
* Beginning snapshot of slot migration {... slots: 0-199}.
* Started child process 3738 for slot migration {... slots: 0-199}
* Slot migration {... slots: 0-199} state transition: waiting-to-snapshot -> snapshotting
* Slot migration {... slots: 0-199} connection lost
* Slot migration {... slots: 0-199} state transition: snapshotting -> failed
# Slot migration {... slots: 0-199} finished. State: failed,
  Message: Connection lost to target.

Target:

* New slot import job created: {... slots: 0-199}.
* Slot migration {... slots: 0-199} state transition: waiting-for-ack -> receiving-snapshot
# == CRITICAL == This slot-import-target is sending an error to its slot-import-source:
  'Protocol error: invalid CRLF in request' after processing the command 'set'
* Slot migration {... slots: 0-199} state transition: receiving-snapshot -> cleaning-up
# Slot migration {... slots: 0-199} finished. State: failed,
  Message: Failed to process command during slot migration.

The whole sequence takes ~270 ms from fork to teardown. On a separate cluster we also saw these variants of the same desync:

WARNING: Receiving inline protocol from slot import, import stream corruption? Closing.
Protocol error (Import using the inline protocol. Desync?) ... cmd=set
  qbuf=163830 qbuf-free=0 tot-net-in=182808517 tot-cmds=299244

The set named in the error is not a client command — it is a mutation being replayed into the migration link. The target is reporting that the bytes it received were not a well-formed command.

How reliably

Each row is a separate migration of 200 slots holding ~512,000 keys, varying only the load aimed at those slots. 100 runs on 9.1.1:

Writes on the migrating slots Value size Slots migrating Failed
none — 200 0 / 9
120,000/s, reads only 512 B 200 0 / 1
120,000/s, writes to other slots on the same source 512 B 200 0 / 1
2,000/s 512 B 200 5 / 10
5,000/s 512 B 200 5 / 10
10,000/s 512 B 200 5 / 10
20,000/s 512 B 200 5 / 10
20,000/s 512 B 2,000 2 / 5
30,000/s 512 B 200 10 / 10
50,000–120,000/s 512 B 200 20 / 20
20,000/s 8 KiB 200 5 / 5
20,000/s 32 KiB 200 5 / 5

Two things stand out: the size of the migrating range barely matters (2,000 slots behaves like 200), so this is not about how long the snapshot runs; and value size matters a great deal — at an identical 20,000 writes/sec, 8 KiB values fail every time where 512 B values fail half the time.

Which versions

Five runs per build, same workload throughout:

Build Date Failed
9.0.0 2025-10-21 0 / 5
cd6faaa 2025-12-12 0 / 5
51f871a (#2872) 2025-12-14 0 / 5
9.0.5 2026-07-21 1 / 5
9.1.0 2026-05-19 5 / 5
9.1.1 2026-07-21 5 / 5

We could not identify a first-bad-commit and are not claiming one. Note the asymmetry: a failure proves a build is affected, but a success does not prove it is unaffected, because the failure is probabilistic. A git bisect over this symptom produced a confident but meaningless answer for exactly that reason — every commit it tested was affected and it converged on the range boundary. A 0/5 result is consistent with a true failure rate up to roughly one in three, so the older builds should be read as "much less exposed", not "clean".

Worth noting #2872 ("Strictly check CRLF when parsing querybuf") is not the cause — a build at that exact commit is 0/5. It changes malformed input from being silently misparsed into being reported, so it may be what makes this visible, but it does not create the malformed bytes.

Ruled out

  • io-threads. Setting io-threads 1 on every node changes nothing — still fails, at both 512 B and 8 KiB values.
  • Replica output buffer limits. client-output-buffer-limit replica is 3367 MB hard and soft; failures occur after roughly 12 MB has accumulated.
  • slot-migration-max-failover-repl-bytes. Raising it from 0 to 256 MB does not help, which is consistent with the failure happening in snapshotting, before the pause phase that setting governs.
  • Snapshot duration. A 2,000-slot migration fails at the same rate as a 200-slot one.
  • Any proxy or gateway. The reproduction talks raw RESP straight to the nodes; there is no proxy, no TLS and no auth in the path.

Where we think it is (unconfirmed)

clusterSlotMigrationShouldInstallWriteHandler() in src/cluster_migrateslots.c deliberately returns false while the job is in SLOT_EXPORT_SNAPSHOTTING, so that concurrent mutations accumulate in the export client's output buffer instead of being flushed:

c
bool clusterSlotMigrationShouldInstallWriteHandler(client *c) {
    slotMigrationJob *job = c->slot_migration_job;
    if (!job || job->type != SLOT_MIGRATION_EXPORT) {
        return true;
    }
    return job->state != SLOT_EXPORT_SNAPSHOTTING;
}

During that window the forked child is writing the snapshot to the same fd. Any flush of the parent's buffer before the child is done would interleave two writers on one socket and produce exactly this symptom — a spliced command, and a target that reports invalid framing.

That guard has a single caller, putClientInPendingWriteQueue() in src/networking.c, and both the guard and that call site are byte-identical between 9.0.0, 51f871a and 9.1.1. So if this is the mechanism, the regression is not the guard itself but some other path that reaches the socket during snapshotting, or a change in when the buffer gets flushed. We checked flushReplicasOutputBuffers(), which does write to replica clients directly, but it requires connHasWriteHandler() || pending_write and the export client has neither while snapshotting — so that path looks safe.

We have not been able to take this further from the outside, and would rather hand over a solid reproduction than a guess.


Related: #4546 — a failed migration can leak its snapshot child and permanently block further migrations on that node.