{"record":{"id":"9bee38dae5f97f46","repo":"weaviate/weaviate","slug":"close-segment-munmap-w-close-contents-file-w","errorCode":null,"errorMessage":"close segment: munmap: %w, close contents file: %w","messagePattern":"close segment: munmap: %w, close contents file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"adapters/repos/db/lsmkv/segment.go","lineNumber":480,"sourceCode":"}\n\nfunc (s *segment) close() error {\n\tvar munmapErr, fileCloseErr error\n\tif s.unMapContents {\n\t\tm := mmap.MMap(s.contents)\n\t\tmunmapErr = m.Unmap()\n\t\tstratLabel := s.strategy.String()\n\t\tmonitoring.GetMetrics().MmapOperations.With(prometheus.Labels{\n\t\t\t\"operation\": \"munmap\",\n\t\t\t\"strategy\":  stratLabel,\n\t\t}).Inc()\n\t}\n\tif s.contentFile != nil {\n\t\tfileCloseErr = s.contentFile.Close()\n\t}\n\n\tif munmapErr != nil || fileCloseErr != nil {\n\t\treturn fmt.Errorf(\"close segment: munmap: %w, close contents file: %w\", munmapErr, fileCloseErr)\n\t}\n\n\treturn nil\n}\n\n// sidecarPaths returns the paths of the files derived from the segment: bloom\n// filters, count net additions and metadata.\nfunc (s *segment) sidecarPaths() []string {\n\tpaths := make([]string, 0, 3+int(s.secondaryIndexCount))\n\tpaths = append(paths, s.bloomFilterPath())\n\tfor i := 0; i < int(s.secondaryIndexCount); i++ {\n\t\tpaths = append(paths, s.bloomFilterSecondaryPath(i))\n\t}\n\treturn append(paths, s.countNetPath(), s.metadataPath())\n}\n\nfunc (s *segment) dropImmediately() error {\n\t// support for persisting bloom filters and cnas was added in v1.17,","sourceCodeStart":462,"sourceCodeEnd":498,"githubUrl":"https://github.com/weaviate/weaviate/blob/75aa4b6d11f8818305aafd4440b4e32794f7ca04/adapters/repos/db/lsmkv/segment.go#L462-L498","documentation":"Combined error from segment.close(): unmap of the mmapped contents and/or closing the underlying segment file both failed. Both errors are reported together; either one indicates resource cleanup trouble, often during shutdown or compaction/drop.","triggerScenarios":"segment.close() when m.Unmap() fails (e.g. contents already unmapped or invalid mapping) or s.contentFile.Close() fails (e.g. file descriptor issues, I/O error on close).","commonSituations":"Double-close after a prior shutdown path, memory pressure / mmap errors, NFS or unusual filesystems where unmap/close can fail, shutdown during active I/O.","solutions":["Restart Weaviate to release mappings and file descriptors","Check logs for the specific underlying munmap vs close error","Avoid running the data directory on NFS; use local block storage","If persistent, check kernel memory (vm.max_map_count, memory limits) and host I/O health"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before close, ensure the segment wasn't already closed/cleaned\nif s == nil || s.contentFile == nil && !s.unMapContents {\n    return nil // nothing to close\n}","typeGuard":"func isDoubleClose(err error) bool {\n    return strings.Contains(err.Error(), \"munmap\") && strings.Contains(err.Error(), \"invalid\")\n}","tryCatchPattern":"if err := seg.close(); err != nil {\n    var pe *os.PathError\n    if strings.Contains(err.Error(), \"close segment: munmap\") || errors.As(err, &pe) {\n        logger.Warnf(\"non-fatal segment close issue: %v\", err)\n    }\n}","preventionTips":["Avoid closing a segment twice (track lifecycle state)","Don't run data on NFS or exotic filesystems","Raise memory limits if munmap failures correlate with memory pressure","Shut down compaction activity before stopping the node"],"tags":["storage","lsmkv","mmap","shutdown"],"backgroundTag":"munmap-failed","analyzedSha":"75aa4b6d11f8818305aafd4440b4e32794f7ca04","analyzedAt":"2026-09-04T14:58:20.392Z","contentChangedAt":"2026-09-04T14:58:20.392Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}