Mark the workspace cache stale instead of evicting it in access-group jobs

Merged
CH
7d ago
+25-5

Fixes the flaky TypeError: Cannot convert undefined or null to object at Object.values(rolesPermissions) (and other cache keys read as undefined) seen in integration shards since Nov 26.

Root cause. The access-group jobs (AccessGroupSyncService, and the record access group recomputes through findAfterEvictingLocalCache) called evictWorkspaceFromLocalCache to read what another process had just written. That deletes the workspace's local cache entries. A read that has just matched an entry's hash against Redis reads the entry back a few microtasks later; when the delete lands in between, the key is missing from the result.

Why now. The evictions date from #30201 but ran behind IS_ACCESS_GROUPS_ENABLED, off in the seeds. #31486, #31499 and #31513 removed the flags on Nov 25, so every workspace now runs these jobs, and channel visibility schedules a sync on record writes. On main, the two message-channel specs trigger 583 evictions, 81 of them during an in-flight read. Across this week's failed integration jobs the crash appears in 0 of 551 through Nov 25 and in 23 of 196 since.

Why not with other metadata. Metadata changes go through invalidate, which writes a new hash and only marks local entries stale. It never deletes them, so a read always finds its entry.

Fix. The jobs follow that pattern: new markWorkspaceLocalCacheStale clears the workspace's memoized reads and marks its local entries stale without deleting them, so the next read rechecks each hash and refetches only what changed. findAfterEvictingLocalCache becomes findAfterMarkingLocalCacheStale. evictWorkspaceFromLocalCache stays for the workspace iterator, which frees memory between workspaces in commands.

Member map invalidation. The evictions were also hiding two writes that never invalidated flatWorkspaceMemberMaps, a key held only in process memory: removing a user from a workspace (deleteUserWorkspace, soft or hard), and schema changes, since the map stores full workspace member records. Both now flush it. Without this, a process kept the old map as long as its hash matched.

flowchart LR
  A[Access-group job starts] --> B[Clear memoized reads]
  B --> C[Mark local entries stale]
  C --> D[Next read checks Redis hash]
  D -- same hash --> E[Keep local data]
  D -- new hash --> F[Fetch from Redis or recompute]
BeforeAfter
Job needs fresh cachedeletes every local entry of the workspacemarks them stale
Concurrent read in the same processcan lose a key (undefined)always finds its entry
Unchanged keys after the job startsrefetched from Redis or recomputedkept after a hash check