fix(protocol): stop flooding logs when disk writes fail
After identity persistence is suspended, later observations were still logged one-by-one. That filled Docker logs and made the WebUI log stream unusable while the bot kept receiving messages. Report the skip once, then at most once a minute with a count. Console write failures must not throw into message handling.
This commit is contained in:
5 files changed
+63
-3
No files matched your search
@@ -506,7 +506,13 @@ class FileWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.quota.reserve(this.file.path, bytes)) return false;
|
if (!this.quota.reserve(this.file.path, bytes)) return false;
|
||||||
|
try {
|
||||||
this.file.stream.write(data);
|
this.file.stream.write(data);
|
||||||
|
} catch (error) {
|
||||||
|
this.disabled = true;
|
||||||
|
this.quota.writeFailed(this.file.path, error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
this.file.bytes += bytes;
|
this.file.bytes += bytes;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -264,7 +264,13 @@ function emit(
|
|||||||
// and ESC (0x1B) so multi-line records (stack traces) and ANSI color
|
// and ESC (0x1B) so multi-line records (stack traces) and ANSI color
|
||||||
// sequences emitted by render() pass through intact.
|
// sequences emitted by render() pass through intact.
|
||||||
// eslint-disable-next-line no-control-regex
|
// eslint-disable-next-line no-control-regex
|
||||||
stream.write(line.replace(/[\x00-\x08\x0B-\x1A\x1C-\x1F\x7F]/g, '') + '\n');
|
const consoleLine = line.replace(/[\x00-\x08\x0B-\x1A\x1C-\x1F\x7F]/g, '') + '\n';
|
||||||
|
try {
|
||||||
|
stream.write(consoleLine);
|
||||||
|
} catch {
|
||||||
|
// A full Docker json-file log volume can make stdout/stderr throw.
|
||||||
|
// Logging must not take down message handling or the WebUI.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// File transport sees levels >= currentFileLevel (SNOWLUMA_LOG_FILE_LEVEL,
|
// File transport sees levels >= currentFileLevel (SNOWLUMA_LOG_FILE_LEVEL,
|
||||||
|
|||||||
@@ -60,6 +60,13 @@ describe('logger UIN slot padding', () => {
|
|||||||
expect(() => createLogger('Test').info('no uin')).not.toThrow();
|
expect(() => createLogger('Test').info('no uin')).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not throw when the console stream cannot accept the line', () => {
|
||||||
|
vi.mocked(process.stderr.write).mockImplementation(() => {
|
||||||
|
throw Object.assign(new Error('ENOSPC: no space left on device'), { code: 'ENOSPC' });
|
||||||
|
});
|
||||||
|
expect(() => createLogger('Test').error('disk is full')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
it('keeps structured subscriber lines plain while terminal output stays colored', () => {
|
it('keeps structured subscriber lines plain while terminal output stays colored', () => {
|
||||||
const captured: LogEntry[] = [];
|
const captured: LogEntry[] = [];
|
||||||
const unsubscribe = subscribeLogs((entry) => captured.push(entry));
|
const unsubscribe = subscribeLogs((entry) => captured.push(entry));
|
||||||
|
|||||||
@@ -589,6 +589,23 @@ describe('IdentityService', () => {
|
|||||||
lastError: expect.stringMatching(/locked/i),
|
lastError: expect.stringMatching(/locked/i),
|
||||||
lastFailureAt: suspendedFailureAt,
|
lastFailureAt: suspendedFailureAt,
|
||||||
});
|
});
|
||||||
|
const skipLogsBeforeBurst = seen.filter((entry) =>
|
||||||
|
/persistence write skipped/.test(entry.message),
|
||||||
|
).length;
|
||||||
|
expect(skipLogsBeforeBurst).toBe(1);
|
||||||
|
for (let i = 0; i < 50; i += 1) {
|
||||||
|
identity.rememberRequestIdentity({
|
||||||
|
uid: `u_skip_burst_${i}`,
|
||||||
|
uin: 500_000 + i,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
expect(seen.filter((entry) => /persistence write skipped/.test(entry.message)))
|
||||||
|
.toHaveLength(1);
|
||||||
|
await vi.advanceTimersByTimeAsync(60_000);
|
||||||
|
identity.rememberRequestIdentity({ uid: 'u_skip_after_minute', uin: 600_000 });
|
||||||
|
const skipLogs = seen.filter((entry) => /persistence write skipped/.test(entry.message));
|
||||||
|
expect(skipLogs).toHaveLength(2);
|
||||||
|
expect(skipLogs[1]?.message).toMatch(/suppressed=50/);
|
||||||
|
|
||||||
releaseLock();
|
releaseLock();
|
||||||
identity.close();
|
identity.close();
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ const moduleLogger = createLogger('Identity');
|
|||||||
|
|
||||||
const PERSISTENCE_QUEUE_CAPACITY = 256;
|
const PERSISTENCE_QUEUE_CAPACITY = 256;
|
||||||
const PERSISTENCE_RETRY_DELAYS_MS = [100, 500, 2_000, 10_000, 30_000] as const;
|
const PERSISTENCE_RETRY_DELAYS_MS = [100, 500, 2_000, 10_000, 30_000] as const;
|
||||||
|
/** After persistence is suspended, do not warn on every skipped write. */
|
||||||
|
const SKIPPED_WRITE_LOG_INTERVAL_MS = 60_000;
|
||||||
|
|
||||||
export type IdentityPersistenceState = 'memory-only' | 'healthy' | 'degraded' | 'closed';
|
export type IdentityPersistenceState = 'memory-only' | 'healthy' | 'degraded' | 'closed';
|
||||||
|
|
||||||
@@ -159,6 +161,8 @@ export class IdentityService {
|
|||||||
private lastFailureAt_: number | null = null;
|
private lastFailureAt_: number | null = null;
|
||||||
private abandonedWrites_ = 0;
|
private abandonedWrites_ = 0;
|
||||||
private skippedWrites_ = 0;
|
private skippedWrites_ = 0;
|
||||||
|
private skippedWritesSinceLog_ = 0;
|
||||||
|
private lastSkippedWriteLogAt_ = 0;
|
||||||
private persistenceSuspended_ = false;
|
private persistenceSuspended_ = false;
|
||||||
private closed_ = false;
|
private closed_ = false;
|
||||||
|
|
||||||
@@ -1242,6 +1246,8 @@ export class IdentityService {
|
|||||||
const attempts = this.retryAttempt_;
|
const attempts = this.retryAttempt_;
|
||||||
this.retryAttempt_ = 0;
|
this.retryAttempt_ = 0;
|
||||||
this.nextRetryAt_ = null;
|
this.nextRetryAt_ = null;
|
||||||
|
this.skippedWritesSinceLog_ = 0;
|
||||||
|
this.lastSkippedWriteLogAt_ = 0;
|
||||||
this.log.info(
|
this.log.info(
|
||||||
'identity persistence recovered: uin=%s flushed=%d pending=0 retries=%d',
|
'identity persistence recovered: uin=%s flushed=%d pending=0 retries=%d',
|
||||||
this.uin_, pendingAtStart, attempts,
|
this.uin_, pendingAtStart, attempts,
|
||||||
@@ -1268,9 +1274,27 @@ export class IdentityService {
|
|||||||
private recordSkippedWrite(label: string): void {
|
private recordSkippedWrite(label: string): void {
|
||||||
this.skippedWrites_ += 1;
|
this.skippedWrites_ += 1;
|
||||||
this.abandonedWrites_ += 1;
|
this.abandonedWrites_ += 1;
|
||||||
|
const now = Date.now();
|
||||||
|
if (
|
||||||
|
this.lastSkippedWriteLogAt_ !== 0
|
||||||
|
&& now - this.lastSkippedWriteLogAt_ < SKIPPED_WRITE_LOG_INTERVAL_MS
|
||||||
|
) {
|
||||||
|
this.skippedWritesSinceLog_ += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const suppressed = this.skippedWritesSinceLog_;
|
||||||
|
this.skippedWritesSinceLog_ = 0;
|
||||||
|
this.lastSkippedWriteLogAt_ = now;
|
||||||
|
if (suppressed > 0) {
|
||||||
this.log.warn(
|
this.log.warn(
|
||||||
'identity persistence write skipped: uin=%s label=%s pending=0 retry=%d skipped=%d abandoned=%d',
|
'identity persistence write skipped: uin=%s label=%s skipped=%d abandoned=%d suppressed=%d',
|
||||||
this.uin_, label, this.retryAttempt_, this.skippedWrites_, this.abandonedWrites_,
|
this.uin_, label, this.skippedWrites_, this.abandonedWrites_, suppressed,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.log.warn(
|
||||||
|
'identity persistence write skipped: uin=%s label=%s skipped=%d abandoned=%d',
|
||||||
|
this.uin_, label, this.skippedWrites_, this.abandonedWrites_,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user