Skip to content

fix: watcher dispose logic#4355

Merged
Aaaaash merged 2 commits intomainfrom
fix/watcher-dispose-logic
Feb 11, 2025
Merged

fix: watcher dispose logic#4355
Aaaaash merged 2 commits intomainfrom
fix/watcher-dispose-logic

Conversation

@Aaaaash
Copy link
Copy Markdown
Member

@Aaaaash Aaaaash commented Feb 11, 2025

Types

  • 🎉 New Features
  • 🐛 Bug Fixes
  • 📚 Documentation Changes
  • 💄 Code Style Changes
  • 💄 Style Changes
  • 🪚 Refactors
  • 🚀 Performance Improvements
  • 🏗️ Build System
  • ⏱ Tests
  • 🧹 Chores
  • Other Changes

Background or solution

Changelog

Summary by CodeRabbit

  • 新特性

    • unwatchFileChanges 方法的返回类型已更新为 void,简化了使用方式。
  • 重构

    • 优化了文件监控管理流程,确保监控路径与实际文件系统更准确匹配,并提升了资源释放与错误处理的稳定性。
  • 样式

    • 调整了部分日志和提示信息的格式,提高了问题排查时的信息清晰度。

@Aaaaash
Copy link
Copy Markdown
Member Author

Aaaaash commented Feb 11, 2025

/next

@opensumi opensumi Bot added the 🐞 bug Something isn't working label Feb 11, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 11, 2025

Walkthrough

该 PR 对文件监视器取消逻辑进行了修改。主要更改包括将所有 unwatchFileChanges 方法的返回类型从 Promise<void> 改为 void,使其由异步返回改为同步返回;在 RecursiveFileSystemWatcher 中,将用于存储处理器的键从 uri 改为 watchPath,并引入了私有方法 disposeWatcher 以增强错误处理和日志记录;同时,在 WatcherHostServiceImpl 中增加了异步处理与资源清理逻辑。

Changes

文件 更改摘要
packages/core-common/src/types/file-watch.ts IWatcher 接口中 unwatchFileChanges(uri: string): Promise<void> 修改为 unwatchFileChanges(uri: string): void
packages/file-service/src/node/hosted/recursive/file-service-watcher.ts RecursiveFileSystemWatcher 类中:
- 将 WATCHER_HANDLERS 键由 uri 改为 watchPath
- 将 unwatchFileChanges 返回类型由 Promise<void> 改为 void
- 添加私有方法 disposeWatcher(path: string) 实现处理器处置与错误日志;
- 移除已弃用的接口并增加中文注释。
packages/file-service/src/node/hosted/un-recursive/file-service-watcher.ts UnRecursiveFileSystemWatcher 类中:
- 格式调整构造函数及日志输出;
- 将 unwatchFileChanges 返回类型由 Promise<void> 改为 void
packages/file-service/src/node/hosted/watcher.host.service.ts WatcherHostServiceImpl 类中:
- 对取消监视操作添加了 async 处理,允许使用 await 调用 unwatchFileChanges
- 增加日志记录并在取消后从 watcherCollection 中删除对应 watcher。

Sequence Diagram(s)

sequenceDiagram
    participant HostService as WatcherHostServiceImpl
    participant Recursive as RecursiveFileSystemWatcher
    participant UnRecursive as UnRecursiveFileSystemWatcher
    participant Dispose as disposeWatcher (内部方法)

    HostService->>Recursive: 调用 unwatchFileChanges(uri)
    Recursive->>Dispose: 内部调用 disposeWatcher(basePath)
    Dispose-->>Recursive: 返回处理结果(无返回值,包含错误日志)
    Recursive-->>HostService: 返回(void)

    HostService->>UnRecursive: 调用 unwatchFileChanges(uri)
    UnRecursive-->>HostService: 返回(void)
    
    HostService->>HostService: 在调用完成后清理 watcherCollection
Loading

Possibly related PRs

Suggested reviewers

  • Ricbet
  • erha19

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > glob@7.2.3: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 507fc4d and 8211cc9.

📒 Files selected for processing (1)
  • packages/file-service/src/node/hosted/watcher.host.service.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/file-service/src/node/hosted/watcher.host.service.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: build-windows
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: ubuntu-latest, Node.js 20.x

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (1)
packages/file-service/src/node/hosted/watcher.host.service.ts (1)

138-145: ⚠️ Potential issue

注意异步清理与同步接口的冲突

Disposable.create 中使用了 async/await,但 unwatchFileChanges 现在是同步接口。这可能导致资源清理不完整。建议重新设计清理逻辑。

-Disposable.create(async () => {
+Disposable.create(() => {
   this.unrecursiveFileSystemWatcher!.unwatchFileChanges(uri.toString());
   this.logger.log('dispose unrecursive watcher: ', uri.toString());
   this.watchedDirs.delete(uri.toString());
   this.WATCHER_HANDLERS.delete(watcherId);
 })

Also applies to: 155-162

🧹 Nitpick comments (2)
packages/file-service/src/node/hosted/un-recursive/file-service-watcher.ts (1)

162-169: 验证同步取消监听的实现

unwatchFileChanges 方法现在是同步的,需要确保资源清理是完整的。建议添加日志记录取消监听的结果。

 unwatchFileChanges(uri: string): void {
   const basePath = FileUri.fsPath(uri);
   if (this.watcherCollections.has(basePath)) {
     const watcher = this.watcherCollections.get(basePath);
     watcher?.close();
     this.watcherCollections.delete(basePath);
+    this.logger.log(`[Un-Recursive] Stopped watching ${basePath}`);
   }
 }
packages/file-service/src/node/hosted/recursive/file-service-watcher.ts (1)

366-370: 建议增加错误处理的详细信息

虽然 unwatchFileChanges 现在正确地调用了 disposeWatcher,但建议在日志中添加更多上下文信息。

 unwatchFileChanges(uri: string): void {
-  this.logger.log('[Recursive] Un watch: ', uri);
+  this.logger.log('[Recursive] Unwatching file changes for: ', uri);
   const basePath = FileUri.fsPath(uri);
   this.disposeWatcher(basePath);
+  this.logger.log('[Recursive] Successfully unwatched: ', uri);
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bfefebb and 507fc4d.

📒 Files selected for processing (4)
  • packages/core-common/src/types/file-watch.ts (1 hunks)
  • packages/file-service/src/node/hosted/recursive/file-service-watcher.ts (2 hunks)
  • packages/file-service/src/node/hosted/un-recursive/file-service-watcher.ts (3 hunks)
  • packages/file-service/src/node/hosted/watcher.host.service.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: 🚀🚀🚀 Next Version for pull request
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build-windows
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: unittest (macos-latest, 18.x, node)
🔇 Additional comments (5)
packages/core-common/src/types/file-watch.ts (1)

43-43: 注意接口变更的影响范围

unwatchFileChanges 方法的返回类型从 Promise<void> 改为 void 是一个破坏性变更,需要确保所有实现类都已相应更新。

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# 搜索所有实现了 IWatcher 接口的类
ast-grep --pattern 'class $_ implements IWatcher {
  $$$
}'

Length of output: 21812


注意 “unwatchFileChanges” 方法实现的一致性

经查,从接口 IWatcher 的实现中,我们在 packages/file-service/src/node/hosted/un-recursive/file-service-watcher.ts 文件中找到了 unwatchFileChanges(uri: string): void 的实现,与最新的接口定义保持一致。目前没有发现其它实现仍使用旧的返回类型 Promise<void>

建议进一步确认代码库中没有遗漏其他实现,如果确认无遗漏后,此变更无须额外处理。

packages/file-service/src/node/hosted/un-recursive/file-service-watcher.ts (1)

54-58: 改进错误日志格式

错误日志格式的改进提高了可读性,便于调试。

packages/file-service/src/node/hosted/watcher.host.service.ts (1)

82-82: 优化资源清理时机

在重新监听前清理 watcherCollection 是一个好的做法,确保了资源的正确释放。

packages/file-service/src/node/hosted/recursive/file-service-watcher.ts (2)

148-152: 改进监听器存储键值

使用 watchPath 作为 WATCHER_HANDLERS 的键值比使用 URI 更准确,因为它反映了实际的文件系统路径。


353-364: 良好的错误处理实践

新增的 disposeWatcher 方法通过 try-catch-finally 结构提供了完善的错误处理,确保即使在出错的情况下也能清理资源。

@opensumi
Copy link
Copy Markdown
Contributor

opensumi Bot commented Feb 11, 2025

🎉 PR Next publish successful!

3.7.1-next-1739238115.0

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 11, 2025

Codecov Report

Attention: Patch coverage is 60.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 54.14%. Comparing base (bfefebb) to head (8211cc9).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...le-service/src/node/hosted/watcher.host.service.ts 0.00% 5 Missing ⚠️
...c/node/hosted/un-recursive/file-service-watcher.ts 33.33% 2 Missing ⚠️
.../src/node/hosted/recursive/file-service-watcher.ts 91.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4355   +/-   ##
=======================================
  Coverage   54.14%   54.14%           
=======================================
  Files        1639     1639           
  Lines      100319   100326    +7     
  Branches    21766    21766           
=======================================
+ Hits        54317    54321    +4     
- Misses      38224    38227    +3     
  Partials     7778     7778           
Flag Coverage Δ
jsdom 49.62% <0.00%> (-0.01%) ⬇️
node 12.28% <60.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

erha19
erha19 previously approved these changes Feb 11, 2025
Ricbet
Ricbet previously approved these changes Feb 11, 2025
@Aaaaash Aaaaash dismissed stale reviews from Ricbet and erha19 via 8211cc9 February 11, 2025 02:11
@Aaaaash Aaaaash merged commit 6131e9a into main Feb 11, 2025
@Aaaaash Aaaaash deleted the fix/watcher-dispose-logic branch February 11, 2025 03:16
@coderabbitai coderabbitai Bot mentioned this pull request May 14, 2025
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants