{"record":{"id":"a3d6e9693530d9cf","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-subscribeuser-e","errorCode":null,"errorMessage":"Failed to send SubscribeUser: {e}","messagePattern":"Failed to send SubscribeUser: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/websocket/client.rs","lineNumber":570,"sourceCode":"            .map_err(|e| anyhow::anyhow!(\"Failed to send UnsubscribeMarket: {e}\"))\n    }\n\n    /// Authenticate and subscribe to the user channel.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if called on a market-channel client (no credentials available).\n    pub async fn subscribe_user(&self) -> anyhow::Result<()> {\n        if self.channel != WsChannel::User {\n            anyhow::bail!(\n                \"subscribe_user() requires a user-channel client (created with new_user())\"\n            );\n        }\n        self.cmd_tx\n            .read()\n            .await\n            .send(HandlerCommand::SubscribeUser)\n            .map_err(|e| anyhow::anyhow!(\"Failed to send SubscribeUser: {e}\"))?;\n        // Set only after the command is successfully enqueued so a failed send does not\n        // leave user_subscribed=true and cause an unintended replay on the next connect().\n        self.user_subscribed.store(true, Ordering::Relaxed);\n        Ok(())\n    }\n\n    /// Returns a cloneable subscription handle for use in spawned tasks.\n    #[must_use]\n    pub fn clone_subscription_handle(&self) -> WsSubscriptionHandle {\n        WsSubscriptionHandle {\n            cmd_tx: Arc::clone(&self.cmd_tx),\n        }\n    }\n\n    /// Takes the message receiver, leaving `None` in its place.\n    ///\n    /// This is useful when the data client needs to spawn its own handler\n    /// task that reads messages independently of the WS client.","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/websocket/client.rs#L552-L588","documentation":"subscribe_user() sends HandlerCommand::SubscribeUser to the feed-handler task to authenticate and open the private user channel. The send fails when the handler's receiver was dropped — the handler task has exited, so no session exists for the authenticated subscription. Note the flag user_subscribed is only set after a successful send, precisely to avoid replaying a subscription that never got enqueued.","triggerScenarios":"Calling subscribe_user() after disconnect(), after the handler task died (connection loss, panic, auth startup failure), or during a concurrent reconnect.","commonSituations":"Enabling the private order/fill feed while the gateway is down; credentials problems killed the handler earlier; shutdown racing an in-flight subscribe_user() from another task.","solutions":["Verify the client is connected (is_active()) and call connect() first if not","Retry subscribe_user() after reconnecting; the user subscription is replayed on reconnect once user_subscribed=true","Fix the root cause of the handler's exit (check wrapped error in logs, refresh auth credentials)","Serialize lifecycle calls (connect/subscribe/disconnect) to avoid races"],"exampleFix":"// before\nclient.subscribe_user().await?;\n// after\nif !client.is_active() {\n    client.connect().await?;\n}\nclient.subscribe_user().await?;","handlingStrategy":"try-catch","validationCode":"if !client.is_active() {\n    client.connect().await?;\n}\n// credentials must be present for the user channel\ndebug_assert!(!api_key.is_empty(), \"user channel requires credentials\");","typeGuard":null,"tryCatchPattern":"match client.subscribe_user().await {\n    Err(e) if e.to_string().contains(\"Failed to send SubscribeUser\") => {\n        client.connect().await?;\n        client.subscribe_user().await?;\n    }\n    other => other?,\n}","preventionTips":["Check is_active() before enabling the user channel","Ensure the handler task is healthy (it exits on fatal connection/auth errors) before subscribing","Keep credentials valid — handler startup auth failures kill the task that would receive this command","Call subscribe_user() only after a successful connect(), never concurrently with disconnect"],"tags":["rust","websocket","channel","auth","polymarket"],"backgroundTag":"channel-closed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}