{"record":{"id":"6d665e28029db9e1","repo":"JuliusBrussee/caveman","slug":"native-session-key-read-w","errorCode":null,"errorMessage":"native session key read: %w","messagePattern":"native session key read: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/nativeruntime/marker.go","lineNumber":58,"sourceCode":"\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}\n\n// SessionMarker builds model-temporary correlation context. Local proxy removes\n// valid markers byte-surgically before provider inspection or forwarding.\nfunc SessionMarker(key []byte, sessionID string) (string, error) {\n\tif len(key) != sessionKeyBytes || sessionID == \"\" || len(sessionID) > 256 {\n\t\treturn \"\", errors.New(\"native session marker: invalid key or session id\")\n\t}\n\tencoded := base64.RawURLEncoding.EncodeToString([]byte(sessionID))\n\tsig := markerMAC(key, encoded)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/proxy/internal/nativeruntime/marker.go#L40-L76","documentation":"The O_EXCL open returned EEXIST (another process won the first-startup race) but the immediate os.ReadFile of the key failed. The winner writes, fsyncs, then closes — and if the winner itself hits a write/sync failure it deletes the file, so the loser's read can hit ENOENT mid-race. Alternatively the file is simply unreadable because it belongs to another uid with mode 0600.","triggerScenarios":"Two caveman processes first-starting simultaneously (CLI adapter plus local proxy) with the winner failing and removing session.key while the loser reads it; session.key owned by a different user (sudo run) so ReadFile gets EACCES.","commonSituations":"Launcher scripts starting several components at once; parallel test suites sharing a temp home; mixed sudo/user uid contexts.","solutions":["Retry LoadOrCreateSessionKey after a short delay — the race window is milliseconds","Serialize first startup: start one component to completion, then the rest","If persistent, check ownership: ls -l <home>/runtime/session.key and chown it to the current user","Remove a foreign or suspect key file and restart to mint a fresh one"],"exampleFix":"// before\nkey, err := nativeruntime.LoadOrCreateSessionKey(home)\nif err != nil {\n\treturn err // transient first-startup race aborts startup\n}\n\n// after\nvar key []byte\nvar err error\nfor attempt := 0; attempt < 3; attempt++ {\n\tkey, err = nativeruntime.LoadOrCreateSessionKey(home)\n\tif err == nil {\n\t\tbreak\n\t}\n\tif !errors.Is(err, fs.ErrNotExist) && !errors.Is(err, fs.ErrPermission) {\n\t\tbreak // not the concurrent-create race; surface it\n\t}\n\ttime.Sleep(100 * time.Millisecond)\n}\nif err != nil {\n\treturn err\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":"func isNotExistErr(err error) bool {\n\treturn errors.Is(err, fs.ErrNotExist)\n}","tryCatchPattern":"var key []byte\nvar err error\nfor attempt := 0; attempt < 3; attempt++ {\n\tkey, err = nativeruntime.LoadOrCreateSessionKey(home)\n\tif err == nil {\n\t\tbreak\n\t}\n\tif !errors.Is(err, fs.ErrNotExist) && !errors.Is(err, fs.ErrPermission) {\n\t\tbreak // not the concurrent first-startup race; surface the error\n\t}\n\ttime.Sleep(100 * time.Millisecond) // winner finishes writing or finishes removing\n}\nif err != nil {\n\treturn err\n}","preventionTips":["Serialize first startup: bring up one component fully, then start the rest — the race only exists on the very first run","Never share one caveman home across different uids; mode 0600 makes the loser's read fail with EACCES","In test suites, create the key once in TestMain before spawning parallel components","Retry with a short backoff: the O_EXCL winner completes or removes the file within milliseconds"],"tags":["go","concurrency","file-race","startup"],"backgroundTag":"concurrent-file-race","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"}