Files
wekan/server/00processErrors.js
T
Lauri Ojansivu edd2904334 Fix #6533 for good: a rejected promise no longer ends the server, and four more guards.
The reporter's 10.45 log shows the startup work landing - "[startup] a startup
step could not finish because the database was busy; WeKan continues" and
"[checklists] Backfilled boardId on 0 row(s); 21 row(s) have no card to take it
from" instead of a 130k-card scan - and then the server dying anyway:

  SyncedCron: Fatal error encountered (unhandledRejection): ValidationError:
    ... sqlite.(*collection).UpdateAll ... database is locked (5) (SQLITE_BUSY)
  SyncedCron: Received UNHANDLED_REJECTION signal - cleaning up running jobs
  systemd: Main process exited, code=exited, status=1/FAILURE

`quave:synced-cron` installs a process-wide unhandledRejection handler that calls
process.exit(1). So ANY unhandled rejection - including one write losing a race
for the SQLite write lock, which is transient by definition - killed the whole
server, and systemd restarted it into a database made busier by the restart. The
counter reached 73.

server/00processErrors.js takes that decision back. The package is deliberately
polite about it: it only exits `if (process.listenerCount('unhandledRejection')
=== 1)`, i.e. when nothing else has an opinion, so having one is the documented
way to stop it. A transient database error is logged and recorded for Admin
Panel / Problems and WeKan keeps serving; anything else is logged with its full
stack and WeKan still keeps serving, because ending everybody's session over one
rejected promise is a larger failure than the one being reported. An uncaught
EXCEPTION still exits unless it is a transient database error - a process that
threw out of a place nobody handled can hold half-applied state.

The four guards the same run surfaced, each a later assertion:

  * boardsVisibilitySettings: the Visibility saves go through
    visibilityTextFields(), which does the "is this input on screen" check once
    for every field instead of per call site.
  * changelogFormat: "the newest release" now means the newest RELEASE (an
    Upcoming section may sit above it), the Upcoming section is checked by its
    own test, and a release needs at least ONE entry - "more than five" measured
    the day's workload rather than the format, and v10.46 carried two fixes.
  * leftMenu: /information redirects the FlowRouter way, like /translation.
  * narrowWindowLayout: the phone rules carry !important, because the desktop
    rule they override is more specific.

Thanks to Nissulya and xet7 !

Fixes #6533,
2026-07-28 17:59:17 +03:00

105 lines
4.5 KiB
JavaScript

// ============================================================================
// A rejected promise must not take the whole server down (#6533).
//
// The reporter's snap was still in a restart loop after the startup work was made
// resilient, and their log says exactly why:
//
// SyncedCron: Fatal error encountered (unhandledRejection): ValidationError:
// ... sqlite.(*collection).UpdateAll ... database is locked (5) (SQLITE_BUSY)
// SyncedCron: Received UNHANDLED_REJECTION signal - cleaning up running jobs
// systemd: Main process exited, code=exited, status=1/FAILURE
//
// `quave:synced-cron` installs a process-wide `unhandledRejection` handler and
// calls `process.exit(1)` from it. So ANY unhandled rejection anywhere in WeKan -
// including one write losing a race for the SQLite write lock, which is transient
// by definition - killed the server, systemd restarted it, and the restart made
// the database busier. The restart counter reached 73.
//
// This file takes that decision back, for two reasons:
//
// * a WeKan server serves everybody using it; ending the process over one
// failed write is a much larger failure than the one being reported, and it
// is the loop itself that makes the database unreachable;
// * SyncedCron's own handler is deliberately polite about it - it only cleans
// up and exits `if (process.listenerCount('unhandledRejection') === 1)`, i.e.
// when nothing else has an opinion. Having an opinion here is the documented
// way to keep it from exiting.
//
// What is NOT changed: nothing is silenced. Every rejection is logged with its
// stack, and a transient database error is also recorded in the `database` event
// stream, so Admin Panel / Problems shows what the database said and how often.
//
// An uncaught EXCEPTION is different: it can leave a request half-done and the
// process in a state nobody reasoned about. A transient database error is still
// survivable there, but anything else keeps the old behaviour and exits, so a
// real fault is not papered over.
// ============================================================================
import { classifyDatabaseError } from '/models/lib/databaseErrors';
// The classifier's ids for "the database was busy / momentarily unreachable" -
// the same list the startup guard uses (server/00startupResilience.js). Everything
// else is a fault that will not fix itself.
const TRANSIENT = new Set([
'deadlock', // includes SQLite's "database is locked (5) (SQLITE_BUSY)"
'too-many-connections',
'connection-lost',
'timeout',
]);
export function isTransientDatabaseError(error) {
if (!error) return false;
const classified = classifyDatabaseError(error);
return !!classified && TRANSIENT.has(classified.id);
}
function record(error, source) {
try {
const { recordDatabaseProblem } = require('/server/lib/databaseProblems');
recordDatabaseProblem(error, { source });
} catch (e) {
/* the recorder is best-effort by design */
}
}
function describe(error) {
return (error && (error.stack || error.reason || error.message)) || String(error);
}
// Installed as early as server/imports.js can manage, and BEFORE SyncedCron
// starts - though the listener count SyncedCron checks is read when the error
// happens, not when the listener is added, so order only affects which log line
// comes first.
process.on('unhandledRejection', reason => {
if (isTransientDatabaseError(reason)) {
console.error(
'[unhandledRejection] the database was busy; WeKan keeps running and the ' +
'write is retried by whatever issued it:',
(reason && (reason.reason || reason.message)) || String(reason),
);
record(reason, 'unhandledRejection');
return;
}
// Not a database problem: a real unhandled rejection, worth the whole stack -
// and still not worth ending everybody's session over.
console.error('[unhandledRejection] WeKan keeps running:', describe(reason));
});
process.on('uncaughtException', error => {
if (isTransientDatabaseError(error)) {
console.error(
'[uncaughtException] the database was busy; WeKan keeps running:',
(error && error.message) || String(error),
);
record(error, 'uncaughtException');
return;
}
// Anything else: say it plainly and stop, as before. A process that has thrown
// out of a place nobody handled can be holding half-applied state, and a
// restart is the honest answer to that.
console.error('[uncaughtException] WeKan is stopping:', describe(error));
process.exit(1);
});