kovidgoyal/kitty · warning

Failed to copy data to new disk cache file during defrag

Error message

Failed to copy data to new disk cache file during defrag

What it means

While copying scrollback cache entries from the old cache file to the new one during defrag, copy_between_files failed (sendfile/copy_file_range or read/write error). The defrag is abandoned mid-copy; existing entries in the old file remain valid and the partially built new file is discarded.

Source

Thrown at kitty/disk-cache.c:258

                fprintf(stderr, "Failed to allocate space for keydup in defrag\n");
                goto cleanup;
            }
        }
    }
    if (ftruncate(new_cache_file, total_data_size) != 0) {
        perror("Failed to allocate space for new disk cache file during defrag");
        goto cleanup;
    }
    lseek(new_cache_file, 0, SEEK_SET);

    mutex(unlock);
    lock_released = true;

    off_t current_pos = 0;
    for (size_t i = 0; i < num_entries_to_defrag; i++) {
        DefragEntry *e = defrag_entries + i;
        if (!copy_between_files(self->cache_file_fd, new_cache_file, e->old_offset, e->data_sz, &fcb)) {
            perror("Failed to copy data to new disk cache file during defrag");
            goto cleanup;
        }
        e->new_offset = current_pos;
        current_pos += e->data_sz;
    }
    ok = true;

cleanup:
    if (lock_released) mutex(lock);
    if (ok) {
        cleanup_holes(&self->holes);
        safe_close(self->cache_file_fd, __FILE__, __LINE__);
        self->cache_file_fd = new_cache_file;
        new_cache_file = -1;
        for (size_t i = 0; i < num_entries_to_defrag; i++) {
            DefragEntry *e = defrag_entries + i;
            cache_map_itr i = vt_get(&self->map, e->key);
            if (!vt_is_end(i)) i.data->val->pos_in_cache_file = e->new_offset;

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Quit kitty and delete the disk-cache directory so it is rebuilt from scratch.
  2. Check disk health and free space (df -i and df -h) on the cache filesystem.
  3. Reproduce and report with kitty --debug if it persists across a rebuilt cache.
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: defrag() after the ftruncate succeeded, when reading the source fd at old_offset fails (EIO, corrupted/truncated cache file after a crash) or writing to the new file fails (ENOSPC hit mid-copy).

Common situations: Power loss / kitty crash left a truncated cache file; disk filled between allocation and copy; failing disk returning EIO; cache on a filesystem where the copy syscall used is unsupported and falls back badly.

Related errors


AI-assisted analysis of kovidgoyal/kitty@6d5d0c4406 (2026-08-27). Data as JSON: /api/errors/4e974ae3f17fc2c6. Report an issue: GitHub.