Repository: redis/redis (2025-08-13T15:59:44Z)

Issue:
[CRASH] Redis 8.0.2 slave keep crashing when it startup.
Notice!
- If a Redis module was involved, please open an issue in the module's repo instead!
- If you're using docker on Apple M1, please make sure the image you're using was compiled for ARM!


**Crash report**

Paste the complete crash log between the quotes below. Please include a few lines from the log preceding the crash report to provide some context.

```

=== REDIS BUG REPORT START: Cut & paste starting from here ===
1027239:S 12 Aug 2025 10:41:05.050 # Redis 8.0.2 crashed by signal: 11, si_code: 128
1027239:S 12 Aug 2025 10:41:05.050 # Accessing address: (nil)
1027239:S 12 Aug 2025 10:41:05.050 # Crashed running the instruction at: 0x466e68

------ STACK TRACE ------
EIP:
/usr/local/bin/redis-server 127.0.0.1:6379(dictSdsDestructor+0x8)[0x466e68]

1027240 bio_close_file
/lib64/libc.so.6(+0x8722a)[0x7fad0048722a]
/lib64/libc.so.6(pthread_cond_wait+0x132)[0x7fad00489782]
/usr/local/bin/redis-server 127.0.0.1:6379(bioProcessBackgroundJobs+0x1e1)[0x53a0f1]
/lib64/libc.so.6(+0x8a19a)[0x7fad0048a19a]
/lib64/libc.so.6(+0x10f210)[0x7fad0050f210]

1027243 io_thd_1
/lib64/libc.so.6(epoll_wait+0x5e)[0x7fad0050e84e]
/usr/local/bin/redis-server 127.0.0.1:6379[0x4631c7]
/usr/local/bin/redis-server 127.0.0.1:6379(aeMain+0xa4)[0x463864]
/usr/local/bin/redis-server 127.0.0.1:6379(IOThreadMain+0x67)[0x46c067]
/lib64/libc.so.6(+0x8a19a)[0x7fad0048a19a]
/lib64/libc.so.6(+0x10f210)[0x7fad0050f210]

1027244 io_thd_2
/lib64/libc.so.6(epoll_wait+0x5e)[0x7fad0050e84e]
/usr/local/bin/redis-server 127.0.0.1:6379[0x4631c7]
/usr/local/bin/redis-server 127.0.0.1:6379(aeMain+0xa4)[0x463864]
/usr/local/bin/redis-server 127.0.0.1:6379(IOThreadMain+0x67)[0x46c067]
/lib64/libc.so.6(+0x8a19a)[0x7fad0048a19a]
/lib64/libc.so.6(+0x10f210)[0x7fad0050f210]

1027241 bio_aof
/lib64/libc.so.6(+0x8722a)[0x7fad0048722a]
/lib64/libc.so.6(pthread_cond_wait+0x132)[0x7fad00489782]
/usr/local/bin/redis-server 127.0.0.1:6379(bioProcessBackgroundJobs+0x1e1)[0x53a0f1]
/lib64/libc.so.6(+0x8a19a)[0x7fad0048a19a]
/lib64/libc.so.6(+0x10f210)[0x7fad0050f210]

1027242 bio_lazy_free
/lib64/libc.so.6(+0x8722a)[0x7fad0048722a]
/lib64/libc.so.6(pthread_cond_wait+0x132)[0x7fad00489782]
/usr/local/bin/redis-server 127.0.0.1:6379(bioProcessBackgroundJobs+0x1e1)[0x53a0f1]
/lib64/libc.so.6(+0x8a19a)[0x7fad0048a19a]
/lib64/libc.so.6(+0x10f210)[0x7fad0050f210]

1027239 redis-server *
/lib64/libc.so.6(+0x3ebf0)[0x7fad0043ebf0]
/usr/local/bin/redis-server 127.0.0.1:6379(dictSdsDestructor+0x8)[0x466e68]
/usr/local/bin/redis-server 127.0.0.1:6379[0x5dc6fc]
/usr/local/bin/redis-server 127.0.0.1:6379(kvstoreEmpty+0xe9)[0x468aa9]
/usr/local/bin/redis-server 127.0.0.1:6379(emptyDbStructure+0x6d)[0x4ac46d]
/usr/local/bin/redis-server 127.0.0.1:6379[0x5def06]
/usr/local/bin/redis-server 127.0.0.1:6379(rdbLoadWithEmptyFunc+0xd3)[0x4cb203]
/usr/local/bin/redis-server 127.0.0.1:6379(readSyncBulkPayload+0xa8d)[0x4bc04d]
/usr/local/bin/redis-server 127.0.0.1:6379[0x5af4b1]
/usr/local/bin/redis-server 127.0.0.1:6379(aeMain+0xea)[0x4638aa]
/usr/local/bin/redis-server 127.0.0.1:6379(main+0x43f)[0x457b0f]
/lib64/libc.so.6(+0x295d0)[0x7fad004295d0]
/lib64/libc.so.6(__libc_start_main+0x80)[0x7fad00429680]
/usr/local/bin/redis-server 127.0.0.1:6379(_start+0x25)[0x4591d5]

6/6 expected stacktraces.

------ STACK TRACE DONE ------

------ REGISTERS ------
1027239:S 12 Aug 2025 10:41:05.053 #
RAX:00007facca831488 RBX:00007facca831488
RCX:0000000000466e60 RDX:00000000a7e532a0
RDI:5f726f7461726570 RSI:5f726f7461726570
RBP:00007ffdd12d1e00 RSP:00007ffdd12d1db8
R8 :00007fad00000f70 R9 :00007fad00bf3780
R10:00007fad00000f78 R11:0000000000000002
R12:8f4c00f40569736d R13:0000000000000000
R14:000000000054d541 R15:00007facca4d8000
RIP:0000000000466e68 EFL:0000000000010202
CSGSFS:002b000000000033
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc7) -> 000000000000003f
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc6) -> 000000000093d642
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc5) -> 0000000000000004
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc4) -> 00007fad000c6150
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc3) -> 00007fad0000c0f8
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc2) -> 0000000000468aa9
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc1) -> 00007ffdd12d1e40
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dc0) -> 0000000000000000
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dbf) -> 0000000000000000
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dbe) -> 00000000004b9150
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dbd) -> 00007fad00113400
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dbc) -> 00007facca4d8000
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dbb) -> 00000000000001b6
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1dba) -> 00000000004b9150
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1db9) -> 3832313138323730
1027239:S 12 Aug 2025 10:41:05.053 # (00007ffdd12d1db8) -> 00000000005dc6fc


------ CONFIG DEBUG OUTPUT ------
io-threads 3
repl-diskless-load disabled
repl-diskless-sync no
replica-read-only yes
lazyfree-lazy-expire no
slave-read-only yes
proto-max-bulk-len 512mb
list-compress-depth 0
lazyfree-lazy-eviction no
lazyfree-lazy-user-del no
activedefrag yes
lazyfree-lazy-server-del no
sanitize-dump-payload no
client-query-buffer-limit 512mb
lazyfree-lazy-user-flush no

------ FAST MEMORY TEST ------
1027239:S 12 Aug 2025 10:41:05.053 # Bio worker thread #0 terminated
1027239:S 12 Aug 2025 10:41:05.053 # Bio worker thread #1 terminated
1027239:S 12 Aug 2025 10:41:05.053 # Bio worker thread #2 terminated
1027239:S 12 Aug 2025 10:41:05.053 # IO thread(tid:140380968306240) terminated
1027239:S 12 Aug 2025 10:41:05.054 # IO thread(tid:140380959913536) terminated
*** Preparing to test memory region 792000 (487424 bytes)
*** Preparing to test memory region 1a34000 (135168 bytes)
*** Preparing to test memory region 7fabfa000000 (8388608 bytes)
*** Preparing to test memory region 7fabfa800000 (4185915392 bytes)
*** Preparing to test memory region 7facf4000000 (135168 bytes)
*** Preparing to test memory region 7facf82ff000 (22020096 bytes)
*** Preparing to test memory region 7facf9800000 (8388608 bytes)
*** Preparing to test memory region 7facfa000000 (8388608 bytes)
*** Preparing to test memory region 7facfa9f9000 (18874368 bytes)
*** Preparing to test memory region 7facfbbfa000 (8388608 bytes)
*** Preparing to test memory region 7facfc3fb000 (8388608 bytes)
*** Preparing to test memory region 7facfcbfc000 (8388608 bytes)
*** Preparing to test memory region 7facfd3fd000 (8388608 bytes)
*** Preparing to test memory region 7facfdbfe000 (8388608 bytes)
*** Preparing to test memory region 7facfe3ff000 (8388608 bytes)
*** Preparing to test memory region 7facff400000 (8388608 bytes)
*** Preparing to test memory region 7fad00000000 (4194304 bytes)
*** Preparing to test memory region 7fad005fb000 (53248 bytes)
*** Preparing to test memory region 7fad00a26000 (12288 bytes)
*** Preparing to test memory region 7fad00bf1000 (28672 bytes)
*** Preparing to test memory region 7fad00cf8000 (8192 bytes)
.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O
Fast memory test PASSED, however your memory can still be broken. Please run a memory test for several hours if possible.

------ DUMPING CODE AROUND EIP ------
Symbol: dictSdsDestructor (base: 0x466e60)
Module: /usr/local/bin/redis-server 127.0.0.1:6379 (base 0x400000)
$ xxd -r -p /tmp/dump.hex /tmp/dump.bin
$ objdump --adjust-vma=0x466e60 -D -b binary -m i386:x86-64 /tmp/dump.bin
------
1027239:S 12 Aug 2025 10:41:13.295 # dump of function (hexdump of 136 bytes):
4889f74885f674300fb646ff83e0073c047e0de958b601000f1f8400000000000fb6c048630485c05e69004829c7e93db601000f1f440000c30f1f800000000049b873657479626465744889f8488b3d24a7320048b96172656e6567796c48be6d6f646e61726f6448330d01a7320048333502a7320048ba75657370656d6f734831c7483315e6a6

=== REDIS BUG REPORT END. Make sure to include from START to END. ===

```

**Additional information**

OS distribution and version: Rocky Linux release 9.6 (Blue Onyx)
Kernel: Linux 5.14.0-570.22.1.el9_6.x86_64

This is the slave bug report, master didn't crash. 
When I found it is crash, I try to restart it but it crash at the same place. 
Below is the last log before it crash. 

```
1027239:S 12 Aug 2025 10:40:39.502 * Full resync from master: 403f5eadaf40b46fa5a90ccde54b462606b52604:15123965342078
1027239:S 12 Aug 2025 10:40:51.718 * MASTER <-> REPLICA sync: receiving 1190754337 bytes from master to disk
1027239:S 12 Aug 2025 10:41:01.847 * MASTER <-> REPLICA sync: Loading DB in memory
1027239:S 12 Aug 2025 10:41:01.848 * MASTER <-> REPLICA sync: Flushing old data
```




Discussion:
@onghongyao thx, can you backup this RDB file of the replica.
it seems that this RDB was corrupted, please try to delete this RDB file(after backup) and restart it again.
if it no longer crashes, then it's possible that the RDB was corrupted.
It shouldn't be RDB file corrupted. 
Based on chatgpt suggestion, I try to change the configuration from 
io-threads 3       activedefrag yes
to
io-threads 1        activedefrag no
& manage to start redis slave. 

@onghongyao Must both of these config be modified simultaneously? If only `io-threads 1` or `activedefrag no` is modified, will it still crash?
I didn't try it 1 by 1, so no idea on this. 
I modify both & manage to start redis back to normal.

@onghongyao, I'll try to reproduce it. Please let me know if there's any news. thx.
@onghongyao can you give the output of `INFO ALL`? 
Which data types did you use?
```

------ INFO OUTPUT ------
# Server
redis_version:8.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:71612ef3aa33f91a
redis_mode:standalone
os:Linux 5.14.0-570.22.1.el9_6.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:11.5.0
process_id:1027239
process_supervised:no
run_id:ba499fc8377625a29f1e199e4aa5f2fa0f99d6ac
tcp_port:6379
server_time_usec:1754966465032700
uptime_in_seconds:46
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:10135997
executable:/usr/local/bin/redis-server
config_file:/etc/redis/redis_6379.conf
io_threads_active:1
listener0:name=tcp,bind=127.0.0.1,bind=192.168.4.18,port=6379

# Clients
connected_clients:6
cluster_connections:0
maxclients:2000
client_recent_max_input_buffer:20504
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
pubsub_clients:3
watching_clients:0
clients_in_timeout_table:0
total_watched_keys:0
total_blocking_keys:0
total_blocking_keys_on_nokey:0

# Memory
used_memory:2167372880
used_memory_human:2.02G
used_memory_rss:3286724608
used_memory_rss_human:3.06G
used_memory_peak:3558648104
used_memory_peak_human:3.31G
used_memory_peak_perc:60.90%
used_memory_overhead:424409752
used_memory_startup:873080
used_memory_dataset:1742963128
used_memory_dataset_perc:80.45%
allocator_allocated:2199137344
allocator_active:2865954816
allocator_resident:3401740288
allocator_muzzy:0
total_system_memory:404177051648
total_system_memory_human:376.42G
used_memory_lua:31744
used_memory_vm_eval:31744
used_memory_lua_human:31.00K
used_memory_scripts_eval:0
number_of_cached_scripts:0
number_of_functions:0
number_of_libraries:0
used_memory_vm_functions:32768
used_memory_vm_total:64512
used_memory_vm_total_human:63.00K
used_memory_functions:192
used_memory_scripts:192
used_memory_scripts_human:192B
maxmemory:53687091200
maxmemory_human:50.00G
maxmemory_policy:noeviction
allocator_frag_ratio:1.30
allocator_frag_bytes:666741440
allocator_rss_ratio:1.19
allocator_rss_bytes:535785472
rss_overhead_ratio:0.97
rss_overhead_bytes:-115015680
mem_fragmentation_ratio:1.50
mem_fragmentation_bytes:1088888448
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_total_replication_buffers:0
mem_replica_full_sync_buffer:0
mem_clients_slaves:0
mem_clients_normal:184792
mem_cluster_links:0
mem_aof_buffer:0
mem_allocator:jemalloc-5.3.0
mem_overhead_db_hashtable_rehashing:0
active_defrag_running:5
lazyfree_pending_objects:0
lazyfreed_objects:0

# Persistence
loading:1
async_loading:0
current_cow_peak:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:961527
rdb_bgsave_in_progress:0
rdb_last_save_time:1754966419
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_saves:0
rdb_last_cow_size:0
rdb_last_load_keys_expired:0
rdb_last_load_keys_loaded:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_rewrites:0
aof_rewrites_consecutive_failures:0
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
aof_current_size:1491535278
aof_base_size:1408865402
aof_pending_rewrite:0
aof_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
loading_start_time:1754966461
loading_total_bytes:1190754337
loading_rdb_used_mem:0
loading_loaded_bytes:0
loading_loaded_perc:0.00
loading_eta_seconds:4763017348

# Threads
io_thread_0:clients=3,reads=0,writes=198
io_thread_1:clients=2,reads=132,writes=130
io_thread_2:clients=1,reads=91,writes=82

# Stats
total_connections_received:15
total_commands_processed:522
instantaneous_ops_per_sec:5
total_net_input_bytes:1190768857
total_net_output_bytes:1259813
total_net_repl_input_bytes:1190754362
total_net_repl_output_bytes:0
instantaneous_input_kbps:114823.04
instantaneous_output_kbps:14.50
instantaneous_input_repl_kbps:114822.73
instantaneous_output_repl_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_subkeys:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:0
evicted_keys:0
evicted_clients:0
evicted_scripts:0
total_eviction_exceeded_time:0
current_eviction_exceeded_time:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
pubsubshard_channels:0
latest_fork_usec:0
total_forks:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:44900
active_defrag_misses:4750
active_defrag_key_hits:13567
active_defrag_key_misses:927
total_active_defrag_time:497
current_active_defrag_time:497
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:67
dump_payload_sanitizations:0
total_reads_processed:223
total_writes_processed:410
io_threaded_reads_processed:223
io_threaded_writes_processed:212
client_query_buffer_limit_disconnections:0
client_output_buffer_limit_disconnections:0
reply_buffer_shrinks:16
reply_buffer_expands:33
eventloop_cycles:164073
eventloop_duration_sum:888817
eventloop_duration_cmd_sum:10840
instantaneous_eventloop_cycles_per_sec:16182
instantaneous_eventloop_duration_usec:4
acl_access_denied_auth:0
acl_access_denied_cmd:0
acl_access_denied_key:0
acl_access_denied_channel:0

# Replication
role:slave
master_host:192.168.4.17
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:1
slave_read_repl_offset:1
slave_repl_offset:1
replica_full_sync_buffer_size:0
replica_full_sync_buffer_peak:0
master_sync_total_bytes:1190754337
master_sync_read_bytes:1190754337
master_sync_left_bytes:0
master_sync_perc:100.00
master_sync_last_io_seconds_ago:4
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:71f5656085764aa1798b6410b9ff19f5f9b384ca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15120902591340
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:2.370496
used_cpu_user:21.456376
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:2.217104
used_cpu_user_main_thread:21.391097

# Modules
module:name=vectorset,ver=1,api=1,filters=0,usedby=[],using=[],options=[handle-io-errors|handle-repl-async-load]

# Commandstats
cmdstat_auth:calls=15,usec=52,usec_per_call=3.47,rejected_calls=0,failed_calls=0
cmdstat_ping:calls=63,usec=15,usec_per_call=0.24,rejected_calls=67,failed_calls=0
cmdstat_subscribe:calls=3,usec=9,usec_per_call=3.00,rejected_calls=0,failed_calls=0
cmdstat_command|docs:calls=1,usec=618,usec_per_call=618.00,rejected_calls=0,failed_calls=0
cmdstat_publish:calls=67,usec=315,usec_per_call=4.70,rejected_calls=0,failed_calls=0
cmdstat_info:calls=137,usec=9680,usec_per_call=70.66,rejected_calls=0,failed_calls=0
cmdstat_client|list:calls=4,usec=146,usec_per_call=36.50,rejected_calls=0,failed_calls=0
cmdstat_client|setname:calls=6,usec=5,usec_per_call=0.83,rejected_calls=0,failed_calls=0

# Errorstats
errorstat_LOADING:count=67

# Latencystats
latency_percentiles_usec_auth:p50=4.015,p99=5.023,p99.9=5.023
latency_percentiles_usec_ping:p50=0.001,p99=1.003,p99.9=1.003
latency_percentiles_usec_subscribe:p50=3.007,p99=4.015,p99.9=4.015
latency_percentiles_usec_command|docs:p50=618.495,p99=618.495,p99.9=618.495
latency_percentiles_usec_publish:p50=5.023,p99=9.023,p99.9=11.007
latency_percentiles_usec_info:p50=74.239,p99=108.031,p99.9=109.055
latency_percentiles_usec_client|list:p50=31.103,p99=46.079,p99.9=46.079
latency_percentiles_usec_client|setname:p50=1.003,p99=2.007,p99.9=2.007

# Cluster
cluster_enabled:0

# Keyspace
db3:keys=3194762,expires=6703402,avg_ttl=0,subexpiry=0
db4:keys=1290,expires=0,avg_ttl=0,subexpiry=0
db5:keys=4,expires=0,avg_ttl=0,subexpiry=0
db6:keys=5306,expires=5,avg_ttl=0,subexpiry=0
db8:keys=281,expires=281,avg_ttl=0,subexpiry=0
db9:keys=23,expires=1,avg_ttl=0,subexpiry=0

# Keysizes
db3_distrib_hashes_items:4=6703402
db4_distrib_hashes_items:2=2,4=1288
db5_distrib_hashes_items:16=4
db6_distrib_strings_sizes:1=4738,2=568
db8_distrib_hashes_items:2=281
db9_distrib_strings_sizes:1=5,8=2,16M=2,32M=6,64M=4,128M=4

```
@onghongyao can you try this fix: https://github.com/redis/redis/pull/14274, thx.



Patch:
diff --git a/src/replication.c b/src/replication.c
index 486d94a5dc1..767b3473d2b 100644
--- a/src/replication.c
+++ b/src/replication.c
@@ -1949,7 +1949,17 @@ static void rdbLoadEmptyDbFunc(void) {
     serverLog(LL_NOTICE, "MASTER <-> REPLICA sync: Flushing old data");
     int empty_db_flags = server.repl_slave_lazy_flush ? EMPTYDB_ASYNC :
                                                         EMPTYDB_NO_FLAGS;
+
+    /* Temporarily disable active defragmentation during database flush.
+     * This prevents defrag from being triggered in replicationEmptyDbCallback()
+     * which could modify the database while it's being emptied. */
+    int orig_active_defrag = server.active_defrag_enabled;
+    server.active_defrag_enabled = 0;
+
     emptyData(-1, empty_db_flags, replicationEmptyDbCallback);
+
+    /* Restore the original active defragmentation. */
+    server.active_defrag_enabled = orig_active_defrag;
 }
 
 /* Once we have a link with the master and the synchronization was


Test patch:
diff --git a/tests/unit/memefficiency.tcl b/tests/unit/memefficiency.tcl
index 68c44c6b484..062bff09233 100644
--- a/tests/unit/memefficiency.tcl
+++ b/tests/unit/memefficiency.tcl
@@ -67,6 +67,14 @@ run_solo {defrag} {
         }
     }
 
+    proc discard_replies_every {rd count frequency discard_num} {
+        if {$count % $frequency == 0} {
+            for {set k 0} {$k < $discard_num} {incr k} {
+                $rd read ; # Discard replies
+            }
+        }
+    }
+
     proc test_active_defrag {type} {
     if {[string match {*jemalloc*} [s mem_allocator]] && [r debug mallctl arenas.page] <= 8192} {
         test "Active defrag main dictionary: $type" {
@@ -339,11 +347,7 @@ run_solo {defrag} {
                     $rd hset bighash $j [concat "asdfasdfasdf" $j]
 
                     incr count
-                    if {$count % 10000 == 0} {
-                        for {set k 0} {$k < 10000} {incr k} {
-                            $rd read ; # Discard replies
-                        }
-                    }
+                    discard_replies_every $rd $count 10000 10000
                 }
                 # creating that big hash, increased used_memory, so the relative frag goes down
                 set expected_frag 1.3
@@ -355,11 +359,7 @@ run_solo {defrag} {
                 $rd setrange $j 150 a
 
                 incr count
-                if {$count % 10000 == 0} {
-                    for {set k 0} {$k < 10000} {incr k} {
-                        $rd read ; # Discard replies
-                    }
-                }
+                discard_replies_every $rd $count 10000 10000
             }
             assert_equal [r dbsize] 500016
 
@@ -369,11 +369,7 @@ run_solo {defrag} {
                 $rd del $j
 
                 incr count
-                if {$count % 10000 == 0} {
-                    for {set k 0} {$k < 10000} {incr k} {
-                        $rd read ; # Discard replies
-                    }
-                }
+                discard_replies_every $rd $count 10000 10000
             }
             assert_equal [r dbsize] 250016
 
@@ -769,12 +765,7 @@ run_solo {defrag} {
                 $rd lpush biglist2 $val
 
                 incr count
-                if {$count % 10000 == 0} {
-                    for {set k 0} {$k < 10000} {incr k} {
-                        $rd read ; # Discard replies
-                        $rd read ; # Discard replies
-                    }
-                }
+                discard_replies_every $rd $count 10000 20000
             }
 
             # create some fragmentation
@@ -884,11 +875,7 @@ run_solo {defrag} {
                     $rd setrange $j 600 x
 
                     incr count
-                    if {$count % 10000 == 0} {
-                        for {set k 0} {$k < 10000} {incr k} {
-                            $rd read ; # Discard replies
-                        }
-                    }
+                    discard_replies_every $rd $count 10000 10000
                 }
 
                 # create some fragmentation of 50%
@@ -898,11 +885,7 @@ run_solo {defrag} {
                     incr sent
                     incr j 1
 
-                    if {$sent % 10000 == 0} {
-                        for {set k 0} {$k < 10000} {incr k} {
-                            $rd read ; # Discard replies
-                        }
-                    }
+                    discard_replies_every $rd $sent 10000 10000
                 }
 
                 # create higher fragmentation in the first slab
@@ -959,6 +942,70 @@ run_solo {defrag} {
     }
     }
 
+    test "Active defrag can't be triggered during replicaof database flush. See issue #14267" {
+        start_server {tags {"repl"} overrides {save ""}} {
+            set master_host [srv 0 host]
+            set master_port [srv 0 port]
+
+            start_server {overrides {save ""}} {
+                set replica [srv 0 client]
+                set rd [redis_deferring_client 0]
+
+                $replica config set hz 100
+                $replica config set activedefrag no
+                $replica config set active-defrag-threshold-lower 5
+                $replica config set active-defrag-cycle-min 65
+                $replica config set active-defrag-cycle-max 75
+                $replica config set active-defrag-ignore-bytes 2mb
+
+                # add a mass of string keys
+                set count 0
+                for {set j 0} {$j < 500000} {incr j} {
+                    $rd setrange $j 150 a
+
+                    incr count
+                    discard_replies_every $rd $count 10000 10000
+                }
+                assert_equal [$replica dbsize] 500000
+
+                # create some fragmentation
+                set count 0
+                for {set j 0} {$j < 500000} {incr j 2} {
+                    $rd del $j
+
+                    incr count
+                    discard_replies_every $rd $count 10000 10000
+                }
+                $rd close
+                assert_equal [$replica dbsize] 250000
+
+                catch {$replica config set activedefrag yes} e
+                if {[$replica config get activedefrag] eq "activedefrag yes"} {
+                    # Start replication sync which will flush the replica's database,
+                    # then enable defrag to run concurrently with the database flush.
+                    $replica replicaof $master_host $master_port
+
+                    # wait for the active defrag to start working (decision once a second)
+                    wait_for_condition 50 100 {
+                        [s total_active_defrag_time] ne 0
+                    } else {
+                        after 120 ;# serverCron only updates the info once in 100ms
+                        puts [$replica info memory]
+                        puts [$replica info stats]
+                        puts [$replica memory malloc-stats]
+                        fail "defrag not started."
+                    }
+
+                    wait_for_sync $replica
+
+                    # wait for the active defrag to stop working (db has been emptied during replication sync)
+                    wait_for_defrag_stop 500 100
+                    assert_equal [$replica dbsize] 0
+                }
+            }
+        }
+    } {} {defrag external:skip tsan:skip cluster}
+
     start_cluster 1 0 {tags {"defrag external:skip tsan:skip cluster"} overrides {appendonly yes auto-aof-rewrite-percentage 0 save "" loglevel notice}} {
         test_active_defrag "cluster"
     }