From 616981ecf9a5be5de467f9d9753ca47aa44c9d21 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Wed, 28 Aug 2019 20:35:05 -0700 Subject: [PATCH] clear extensions before reclaiming HttpRequests in their pool (#1063) Issue #1062 --- src/request.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/request.rs b/src/request.rs index 0fc0647ff..ac9b9933e 100644 --- a/src/request.rs +++ b/src/request.rs @@ -259,6 +259,7 @@ impl Drop for HttpRequest { if Rc::strong_count(&self.0) == 1 { let v = &mut self.0.pool.0.borrow_mut(); if v.len() < 128 { + self.extensions_mut().clear(); v.push(self.0.clone()); } } @@ -494,4 +495,36 @@ mod tests { let resp = call_service(&mut srv, req); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } + + #[test] + fn test_extensions_dropped() { + struct Tracker { + pub dropped: bool, + } + struct Foo { + tracker: Rc>, + } + impl Drop for Foo { + fn drop(&mut self) { + self.tracker.borrow_mut().dropped = true; + } + } + + let tracker = Rc::new(RefCell::new(Tracker { dropped: false })); + { + let tracker2 = Rc::clone(&tracker); + let mut srv = init_service(App::new().data(10u32).service( + web::resource("/").to(move |req: HttpRequest| { + req.extensions_mut().insert(Foo { tracker: Rc::clone(&tracker2) }); + HttpResponse::Ok() + }), + )); + + let req = TestRequest::default().to_request(); + let resp = call_service(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + } + + assert!(tracker.borrow().dropped); + } }