Skip to content

fix: run NSRunningApplication metadata lookups off main thread with bounded concurrency (#9)#23

Merged
ShadyUnderLight merged 1 commit into
mainfrom
fix/issue9-metadata-off-main-thread
Apr 20, 2026
Merged

fix: run NSRunningApplication metadata lookups off main thread with bounded concurrency (#9)#23
ShadyUnderLight merged 1 commit into
mainfrom
fix/issue9-metadata-off-main-thread

Conversation

@ShadyUnderLight
Copy link
Copy Markdown
Owner

问题

ProcessSnapshotProvider.resolveMetadata 同步调用 NSRunningApplication(processIdentifier:),会触发进程属性查询,在进程已退出或大量进程时可能产生延迟。虽然在 actor 线程内执行,但 macOS 内部可能仍路由到主线程,容易成为瓶颈。

maxMetadataLookupsPerSnapshot = 48 只限制了参与排序的进程数量,不是全局 metadata 查询的并发限制。

解决方案

  1. snapshot() 改为 async throws,在 actor 内并发执行 metadata 解析
  2. resolveMetadataBatch() 使用 TaskGroup + Throttle actor 将 metadata 解析任务分散到后台,最大并发数 maxConcurrentMetadataLookups = 8
  3. 每个任务在 TaskGroup.addTask 内执行 NSRunningApplication 查询,实际运行在 Swift 协程后台线程,不阻塞 actor
  4. AppMetadata / CachedMetadata / RawProcess 标记为 Sendable,满足 actor isolated 约束

行为变化

场景 旧行为 新行为
metadata 查询 同步在 actor 内串行执行 TaskGroup 并发执行,最多 8 个同时
主线程 可能被 NSRunningApplication 内部路由阻塞 查询在协程后台执行,主线程不受影响
缓存 refreshMetadataCache 串行写入 结果批量写回 cache

Closes #9

@ShadyUnderLight ShadyUnderLight merged commit d2e3d6c into main Apr 20, 2026
2 checks passed
@ShadyUnderLight ShadyUnderLight deleted the fix/issue9-metadata-off-main-thread branch April 20, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] NSRunningApplication 在 ProcessSnapshotProvider actor 内同步调用可能阻塞

1 participant