{"record":{"id":"b0e3eff117e486b9","repo":"clockworklabs/SpacetimeDB","slug":"client-not-found-client-id","errorCode":null,"errorMessage":"Client not found: {client_id:?}","messagePattern":"Client not found: (.+?)","errorType":"exception","errorClass":"DBError","httpStatus":null,"severity":"warning","filePath":"crates/core/src/subscription/module_subscription_manager.rs","lineNumber":847,"sourceCode":"        for id in self.clients.keys().copied().collect::<Vec<_>>() {\n            if let Some(client) = self.clients.get(&id)\n                && client.dropped.load(Ordering::Relaxed)\n            {\n                self.remove_all_subscriptions(&id);\n            }\n        }\n    }\n\n    /// Remove a single subscription for a client.\n    /// This will return an error if the client does not have a subscription with the given query id.\n    pub fn remove_subscription(&mut self, client_id: ClientId, query_id: ClientQueryId) -> Result<Vec<Query>, DBError> {\n        let subscription_id = (client_id, query_id);\n        let Some(ci) = self\n            .clients\n            .get_mut(&client_id)\n            .filter(|ci| !ci.dropped.load(Ordering::Acquire))\n        else {\n            return Err(anyhow::anyhow!(\"Client not found: {client_id:?}\").into());\n        };\n\n        #[cfg(test)]\n        ci.assert_ref_count_consistency();\n\n        let Some(query_hashes) = ci.v1_subscriptions.remove(&subscription_id) else {\n            return Err(anyhow::anyhow!(\"Subscription not found: {subscription_id:?}\").into());\n        };\n        let mut queries_to_return = Vec::new();\n        for hash in query_hashes {\n            let remaining_refs = {\n                let Some(count) = ci.subscription_ref_count.get_mut(&hash) else {\n                    return Err(anyhow::anyhow!(\"Query count not found for query hash: {hash:?}\").into());\n                };\n                *count -= 1;\n                *count\n            };\n            if remaining_refs > 0 {","sourceCodeStart":829,"sourceCodeEnd":865,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/9e0d92412ff2248f401a8ad12d535f2b5ac30912/crates/core/src/subscription/module_subscription_manager.rs#L829-L865","documentation":"ModuleSubscriptionManager::remove_subscription could not find a live entry for the ClientId in its clients map, or the entry is flagged dropped. The unsubscribe arrived after the client's server-side state was already torn down (disconnect cleanup) or the client was never registered. Its subscriptions are removed along with the client state, so the explicit removal has nothing to act on.","triggerScenarios":"Sending Unsubscribe on a connection that already disconnected (the manager dropped its state), or racing a disconnect: socket close plus cleanup is processed before the explicit unsubscribe message.","commonSituations":"Client SDKs that both close the socket and send unsubscribe; retrying unsubscribe after a reconnect; disconnect watchdog racing message handling.","solutions":["Treat it as best-effort: if the client is already gone, its subscriptions were cleaned up with it - ignore the error","Send unsubscribe before closing the socket, and do not resend it after reconnect since subscription ids do not survive reconnects","In client SDK teardown paths, catch and suppress this specific error instead of propagating it"],"exampleFix":"// before\nlet queries = manager.remove_subscription(client_id, query_id)?; // may error during disconnect races\n\n// after\nmatch manager.remove_subscription(client_id, query_id) {\n    Ok(queries) => { /* release query resources */ }\n    Err(e) if e.to_string().contains(\"Client not found\") => { /* already disconnected; nothing to do */ }\n    Err(e) => return Err(e),\n}","handlingStrategy":"try-catch","validationCode":"// Ensure the connection is still open before sending Unsubscribe\nif connection.is_closed() {\n    // skip unsubscribe; server cleanup removes subscriptions on disconnect\n    return Ok(());\n}","typeGuard":null,"tryCatchPattern":"match manager.remove_subscription(client_id, query_id) {\n    Ok(queries) => { /* release resources */ }\n    Err(e) if e.to_string().contains(\"Client not found\") => { /* already cleaned up; ignore */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Send unsubscribe before closing the socket","Do not resend unsubscribe after reconnect; ids do not survive reconnects","Treat teardown-path unsubscribe errors as best-effort in client SDKs"],"tags":["spacetimedb","subscriptions","client-lifecycle","race-condition"],"backgroundTag":"client-session-expired","analyzedSha":"9e0d92412ff2248f401a8ad12d535f2b5ac30912","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}