From 297f8596316883757a6318951626defbd5f96da9 Mon Sep 17 00:00:00 2001 From: sakridge Date: Tue, 11 Sep 2018 16:52:45 -0700 Subject: [PATCH] Change '>=' back to '>' to fix recycling of blobs/packets (#1192) Recycler will have a strong ref to the item so it will be at least 1, >= will always prevent recycling. --- src/packet.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/packet.rs b/src/packet.rs index e9f6c960e8bff9..9110e694cd630d 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -210,7 +210,7 @@ impl Recycler { // be passed across threads ('alloc' is a nightly-only API), and so our // reference-counted recyclables are awkwardly being recycled by hand, // which allows this race condition to exist. - if Arc::strong_count(&x) >= 1 { + if Arc::strong_count(&x) > 1 { // Commenting out this message, is annoying for known use case of // validator hanging onto a blob in the window, but also sending it over // to retransmmit_request @@ -557,6 +557,23 @@ mod tests { assert_eq!(r.gc.lock().unwrap().len(), 0); } + #[test] + pub fn test_recycling_is_happening() { + // Test the case in allocate() which should return a re-used object and not allocate a new + // one. + let r = PacketRecycler::default(); + let x0 = r.allocate(); + { + x0.write().unwrap().packets.resize(1, Packet::default()); + } + r.recycle(x0, "recycle"); + let x1 = r.allocate(); + assert_ne!( + x1.read().unwrap().packets.len(), + Packets::default().packets.len() + ); + } + #[test] pub fn blob_recycler_test() { let r = BlobRecycler::default();