{"record":{"id":"f379a80f1bd08111","repo":"actix/actix-web","slug":"cannot-reuse-response-builder-f379a8","errorCode":null,"errorMessage":"cannot reuse response builder","messagePattern":"cannot reuse response builder","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"actix-web/src/response/builder.rs","lineNumber":272,"sourceCode":"    ///     .finish();\n    /// ```\n    #[cfg(feature = \"cookies\")]\n    pub fn cookie(&mut self, cookie: cookie::Cookie<'_>) -> &mut Self {\n        match cookie.to_string().try_into_value() {\n            Ok(hdr_val) => self.append_header((header::SET_COOKIE, hdr_val)),\n            Err(err) => {\n                self.error = Some(err.into());\n                self\n            }\n        }\n    }\n\n    /// Returns a reference to the response-local data/extensions container.\n    #[inline]\n    pub fn extensions(&self) -> Ref<'_, Extensions> {\n        self.res\n            .as_ref()\n            .expect(\"cannot reuse response builder\")\n            .extensions()\n    }\n\n    /// Returns a mutable reference to the response-local data/extensions container.\n    #[inline]\n    pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {\n        self.res\n            .as_mut()\n            .expect(\"cannot reuse response builder\")\n            .extensions_mut()\n    }\n\n    /// Set a body and build the `HttpResponse`.\n    ///\n    /// Unlike [`message_body`](Self::message_body), errors are converted into error\n    /// responses immediately.\n    ///\n    /// `HttpResponseBuilder` can not be used after this call.","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/actix/actix-web/blob/4d435abc281842f3cbee165b6cde739e001d3a25/actix-web/src/response/builder.rs#L254-L290","documentation":"This is a runtime panic from `HttpResponseBuilder::extensions()`. The builder stores the response in an `Option<Response<BoxBody>>`; after a terminal method like `.body()`, `.finish()`, `.json()`, or `.streaming()` consumes the inner response via `.take()`, calling `.extensions()` finds `None` and panics with `.expect(\"cannot reuse response builder\")` at line 272.","triggerScenarios":"Calling `.extensions()` after `.body()`, `.finish()`, `.json()`, or `.streaming()` on the same builder. Also happens if `.extensions()` is called after `.take()`. For example: `let res = builder.finish(); let ext = builder.extensions();` panics.","commonSituations":"Storing a builder, calling `.finish()` early (perhaps in a helper), then trying to access extensions later. Also occurs with the `From<HttpResponseBuilder>` impl which calls `.finish()` internally.","solutions":["Access `.extensions()` before calling any terminal method (`.body()`, `.finish()`, etc.)","Use `.take()` to split the builder into a new instance if you need to access state after building","Return the response immediately after building; do not continue to use the builder","Access extensions on the built `HttpResponse` instead: `res.extensions()`"],"exampleFix":"// before\nlet mut builder = HttpResponse::Ok();\nbuilder.insert_header((\"X-Test\", \"1\"));\nlet res = builder.finish();\nlet ext = builder.extensions(); // panics\n\n// after\nlet mut builder = HttpResponse::Ok();\nbuilder.insert_header((\"X-Test\", \"1\"));\nlet ext = builder.extensions(); // before finish\nlet res = builder.finish();","handlingStrategy":"validation","validationCode":"// Access extensions before calling any terminal method.\n// Structure handlers to call .finish()/.body() last.\nasync fn handler(req: HttpRequest) -> HttpResponse {\n    let mut builder = HttpResponse::Ok();\n    builder.extensions_mut().insert(MyData { id: 42 }); // before finish\n    builder.insert_header((\"X-Custom\", \"value\"));\n    builder.finish() // terminal call, last\n}","typeGuard":null,"tryCatchPattern":"// Rust does not catch panics. Ensure .extensions() is called before .finish()/.body().\n// If you need extensions after building, use the HttpResponse directly:\nlet res = HttpResponse::Ok().finish();\n// Access on the response, not the builder:\nlet ext = res.extensions();","preventionTips":["Call .extensions() before any terminal method (.finish(), .body(), .json(), .streaming())","Access extensions on the built HttpResponse instead of the builder after construction","Use method chaining to make the call order explicit and unambiguous","Treat .finish()/.body() as the final operation on a builder"],"tags":["rust","actix-web","response-builder","runtime-panic","api-misuse"],"backgroundTag":null,"analyzedSha":"4d435abc281842f3cbee165b6cde739e001d3a25","analyzedAt":"2026-08-09T01:01:40.926Z","contentChangedAt":"2026-08-09T01:01:40.926Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}