Skip to content
Open
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
10 changes: 10 additions & 0 deletions .changeset/settings-write-lock-1039.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"nostream": patch
---

fix(admin): serialize every settings-file writer with the settings write lock

The lock records its owner hostname and refreshes a heartbeat, so a relay in a
container no longer steals a live CLI lock just because the host PID does not
exist in its own PID namespace. An expired heartbeat always wins over a reused
PID, and the lock is held until async writers settle.
42 changes: 28 additions & 14 deletions src/cli/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
toCategoryLabel,
validatePathAgainstDefaults,
validateSettings,
withSettingsWriteLock,
} from '../../utils/settings-config'
import {
isSecretEnvKey,
Expand Down Expand Up @@ -128,25 +129,38 @@ export const runConfigSet = async (
return 1
}

const settings = loadUserSettings() as unknown as Record<string, unknown>
const next = setByPath(settings, path, parseTypedValue(rawValue, valueType))
// Read and write under the settings-file lock so a CLI update cannot clobber
// a moderation ban written by a running relay in between.
const outcome = await withSettingsWriteLock<
| { ok: true }
| { ok: false; validationIssues: Array<{ path: string; message: string }> }
>(() => {
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
const settings = loadUserSettings() as unknown as Record<string, unknown>
const next = setByPath(settings, path, parseTypedValue(rawValue, valueType))

if (options.validate !== false) {
const merged = loadMergedSettings() as unknown as Record<string, unknown>
const mergedNext = setByPath(merged, path, getByPath(next, path))
const validationIssues = validateSettings(mergedNext as any)

if (validationIssues.length > 0) {
return { ok: false as const, validationIssues }
}
}

if (options.validate !== false) {
const merged = loadMergedSettings() as unknown as Record<string, unknown>
const mergedNext = setByPath(merged, path, getByPath(next, path))
const validationIssues = validateSettings(mergedNext as any)
saveSettings(next as any)

if (validationIssues.length > 0) {
logError('Config update rejected by validation:')
for (const issue of validationIssues) {
logError(`- ${issue.path}: ${issue.message}`)
}
return { ok: true as const }
})

return 1
if ('validationIssues' in outcome) {
logError('Config update rejected by validation:')
for (const issue of outcome.validationIssues) {
logError(`- ${issue.path}: ${issue.message}`)
}
}

saveSettings(next as any)
return 1
}

logInfo(`Updated ${path}`)

Expand Down
50 changes: 34 additions & 16 deletions src/controllers/admin/patch-notifications-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
loadUserSettings,
saveSettings,
validateSettings,
withSettingsWriteLock,
} from '../../utils/settings-config'
import { validateSchema } from '../../utils/validation'
import { recordAdminAuditFromRequest } from '../../utils/record-admin-audit'
Expand All @@ -35,27 +36,44 @@ export class PatchAdminNotificationsController implements IController {
return
}

const merged = loadMergedSettings()
const current = getMergedAdminNotifications(merged)
const nextNotifications = mergeAdminNotificationsPatch(current, validation.value)
// Read and write under the same settings-file lock the moderation writer
// uses, so a notification save cannot clobber a ban written meanwhile.
const outcome = await withSettingsWriteLock<
| { ok: true; redacted: ReturnType<typeof getRedactedAdminNotifications> }
| { ok: false; issues: Array<{ path: string; message: string }> }
>(() => {
const merged = loadMergedSettings()
const current = getMergedAdminNotifications(merged)
const nextNotifications = mergeAdminNotificationsPatch(current, validation.value)

const userSettings = loadUserSettings() as Settings
const nextUserSettings = mergeDeepRight(userSettings, {
admin: {
...userSettings.admin,
notifications: nextNotifications,
},
}) as Settings
const userSettings = loadUserSettings() as Settings
const nextUserSettings = mergeDeepRight(userSettings, {
admin: {
...userSettings.admin,
notifications: nextNotifications,
},
}) as Settings

const mergedNext = mergeDeepRight(loadDefaults(), nextUserSettings) as Settings
const mergedNext = mergeDeepRight(loadDefaults(), nextUserSettings) as Settings

const issues = validateSettings(mergedNext)
if (issues.length > 0) {
response.status(400).setHeader('content-type', 'application/json').send({ error: 'Validation failed', issues })
const issues = validateSettings(mergedNext)
if (issues.length > 0) {
return { ok: false as const, issues }
}

saveSettings(nextUserSettings)

return { ok: true as const, redacted: getRedactedAdminNotifications() }
})

if ('issues' in outcome) {
response
.status(400)
.setHeader('content-type', 'application/json')
.send({ error: 'Validation failed', issues: outcome.issues })
return
}

saveSettings(nextUserSettings)
appendSettingsAuditLog({
action: 'settings.updated',
changes: [{ path: 'admin.notifications', reload: 'hot-reload' }],
Expand All @@ -74,7 +92,7 @@ export class PatchAdminNotificationsController implements IController {

response.status(200).setHeader('content-type', 'application/json').send({
ok: true,
notifications: getRedactedAdminNotifications(),
notifications: outcome.redacted,
})
}
}
47 changes: 32 additions & 15 deletions src/controllers/admin/patch-settings-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
setByPath,
validatePathAgainstDefaults,
validateSettings,
withSettingsWriteLock,
} from '../../utils/settings-config'
import {
isWriteProtectedSettingsPath,
Expand Down Expand Up @@ -50,27 +51,43 @@ export class PatchAdminSettingsController implements IController {
return
}

const userSettings = loadUserSettings() as unknown as Record<string, unknown>
const nextUserSettings = changes.reduce(
(settings, change) => setByPath(settings, change.path, change.value),
userSettings,
)
const merged = loadMergedSettings() as unknown as Record<string, unknown>
const mergedNext = changes.reduce(
(settings, change) => setByPath(settings, change.path, getByPath(nextUserSettings, change.path)),
merged,
)
const validationIssues = validateSettings(mergedNext as unknown as Settings)

if (validationIssues.length > 0) {
// Every writer of settings.yaml takes this lock. Reading inside it is what
// stops an ordinary settings save from writing a document read before a
// moderation ban was applied, which would silently undo the ban.
const outcome = await withSettingsWriteLock<
| { ok: true; nextUserSettings: Record<string, unknown> }
| { ok: false; issues: Array<{ path: string; message: string }> }
>(() => {
const userSettings = loadUserSettings() as unknown as Record<string, unknown>
const nextUserSettings = changes.reduce(
(settings, change) => setByPath(settings, change.path, change.value),
userSettings,
)
const merged = loadMergedSettings() as unknown as Record<string, unknown>
const mergedNext = changes.reduce(
(settings, change) => setByPath(settings, change.path, getByPath(nextUserSettings, change.path)),
merged,
)
const validationIssues = validateSettings(mergedNext as unknown as Settings)

if (validationIssues.length > 0) {
return { ok: false as const, issues: validationIssues }
}

saveSettings(nextUserSettings as unknown as Settings)

return { ok: true as const, nextUserSettings }
})

if ('issues' in outcome) {
response
.status(400)
.setHeader('content-type', 'application/json')
.send({ error: 'Validation failed', issues: validationIssues })
.send({ error: 'Validation failed', issues: outcome.issues })
return
}

saveSettings(nextUserSettings as unknown as Settings)
const nextUserSettings = outcome.nextUserSettings
const updatedChanges = changes.map(({ path }) => ({
path,
value: redactSettingsValue(path, getByPath(nextUserSettings, path)),
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/admin/post-settings-restore-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PostAdminSettingsRestoreController implements IController {
}

try {
restoreSettingsBackup(validation.value.filename)
await restoreSettingsBackup(validation.value.filename)
appendSettingsAuditLog({
action: 'settings.restored',
filename: validation.value.filename,
Expand Down
Loading
Loading