{"record":{"id":"6cab132f5990801f","repo":"kovidgoyal/kitty","slug":"failed-to-open-second-file-for-defrag-of-disk-cach","errorCode":null,"errorMessage":"Failed to open second file for defrag of disk cache","messagePattern":"Failed to open second file for defrag of disk cache","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kitty/disk-cache.c","lineNumber":225,"sourceCode":"    vt_cleanup(&holes->end_pos_map);\n    holes->largest_hole_size = 0;\n}\n\nstatic void\ndefrag(DiskCache *self) {\n    int new_cache_file = -1;\n    RAII_ALLOC(DefragEntry, defrag_entries, NULL);\n    RAII_FreeFastFileCopyBuffer(fcb);\n    bool lock_released = false, ok = false;\n\n    off_t size_on_disk = self->end_of_data_offset;\n    if (size_on_disk <= 0) goto cleanup;\n    size_t num_entries = vt_size(&self->map);\n    if (!num_entries) goto cleanup;\n    bool opened_securely;\n    new_cache_file = open_cache_file(self->cache_dir, &opened_securely);\n    if (new_cache_file < 0) {\n        perror(\"Failed to open second file for defrag of disk cache\");\n        goto cleanup;\n    }\n    defrag_entries = calloc(num_entries, sizeof(DefragEntry));\n    if (!defrag_entries) goto cleanup;\n    size_t total_data_size = 0, num_entries_to_defrag = 0;\n    cache_map_for_loop(i) {\n        CacheValue *s = i.data->val;\n        if (s->pos_in_cache_file > -1 && s->data_sz) {\n            total_data_size += s->data_sz;\n            DefragEntry *e = defrag_entries + num_entries_to_defrag++;\n            e->old_offset = s->pos_in_cache_file;\n            e->data_sz = s->data_sz;\n            e->key = keydup(i.data->key); // have to dup the key as we release the mutex and another thread might free the underlying key.\n            if (!e->key.hash_key) {\n                fprintf(stderr, \"Failed to allocate space for keydup in defrag\\n\");\n                goto cleanup;\n            }\n        }","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kitty/disk-cache.c#L207-L243","documentation":"While defragmenting the scrollback disk cache, kitty failed to open a second cache file in the cache directory. open_cache_file returned -1, so the defrag pass aborts (defrag is best-effort: entries stay in the fragmented file until the next attempt). Typical causes are ENOSPC (no inodes/space), EMFILE (fd limit), or permission problems on the cache dir.","triggerScenarios":"Calling anything that triggers find_cache_entry_to_write (large scrollback being written to the disk cache) after the cache dir is full, read-only, has a stale lock, or the process hit its open-file limit while opening the new cache file for defrag.","commonSituations":"Tiny TMPFS /tmp or full XDG_CACHE_HOME when scrollback_font_size / heavy scrollback fills the disk; home directory mounted read-only (sandbox/flatpak); ulimit -n too low with many kittens.","solutions":["Free space / raise inode availability on the filesystem holding kitty's cache dir (usually $XDG_CACHE_HOME/kitty).","Check `ulimit -n` and raise it if the process is near its fd limit.","Verify permissions on the cache directory and delete stale files in it (they are safe to remove when kitty is closed).","If on a constrained system, disable or reduce the disk cache via kitty.conf scrollback settings (e.g. smaller scrollback_pager_history)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"df -h \"${XDG_CACHE_HOME:-$HOME/.cache}\" && df -i \"${XDG_CACHE_HOME:-$HOME/.cache}\"  # ensure free space+inodes before heavy scrollback sessions","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep free space on the cache filesystem.","Periodically clear kitty's cache dir while kitty is closed.","Don't put XDG_CACHE_HOME on read-only or FUSE mounts."],"tags":["disk-cache","enospc","kitty","defrag"],"backgroundTag":"cache-file-open-failed","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}