title: Why does Tokio return the error "Cannot drop a runtime in a context where blocking is not allowed"?
body: <p>I have a Tokio client that talks to a remote server and is supposed to keep the connection alive permanently. I've implemented the initial authentication handshake and found that when my test terminates, I get an odd panic:</p>
  <pre class="lang-none prettyprint-override"><code>---- test_connect_without_database stdout ----
  thread 'test_connect_without_database' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.5/src/runtime/blocking/shutdown.rs:51:21
  </code></pre>
  <p>I'm at an absolute loss as to what might be causing this so I don't know what other bits of code to add for context.</p>
  <p>Here's my minimal reproducible example (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4fd5f88d41595ebaf352de98106d3a85" rel="noreferrer">playground</a>):</p>
  <pre class="lang-rust prettyprint-override"><code>use std::cell::RefCell;
  use std::net::{IpAddr, SocketAddr};
  use tokio::net::TcpStream;
  use tokio::prelude::*;
  use tokio::runtime;
  
  #[derive(PartialEq, Debug)]
  pub struct Configuration {
      /// Database username.
      username: String,
      /// Database password.
      password: String,
      /// Database name.
      db_name: String,
      /// IP address for the remove server.
      address: IpAddr,
      /// Remote server port.
      port: u16,
      /// Number of connections to open.
      connections: u16,
  }
  
  impl Configuration {
      pub fn new(
          username: &amp;str,
          password: &amp;str,
          db_name: &amp;str,
          address: &amp;str,
          port: u16,
          connections: u16,
      ) -&gt; Result&lt;Configuration, Box&lt;dyn std::error::Error&gt;&gt; {
          let address = address.to_string().parse()?;
          let configuration = Configuration {
              username: username.to_string(),
              password: password.to_string(),
              db_name: db_name.to_string(),
              address,
              port,
              connections,
          };
          Ok(configuration)
      }
  
      pub fn socket(&amp;self) -&gt; SocketAddr {
          SocketAddr::new(self.address, self.port)
      }
  }
  
  #[derive(Debug)]
  pub struct Session {
      configuration: Configuration,
      runtime: RefCell&lt;runtime::Runtime&gt;,
  }
  
  impl Session {
      fn new(config: Configuration) -&gt; Result&lt;Session, io::Error&gt; {
          let runtime = runtime::Builder::new_multi_thread()
              .worker_threads(6)
              .enable_all()
              .build()?;
          let session = Session {
              configuration: config,
              runtime: RefCell::new(runtime),
          };
          Ok(session)
      }
  
      fn configuration(&amp;self) -&gt; &amp;Configuration {
          &amp;self.configuration
      }
  }
  
  #[derive(Debug)]
  pub(crate) struct Connection&lt;'a&gt; {
      /// Socket uses to read and write from.
      session: &amp;'a Session,
      /// Connection to the remote server.
      stream: TcpStream,
      /// Flag that indicates whether the connection is live.
      live: bool,
  }
  
  impl&lt;'a&gt; Connection&lt;'a&gt; {
      async fn new(session: &amp;Session) -&gt; Result&lt;Connection&lt;'_&gt;, Box&lt;dyn std::error::Error&gt;&gt; {
          let mut stream = TcpStream::connect(session.configuration().socket()).await?;
          let conn = Connection {
              session,
              stream,
              live: true,
          };
  
          Ok(conn)
      }
  
      fn live(&amp;self) -&gt; bool {
          self.live
      }
  }
  
  #[tokio::test]
  async fn test_connect_without_database() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
      let config = Configuration::new(&quot;rust&quot;, &quot;&quot;, &quot;&quot;, &quot;127.0.0.1&quot;, 2345, 2).unwrap();
      let session = Session::new(config).unwrap();
      let conn = Connection::new(&amp;session).await?;
      assert!(conn.live());
      Ok(())
  }
  
  fn main() {
      println!(&quot;{}&quot;, 65u8 as char)
  }
  </code></pre>