Skip to content

fix(web): Fix multiple writes race condition on config file watcher #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2025

Conversation

brendan-kellam
Copy link
Contributor

@brendan-kellam brendan-kellam commented Jul 26, 2025

When starting up a fresh Docker installation with a empty config file, I hit the following issue when I made a edit to the file:

sourcebot-1  | 2025-07-26T21:30:33.399Z info: [web-initialize] Config file /app/config.json changed. Re-syncing...
<-- NOTICE THE SECOND CHANGE EVENT 6ms LATER --> sourcebot-1  | 2025-07-26T21:30:33.405Z info: [web-initialize] Config file /app/config.json changed. Re-syncing...
sourcebot-1  | 2025-07-26T21:30:33.445Z info: [web-initialize] Upserted connection with name 'starter-connection'. Connection ID: 1
postgres-1   | 2025-07-26 21:30:33.445 UTC [42] ERROR:  duplicate key value violates unique constraint "Connection_name_orgId_key"
postgres-1   | 2025-07-26 21:30:33.445 UTC [42] DETAIL:  Key (name, "orgId")=(starter-connection, 1) already exists.
postgres-1   | 2025-07-26 21:30:33.445 UTC [42] STATEMENT:  INSERT INTO "public"."Connection" ("name","config","isDeclarative","createdAt","updatedAt","syncStatus","connectionType","orgId") VALUES ($1,$2,$3,$4,$5,CAST($6::text AS "public"."ConnectionSyncStatus"),$7,$8) RETURNING "public"."Connection"."id"

According to Cursor:

Race condition in config file watching: Docker volume mounts trigger multiple filesystem events for single file changes, causing concurrent syncDeclarativeConfig() executions and database unique constraint violations
File watching instability: fs.watch() fires multiple events within milliseconds (6ms apart in logs), leading to duplicate connection creation attempts

The fix (also suggested by Cursor):

Replaced fs.watch() with chokidar: Added file write stability detection (awaitWriteFinish) to ensure file changes are complete before syncing
Added proper error handling: Wrapped sync operations in try/catch to prevent crashes from concurrent execution conflicts

I switched to using chokidar and I am no longer hitting the issue.

Summary by CodeRabbit

  • Bug Fixes

    • Resolved a race condition when monitoring configuration file changes, improving reliability during multiple simultaneous writes.
  • Chores

    • Updated internal dependencies to enhance file watching capabilities.

@brendan-kellam brendan-kellam requested a review from msukkari July 26, 2025 22:16
Copy link

coderabbitai bot commented Jul 26, 2025

Walkthrough

The changes update the configuration file watcher in the web package by replacing Node.js's native fs.watch with the chokidar library for improved file watching and race condition handling. The changelog is updated to reflect this fix, and the new dependency is added to the package manifest.

Changes

File(s) Change Summary
CHANGELOG.md Added an entry under "Unreleased" describing the fix for a race condition in the config watcher.
packages/web/package.json Added chokidar@^4.0.3 as a new dependency.
packages/web/src/initialize.ts Replaced fs.watch with chokidar for file watching; improved event handling and error logging.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant Chokidar
    participant ConfigSync

    App->>Chokidar: Start watching config file
    Chokidar-->>App: Emits 'change' event after file stabilizes
    App->>ConfigSync: Attempt to sync declarative config (async)
    ConfigSync-->>App: Success or error (logged)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇
A watcher once raced, but now it is wise,
With Chokidar’s help, no more surprise.
Configs sync safely, no errors in sight,
The changelog now notes our fix with delight.
Dependency added, the code’s feeling spry—
Hooray for a bug that has hopped goodbye!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bkellam/chokidar

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @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.

This comment has been minimized.

Copy link

@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

🧹 Nitpick comments (1)
packages/web/src/initialize.ts (1)

230-246: Excellent implementation to fix the race condition.

The chokidar configuration effectively addresses the Docker volume mount race condition issues:

  • ignoreInitial: true prevents duplicate events on startup
  • awaitWriteFinish with 100ms stabilityThreshold ensures file writes are complete
  • atomic: true handles temp file + rename operations properly
  • Added error handling prevents crashes from sync failures

Consider these minor enhancements:

             const watcher = chokidar.watch(configPath, {
                 ignoreInitial: true,           // Don't fire events for existing files
                 awaitWriteFinish: {
                     stabilityThreshold: 100,   // File size stable for 100ms
                     pollInterval: 100          // Check every 100ms
                 },
-                atomic: true                   // Handle atomic writes (temp file + rename)
+                atomic: true,                  // Handle atomic writes (temp file + rename)
+                usePolling: false,             // Use native events when possible
+                ignorePermissionErrors: true   // Ignore permission errors in Docker
             });
             
             watcher.on('change', async () => {
-                logger.info(`Config file ${configPath} changed. Re-syncing...`);
+                logger.info(`Config file ${configPath} changed, re-syncing declarative config...`);
                 try {
                     await syncDeclarativeConfig(configPath);
+                    logger.info(`Successfully synced declarative config from ${configPath}`);
                 } catch (error) {
-                    logger.error(`Failed to sync config: ${error}`);
+                    logger.error(`Failed to sync declarative config from ${configPath}:`, error);
                 }
             });
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e404838 and fcbdf6d.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • packages/web/package.json (1 hunks)
  • packages/web/src/initialize.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*

📄 CodeRabbit Inference Engine (.cursor/rules/style.mdc)

Filenames should always be camelCase. Exception: if there are filenames in the same directory with a format other than camelCase, use that format to keep things consistent.

Files:

  • CHANGELOG.md
  • packages/web/package.json
  • packages/web/src/initialize.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (3)
packages/web/package.json (1)

114-114: Chokidar dependency is up-to-date and secure

Verified that [email protected] is the latest stable release on npm and no security advisories were found. Approving the addition.

• packages/web/package.json (line 114): "chokidar": "^4.0.3"

packages/web/src/initialize.ts (1)

5-5: LGTM: Chokidar import added.

The import statement is correctly placed and follows the existing import organization pattern.

CHANGELOG.md (1)

13-15: LGTM: Changelog entry accurately documents the fix.

The changelog entry properly documents the race condition fix and follows the established format with the correct PR reference.

@brendan-kellam brendan-kellam merged commit 3e50469 into main Jul 26, 2025
6 checks passed
@brendan-kellam brendan-kellam deleted the bkellam/chokidar branch July 26, 2025 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants