{"record":{"id":"d47c036ef285fbdd","repo":"gitbutlerapp/gitbutler","slug":"handler-task-panicked-e","errorCode":null,"errorMessage":"handler task panicked: {e}","messagePattern":"handler task panicked: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-server/src/lib.rs","lineNumber":70,"sourceCode":"\n#[derive(Clone)]\nstruct AppState {\n    broadcaster: Arc<Mutex<Broadcaster>>,\n    extra: Extra,\n    app_settings: AppSettingsWithDiskSync,\n}\n\n/// Converts a synchronous command handler into an axum `MethodRouter` that works with\n/// `Router::route`.\nfn but_post<F, S>(f: F) -> MethodRouter<S, Infallible>\nwhere\n    F: Fn(serde_json::Value) -> anyhow::Result<serde_json::Value> + Copy + Send + Sync + 'static,\n    S: Clone + Send + Sync + 'static,\n{\n    post(move |Json(params)| async move {\n        let res = tokio::task::spawn_blocking(move || f(params))\n            .await\n            .unwrap_or_else(|e| Err(anyhow::anyhow!(\"handler task panicked: {e}\")));\n        cmd_result_to_json(res)\n    })\n}\n\n/// Converts an asynchronous command handler into an axum `MethodRouter` that works with\n/// `Router::route`.\nfn but_post_async<F, Fut, S>(f: F) -> MethodRouter<S, Infallible>\nwhere\n    F: Fn(serde_json::Value) -> Fut + Copy + Send + Sync + 'static,\n    Fut: Future<Output = anyhow::Result<serde_json::Value>> + Send,\n    S: Clone + Send + Sync + 'static,\n{\n    post(move |Json(params)| async move {\n        let res = f(params).await;\n        cmd_result_to_json(res)\n    })\n}\n","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-server/src/lib.rs#L52-L88","documentation":"Wraps a JoinError from `tokio::task::spawn_blocking` when the blocking handler function panics. Instead of propagating the panic to the axum worker, the server converts it into a normal Err result that is serialized as a JSON error response via `cmd_result_to_json`.","triggerScenarios":"Any `but_post`-registered command whose blocking handler function `f` panics (unwrap on None/Err, index out of bounds, explicit panic, assertion failure) while processing the posted JSON params.","commonSituations":"A command handler hits an unexpected state like a poisoned lock, corrupted repo state, or a serde edge case that unwraps; clients see an HTTP response with 'handler task panicked' instead of a server crash.","solutions":["Check the server logs/panic message for the handler's panic location and fix the underlying bug in that command handler","Look at the posted JSON params in the request — a malformed or unexpected payload often triggers the unwrap/panic","Update or file an issue against the specific command handler so it returns Result instead of panicking","Retry only after fixing; retrying the same request will panic again"],"exampleFix":"// before\nfn handler(params: Value) -> Result<Value> {\n    let id = params[\"id\"].as_str().unwrap(); // panics\n// after\nfn handler(params: Value) -> Result<Value> {\n    let id = params[\"id\"].as_str()\n        .ok_or_else(|| anyhow::anyhow!(\"missing or non-string 'id'\"))?;","handlingStrategy":"try-catch","validationCode":"// client-side: validate params shape before posting\nif (typeof params.id !== 'string') throw new Error('id must be a string');\nawait fetch(url, { method: 'POST', body: JSON.stringify(params) });","typeGuard":"function isPanicResponse(res: { ok: boolean; body: unknown }): boolean {\n  return !res.ok || (typeof res.body === 'object' && res.body !== null &&\n    'message' in res.body && String((res.body as any).message).includes('handler task panicked'));\n}","tryCatchPattern":"const res = await post('/cmd', params);\nconst body = await res.json();\nif (typeof body?.message === 'string' && body.message.startsWith('handler task panicked')) {\n  reportBug(body.message); // server-side bug, not retryable\n}","preventionTips":["Fix handler unwraps — return Result instead of panicking in blocking handlers","Log posted params on panic to reproduce","Add panic hooks in spawn_blocking handlers to capture backtraces","Keep client/server versions in sync"],"tags":["panic","tokio","server","json"],"backgroundTag":"thread-interrupted","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}