Return a fresh context, retaining project contexts by LRU order. ``resolved_path`` is resolved by the caller, making this method the sole owner of file statting and cache-key construction. ``pinned=True`` is reserved for the server's configured default graph; it rem
(self, resolved_path: str, *, pinned: bool = False)
| 175 | return entry |
| 176 | |
| 177 | def load(self, resolved_path: str, *, pinned: bool = False) -> tuple[nx.Graph, dict[int, list[str]]]: |
| 178 | """Return a fresh context, retaining project contexts by LRU order. |
| 179 | |
| 180 | ``resolved_path`` is resolved by the caller, making this method the |
| 181 | sole owner of file statting and cache-key construction. |
| 182 | |
| 183 | ``pinned=True`` is reserved for the server's configured default graph; |
| 184 | it remains warm without consuming a project-cache slot. |
| 185 | |
| 186 | The actual rebuild (``_load_entry``, which parses graph.json and builds |
| 187 | its trigram index) runs outside ``_lock`` so a slow reload of one path |
| 188 | never blocks a request for another (#5177). While a rebuild for THIS |
| 189 | path is already in flight, a caller that still has an old entry for it |
| 190 | gets that stale entry back immediately rather than queuing behind the |
| 191 | rebuild; a caller with no entry yet (the path's first ever load) has |
| 192 | nothing stale to serve and waits for the one in-flight build instead |
| 193 | of starting a redundant second one. |
| 194 | """ |
| 195 | entries = self._pinned if pinned else self._entries |
| 196 | loading_key = (resolved_path, pinned) |
| 197 | with self._lock: |
| 198 | try: |
| 199 | stat_result = Path(resolved_path).stat() |
| 200 | except FileNotFoundError: |
| 201 | raise FileNotFoundError(f"graph.json not found: {resolved_path}") from None |
| 202 | key = (stat_result.st_mtime_ns, stat_result.st_size) |
| 203 | entry = entries.get(resolved_path) |
| 204 | if entry is not None and entry["key"] == key: |
| 205 | if not pinned: |
| 206 | self._entries.move_to_end(resolved_path) |
| 207 | return entry["G"], entry["communities"] |
| 208 | |
| 209 | in_flight = self._loading.get(loading_key) |
| 210 | if entry is not None and in_flight is not None: |
| 211 | if not pinned: |
| 212 | self._entries.move_to_end(resolved_path) |
| 213 | return entry["G"], entry["communities"] |
| 214 | |
| 215 | if in_flight is not None: |
| 216 | wait_event, build_here = in_flight, False |
| 217 | else: |
| 218 | wait_event = threading.Event() |
| 219 | self._loading[loading_key] = wait_event |
| 220 | build_here = True |
| 221 | |
| 222 | if not build_here: |
| 223 | wait_event.wait() |
| 224 | # The build this thread waited on may belong to a key that was |
| 225 | # already superseded by a newer write; recurse to either pick up |
| 226 | # its result or (if it failed, or was already stale on arrival) |
| 227 | # take a fresh run at loading/building it. |
| 228 | return self.load(resolved_path, pinned=pinned) |
| 229 | |
| 230 | try: |
| 231 | new_entry = self._load_entry(resolved_path, key) |
| 232 | except BaseException: |
| 233 | # Build failed: clear the in-flight marker and wake waiters so one |
| 234 | # of them retries. The old entry (if any) is left untouched. |