{"record":{"id":"b6d06e4fc36b18b6","repo":"mudler/LocalAI","slug":"ds4-kvcache-save-ds4-session-save-payload-rc-d","errorCode":null,"errorMessage":"ds4 KvCache::Save: ds4_session_save_payload rc=%d err=%s; removing %s\n","messagePattern":"ds4 KvCache::Save: ds4_session_save_payload rc=(.+?) err=(.+?); removing (.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/cpp/ds4/kv_cache.cpp","lineNumber":197,"sourceCode":"    if (!fp) {\n        std::fprintf(stderr, \"ds4 KvCache::Save: fopen failed: %s\\n\", std::strerror(errno));\n        return;\n    }\n    char magic[4] = {'D','S','4','G'};\n    uint32_t version = 1;\n    uint32_t ctx = static_cast<uint32_t>(ctx_size);\n    uint32_t prefix_len = static_cast<uint32_t>(rendered_text.size());\n    std::fwrite(magic, 4, 1, fp);\n    std::fwrite(&version, 4, 1, fp);\n    std::fwrite(&ctx, 4, 1, fp);\n    std::fwrite(&prefix_len, 4, 1, fp);\n    std::fwrite(rendered_text.data(), prefix_len, 1, fp);\n    std::fwrite(&payload_bytes, 8, 1, fp);\n    char errbuf[256] = {0};\n    int rc = ds4_session_save_payload(session, fp, errbuf, sizeof(errbuf));\n    std::fclose(fp);\n    if (rc != 0) {\n        std::fprintf(stderr, \"ds4 KvCache::Save: ds4_session_save_payload rc=%d err=%s; removing %s\\n\",\n                     rc, errbuf, path.c_str());\n        std::remove(path.c_str());\n    } else {\n        std::fprintf(stderr, \"ds4 KvCache::Save: wrote %s ok\\n\", path.c_str());\n    }\n}\n\n} // namespace ds4cpp\n","sourceCodeStart":179,"sourceCodeEnd":206,"githubUrl":"https://github.com/mudler/LocalAI/blob/44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26/backend/cpp/ds4/kv_cache.cpp#L179-L206","documentation":"ds4 KV-cache save got past fopen but ds4_session_save_payload() returned non-zero (with its error string in errbuf); the header bytes were already written to the file, so the code fclose()s it and std::remove()s the partial file to prevent a corrupt cache from being loaded later. The final 'wrote ... ok' log is skipped. Non-fatal to generation; the session simply is not cached.","triggerScenarios":"Serialization of the session payload fails mid-write: disk filling up during the write (payload_bytes was measured upfront), an internal DSML/session state error surfaced through errbuf, or the session object being in a state that cannot be serialized after an interrupted generation.","commonSituations":"Large contexts producing multi-GB payloads hitting ENOSPC mid-write; concurrent saves of the same session; backend bugs in the DSML state machine leaving the session partially updated; ephemeral storage limits in Kubernetes emptyDir.","solutions":["Read err in the log line — it is the library's own reason (e.g. write error, bad state) and points at the failing stage.","Free disk space or move the cache directory to a volume with room for payload_bytes (printed earlier in the 'path=... payload_bytes=...' line) plus margin.","If it recurs on the same prompt, report a ds4 backend issue with the err string — deterministic serialization failures are backend bugs, not config.","Treat as performance-only degradation meanwhile: requests still work, only KV prefix reuse is lost."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// C: check free space against the printed payload_bytes before saving\nunsigned long long avail = fs_available_bytes(cache_dir);\nif (avail < payload_bytes + (payload_bytes >> 3)) {\n    fprintf(stderr, \"ds4 KvCache::Save: skipping, low disk (%llu < %llu)\\n\", avail, payload_bytes);\n    return; // avoid partial write + remove churn\n}","typeGuard":null,"tryCatchPattern":"// callers treat a missing cache file as a soft miss, never an error\nif (!std::filesystem::exists(cache_path)) {\n    // cold path: proceed without prefix reuse\n}","preventionTips":["Provision cache storage at least max-ctx payload size plus 10% headroom.","Log and alert on repeated 'ds4_session_save_payload rc=' lines with the same err string — deterministic failures are backend bugs worth reporting.","Because the code removes partial files, never assume a file's presence implies a good save; check for the 'wrote ... ok' line in logs."],"tags":["cpp","ds4","kv-cache","serialization","disk-space"],"backgroundTag":null,"analyzedSha":"44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26","analyzedAt":"2026-08-15T10:13:50.291Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}