{"record":{"id":"55915921db91b91c","repo":"JuliusBrussee/caveman","slug":"native-session-key-close-w","errorCode":null,"errorMessage":"native session key close: %w","messagePattern":"native session key close: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/nativeruntime/marker.go","lineNumber":49,"sourceCode":"\tpath := filepath.Join(dir, \"session.key\")\n\tkey := make([]byte, sessionKeyBytes)\n\tif _, err := rand.Read(key); err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key random: %w\", err)\n\t}\n\tfile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)\n\tif err == nil {\n\t\tif _, writeErr := file.Write(key); writeErr != nil {\n\t\t\t_ = file.Close()\n\t\t\t_ = os.Remove(path)\n\t\t\treturn nil, fmt.Errorf(\"native session key write: %w\", writeErr)\n\t\t}\n\t\tif syncErr := file.Sync(); syncErr != nil {\n\t\t\t_ = file.Close()\n\t\t\t_ = os.Remove(path)\n\t\t\treturn nil, fmt.Errorf(\"native session key sync: %w\", syncErr)\n\t\t}\n\t\tif closeErr := file.Close(); closeErr != nil {\n\t\t\treturn nil, fmt.Errorf(\"native session key close: %w\", closeErr)\n\t\t}\n\t\treturn key, nil\n\t}\n\tif !errors.Is(err, os.ErrExist) {\n\t\treturn nil, fmt.Errorf(\"native session key create: %w\", err)\n\t}\n\tkey, err = os.ReadFile(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key read: %w\", err)\n\t}\n\tif len(key) != sessionKeyBytes {\n\t\treturn nil, fmt.Errorf(\"native session key length = %d, want %d\", len(key), sessionKeyBytes)\n\t}\n\tif err := os.Chmod(path, 0o600); err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key chmod: %w\", err)\n\t}\n\treturn key, nil\n}","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/proxy/internal/nativeruntime/marker.go#L31-L67","documentation":"file.Close returned an error after both Write and Sync succeeded. Some filesystems — NFS most famously — defer write-back errors to Close, so this usually echoes a late flush failure; however, because Sync already passed, the key bytes are typically durable on disk. Notably this error path does NOT remove session.key, so the very next startup finds a valid 32-byte key via the read branch and succeeds.","triggerScenarios":"An NFS home where the final close surfaces ESTALE or EIO; a rare local filesystem flushing a delayed error exactly at close after a successful fsync.","commonSituations":"Corporate NFS-mounted home directories; flaky network mounts; transient storage hiccups during first startup.","solutions":["Simply re-run — the key file already persisted and the next start reads it","Verify: wc -c <home>/runtime/session.key must print 32","If it recurs, check mount health (dmesg, connectivity to the NFS server)","Move CAVEMAN_HOME to local storage to avoid deferred close errors"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"key, err := nativeruntime.LoadOrCreateSessionKey(home)\nif err != nil && strings.Contains(err.Error(), \"session key close\") {\n\t// Sync already passed: the key is durable and this path intentionally leaves the file in place.\n\t// Recover instead of failing the whole startup.\n\tif k, rerr := os.ReadFile(filepath.Join(home, \"runtime\", \"session.key\")); rerr == nil && len(k) == 32 {\n\t\tkey, err = k, nil\n\t}\n}\nif err != nil {\n\treturn err\n}","preventionTips":["Keep CAVEMAN_HOME off NFS-style mounts that defer write-back errors to close","Know this invariant: the close-error path leaves a valid synced key on disk, so a restart always recovers","Alert on repeated close-stage errors — they usually precede broader storage degradation","Verify recovery with wc -c <home>/runtime/session.key == 32 after any such failure"],"tags":["go","filesystem","nfs","io"],"backgroundTag":"file-close-error","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}