title: Why does my shared actix-web state sometimes reset back to the original value?
body: <p>I'm trying to implement shared state in the Actix-Web framework with <code>Arc</code> and <code>Mutex</code>. The following code compiles, but when I run it, the counter sometimes goes all the way back to 0. How do I prevent that from happening?</p>
  
  <pre><code>use actix_web::{web, App, HttpServer};
  use std::sync::{Arc, Mutex};
  
  // This struct represents state
  struct AppState {
      app_name: String,
      counter: Arc&lt;Mutex&lt;i64&gt;&gt;,
  }
  
  fn index(data: web::Data&lt;AppState&gt;) -&gt; String {
      let mut counter = data.counter.lock().unwrap();
      *counter += 1;
      format!("{}", counter)
  }
  
  pub fn main() {
      HttpServer::new(|| {
          App::new()
              .hostname("hello world")
              .register_data(web::Data::new(AppState {
                  app_name: String::from("Actix-web"),
                  counter: Arc::new(Mutex::new(0)),
              }))
              .route("/", web::get().to(index))
      })
      .bind("127.0.0.1:8088")
      .unwrap()
      .run()
      .unwrap();
  }
  </code></pre>