fix(ws): guard fingerprint insert against stale device_id (FK violation noise)

A socket reconnecting with a device_id that no longer exists in `devices`
(e.g. the row was deleted server-side) hit the device_fingerprints insert
with an unknown foreign key. INSERT OR IGNORE does NOT suppress FOREIGN KEY
violations, so it threw a caught-but-noisy "Fingerprint tracking error" on
every such reconnect. Null out an unknown device_id before the insert; a
genuinely fresh device sends no device_id and was always fine.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
ScreenTinker 2026-06-11 15:33:20 -05:00 committed by screentinker
parent e8a318e5fb
commit c8e664e66c

View file

@ -305,8 +305,13 @@ module.exports = function setupDeviceSocket(io) {
}
}
} else if (device_id || pairing_code) {
// device_id can be stale (e.g. a reconnect after the device row was
// deleted). device_fingerprints.device_id has an FK to devices(id), and
// INSERT OR IGNORE does NOT suppress FK violations - so null out an
// unknown id instead of letting it throw (was a caught, noisy error).
const fpDeviceId = (device_id && db.prepare('SELECT 1 FROM devices WHERE id = ?').get(device_id)) ? device_id : null;
db.prepare("INSERT OR IGNORE INTO device_fingerprints (fingerprint, device_id) VALUES (?, ?)")
.run(fingerprint, device_id || null);
.run(fingerprint, fpDeviceId);
}
} catch (e) {
console.error('Fingerprint tracking error:', e.message);