Skip to content

Commit b4aee82

Browse files
rboarescuclaude
andcommitted
chore: sync patch to v1.5.1 upstream line offsets
Update mcp_server_get_collection.patch to match the current mempalace 3.3.3 source — hunk offset moved from line 212 to 271 after upstream refactored _get_collection to use _pin_hnsw_threads(). Patch content and behaviour are unchanged; only the diff context lines are updated. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent d0aabb9 commit b4aee82

1 file changed

Lines changed: 45 additions & 39 deletions

File tree

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,82 @@
11
--- a/mempalace/mcp_server.py
22
+++ b/mempalace/mcp_server.py
3-
@@ -212,25 +212,56 @@
4-
5-
3+
@@ -271,34 +271,49 @@
4+
5+
66
def _get_collection(create=False):
77
- """Return the ChromaDB collection, caching the client between calls."""
88
- global _collection_cache, _metadata_cache, _metadata_cache_time
99
- try:
1010
- client = _get_client()
1111
- if create:
12-
- _collection_cache = ChromaCollection(
13-
- client.get_or_create_collection(
14-
- _config.collection_name, metadata={"hnsw:space": "cosine"}
12+
- # hnsw:num_threads=1 disables ChromaDB's multi-threaded ParallelFor
13+
- # HNSW insert path, which has a race in repairConnectionsForUpdate /
14+
- # addPoint (see issues #974, #965). Set via metadata on fresh
15+
- # collections and re-applied via _pin_hnsw_threads() for legacy
16+
- # palaces whose collections were created before this fix (the
17+
- # runtime config does not persist cross-process in chromadb 1.5.x,
18+
- # so the retrofit runs every time _get_collection opens a cache).
19+
- raw = client.get_or_create_collection(
20+
- _config.collection_name,
21+
- metadata={"hnsw:space": "cosine", "hnsw:num_threads": 1},
22+
- )
23+
- _pin_hnsw_threads(raw)
24+
- _collection_cache = ChromaCollection(raw)
25+
- _metadata_cache = None
26+
- _metadata_cache_time = 0
27+
- elif _collection_cache is None:
28+
- raw = client.get_collection(_config.collection_name)
29+
- _pin_hnsw_threads(raw)
30+
- _collection_cache = ChromaCollection(raw)
31+
- _metadata_cache = None
32+
- _metadata_cache_time = 0
33+
- return _collection_cache
34+
- except Exception:
35+
- return None
1536
+ """Return the ChromaDB collection, caching the client between calls.
1637
+
1738
+ Retries once on failure after clearing all caches (fixes stale-cache
1839
+ breakage without requiring a daemon restart). Logs the exception so
1940
+ failures are visible in the daemon log instead of silently returning None.
20-
+ Sets hnsw:num_threads=1 on every open — ChromaDB 1.5.x does not persist
21-
+ HNSW metadata across reopens, so parallel inserts stay disabled.
2241
+ """
2342
+ global _client_cache, _collection_cache, _metadata_cache, _metadata_cache_time
2443
+ for attempt in range(2):
2544
+ try:
2645
+ client = _get_client()
2746
+ if create:
28-
+ _collection_cache = ChromaCollection(
29-
+ client.get_or_create_collection(
30-
+ _config.collection_name,
31-
+ metadata={"hnsw:space": "cosine", "hnsw:num_threads": 1},
32-
+ )
47+
+ # hnsw:num_threads=1 disables ChromaDB's multi-threaded ParallelFor
48+
+ # HNSW insert path, which has a race in repairConnectionsForUpdate /
49+
+ # addPoint (see issues #974, #965). Set via metadata on fresh
50+
+ # collections and re-applied via _pin_hnsw_threads() for legacy
51+
+ # palaces whose collections were created before this fix (the
52+
+ # runtime config does not persist cross-process in chromadb 1.5.x,
53+
+ # so the retrofit runs every time _get_collection opens a cache).
54+
+ raw = client.get_or_create_collection(
55+
+ _config.collection_name,
56+
+ metadata={"hnsw:space": "cosine", "hnsw:num_threads": 1},
3357
+ )
58+
+ _pin_hnsw_threads(raw)
59+
+ _collection_cache = ChromaCollection(raw)
3460
+ _metadata_cache = None
3561
+ _metadata_cache_time = 0
3662
+ elif _collection_cache is None:
37-
+ _collection_cache = ChromaCollection(
38-
+ client.get_collection(_config.collection_name)
39-
)
63+
+ raw = client.get_collection(_config.collection_name)
64+
+ _pin_hnsw_threads(raw)
65+
+ _collection_cache = ChromaCollection(raw)
4066
+ _metadata_cache = None
4167
+ _metadata_cache_time = 0
42-
+ # Re-apply num_threads=1 on every open since ChromaDB 1.5.x does
43-
+ # not persist HNSW metadata across PersistentClient reopens (#1161).
44-
+ if _collection_cache is not None:
45-
+ try:
46-
+ existing = getattr(_collection_cache._collection, "metadata", {}) or {}
47-
+ if existing.get("hnsw:num_threads") != 1:
48-
+ _collection_cache._collection.modify(
49-
+ metadata={**existing, "hnsw:num_threads": 1}
50-
+ )
51-
+ except Exception:
52-
+ pass
5368
+ return _collection_cache
5469
+ except Exception as e:
5570
+ logger.error(
5671
+ "_get_collection attempt %d failed (palace=%s): %s",
5772
+ attempt + 1, _config.palace_path, e,
58-
)
59-
- _metadata_cache = None
60-
- _metadata_cache_time = 0
61-
- elif _collection_cache is None:
62-
- _collection_cache = ChromaCollection(client.get_collection(_config.collection_name))
63-
- _metadata_cache = None
64-
- _metadata_cache_time = 0
65-
- return _collection_cache
66-
- except Exception:
67-
- return None
73+
+ )
6874
+ if attempt == 0:
6975
+ _client_cache = None
7076
+ _collection_cache = None
7177
+ _metadata_cache = None
7278
+ _metadata_cache_time = 0
7379
+ return None
74-
75-
80+
81+
7682
def _no_palace():

0 commit comments

Comments
 (0)