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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Add search context to ask sourcebot context selector. [#397](https://github.com/sourcebot-dev/sourcebot/pull/397)

### Fixed
- Fixed multiple writes race condition on config file watcher. [#398](https://github.com/sourcebot-dev/sourcebot/pull/398)

## [4.6.0] - 2025-07-25

### Added
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"ai": "5.0.0-beta.28",
"ajv": "^8.17.1",
"bcryptjs": "^3.0.2",
"chokidar": "^4.0.3",
"class-variance-authority": "^0.7.0",
"client-only": "^0.0.1",
"clsx": "^2.1.1",
Expand Down
19 changes: 16 additions & 3 deletions packages/web/src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConnectionSyncStatus, OrgRole, Prisma, RepoIndexingStatus } from '@sour
import { env } from './env.mjs';
import { prisma } from "@/prisma";
import { SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_DOMAIN, SOURCEBOT_GUEST_USER_ID, SINGLE_TENANT_ORG_NAME } from './lib/constants';
import { watch } from 'fs';
import chokidar from 'chokidar';
import { ConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
import { hasEntitlement, loadConfig, isRemotePath, syncSearchContexts } from '@sourcebot/shared';
import { isServiceError, getOrgMetadata } from './lib/utils';
Expand Down Expand Up @@ -227,9 +227,22 @@ const initSingleTenancy = async () => {

// watch for changes assuming it is a local file
if (!isRemotePath(configPath)) {
watch(configPath, () => {
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)
});

watcher.on('change', async () => {
logger.info(`Config file ${configPath} changed. Re-syncing...`);
syncDeclarativeConfig(configPath);
try {
await syncDeclarativeConfig(configPath);
} catch (error) {
logger.error(`Failed to sync config: ${error}`);
}
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6606,6 +6606,7 @@ __metadata:
ai: "npm:5.0.0-beta.28"
ajv: "npm:^8.17.1"
bcryptjs: "npm:^3.0.2"
chokidar: "npm:^4.0.3"
class-variance-authority: "npm:^0.7.0"
client-only: "npm:^0.0.1"
clsx: "npm:^2.1.1"
Expand Down Expand Up @@ -8714,7 +8715,7 @@ __metadata:
languageName: node
linkType: hard

"chokidar@npm:^4.0.1":
"chokidar@npm:^4.0.1, chokidar@npm:^4.0.3":
version: 4.0.3
resolution: "chokidar@npm:4.0.3"
dependencies:
Expand Down