1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

clear extensions before reclaiming HttpRequests in their pool (#1063)

Issue #1062
This commit is contained in:
Philip Jenvey 2019-08-28 20:35:05 -07:00 committed by Nikolay Kim
parent 98bf8ab098
commit 616981ecf9

View File

@ -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<RefCell<Tracker>>,
}
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);
}
}