From c8e664e66c61cd61a59a5f4bec00669c3b062703 Mon Sep 17 00:00:00 2001 From: ScreenTinker Date: Thu, 11 Jun 2026 15:33:20 -0500 Subject: [PATCH] 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) --- server/ws/deviceSocket.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/ws/deviceSocket.js b/server/ws/deviceSocket.js index 954384f..ef8ecad 100644 --- a/server/ws/deviceSocket.js +++ b/server/ws/deviceSocket.js @@ -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);