title: Is it possible to preserve items in a Tokio MPSC when the last Sender is dropped, but the Reciever is still active?
body: <p>I have a bounded Tokio MPSC queue with one sender and one reciever. I put several items into it in one task (in this case, the numbers 1 to 10, inclusive) and pull those items out in another task. As expected, the receiver processes all 10 values.</p>
  <p>Then, I add some other async task in between processing (in this case, by uncommenting the <code>sleep</code> call in the receive loop) and now, the contents of the channel are dropped when the last sender is finished.</p>
  <pre class="lang-rust prettyprint-override"><code>use tokio::sync::mpsc;
  use tokio::time::sleep;
  use std::time::Duration;
  
  #[tokio::main]
  async fn main() {
      let (mut tx, mut rx) = mpsc::channel(3);
      
      tokio::spawn(async move {
          while let Some(val) = rx.recv().await {
              println!(&quot;Recieved {}&quot;, val);
              //sleep(Duration::from_secs(1)).await;
          }
          
          println!(&quot;Recieve finished&quot;);
      });
      
      for i in 1..=10i32 {
          tx.send(i).await.unwrap();
          println!(&quot;Sent {}&quot;, i);
      }
  }
  </code></pre>
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a0cfba02da7aa6851bb675a29c7aa5a9" rel="nofollow noreferrer">Rust Playground</a></p>
  <p>Here's the output with the <code>sleep</code> commented out (order of interleaved values sometimes changes, but &quot;Recieved 10&quot; is always printed):</p>
  <pre><code>Sent 1
  Recieved 1
  Recieved 2
  Sent 2
  Sent 3
  Recieved 3
  Recieved 4
  Sent 4
  Sent 5
  Recieved 5
  Recieved 6
  Sent 6
  Sent 7
  Recieved 7
  Recieved 8
  Sent 8
  Sent 9
  Recieved 9
  Recieved 10
  Sent 10
  </code></pre>
  <p>And here's the output with the <code>sleep</code> not commented:</p>
  <pre><code>Sent 1
  Sent 2
  Sent 3
  Recieved 1
  Sent 4
  Recieved 2
  Sent 5
  Recieved 3
  Sent 6
  Recieved 4
  Sent 7
  Recieved 5
  Sent 8
  Recieved 6
  Sent 9
  Recieved 7
  Sent 10
  </code></pre>
  <p>Is there any way to be certain that all values put into the queue are processed by the <code>Reciever</code> even after the last <code>Sender</code> is dropped (assuming that the <code>Reciever</code> is not dropped)? The <code>close</code> function appears to do this, but going the other way (ensuring that the queue contents are processed before the <code>Reciever</code> is dropped). If not, is there an alternate async-friendly MPSC implementation that can provide this guarantee?</p>