title: AttributeError: 'NoneType' object has no attribute '_global_run_stack'
body: <p><strong>Description</strong></p>
  <p>I am using PTAN library with an A3C model and I am trying to work with <strong>wandb sweep</strong> but I've encountered some weird problems, and I am not sure if it's a bug regarding sweep (because if I am going to use just a simple model without any threads involving is going to work properly) or I am doing something wrong.</p>
  <p><strong>How to reproduce</strong></p>
  <p><em><strong>training function:</strong></em></p>
  <pre><code>def train(conf):
      batch = []
      step_idx = 0
      epoch = conf['epochs']
      try:
          with commune.RewardTracker(writer, stop_reward=conf['reward_bound']) as tracker:
              with ptan.common.utils.TBMeanTracker(writer, batch_size=100) as tb_tracker:
                  while True:
                      if step_idx == epoch:
                          break
                      train_entry = train_queue.get()
                      if isinstance(train_entry, TotalReward):
                          if tracker.reward(train_entry.reward, step_idx):
                              break
                          continue
                      if isinstance(train_entry, TotalProfit):
                          tracker.profits(train_entry.total_profit, train_entry.curr_profit, step_idx)
                          continue
                      step_idx += 1
                      if step_idx % 100 == 0:
                          torch.save(net.state_dict(), os.path.join(SAVING_FOLDER, PROJECT_NAME))
  
                      batch.append(train_entry)
                      if len(batch) &lt; conf['batch_size']:
                          continue
  
                      states_v, actions_t, vals_ref_v = commune.unpack_batch(batch, net,
                                                                             last_val_gamma=conf['gamma'] ** conf['reward_steps'],
                                                                             device=device)
                      batch.clear()
  
                      optimizer.zero_grad()
                      logits_v, value_v = net(states_v)
  
                      loss_value_v = F.mse_loss(value_v.squeeze(-1), vals_ref_v)
  
                      log_prob_v = F.log_softmax(logits_v, dim=1)
                      adv_v = vals_ref_v - value_v.detach()
                      log_prob_actions_v = adv_v * log_prob_v[range(conf['batch_size']), actions_t]
                      loss_policy_v = -log_prob_actions_v.mean()
  
                      prob_v = F.softmax(logits_v, dim=1)
                      entropy_loss_v = conf['entropy_beta'] * (prob_v * log_prob_v).sum(dim=1).mean()
  
                      loss_v = entropy_loss_v + loss_value_v + loss_policy_v
                      loss_v.backward()
                      nn_utils.clip_grad_norm_(net.parameters(), conf['clip_grad'])
                      optimizer.step()
  
                      tb_tracker.track(&quot;advantage&quot;, adv_v, step_idx)
                      tb_tracker.track(&quot;values&quot;, value_v, step_idx)
                      tb_tracker.track(&quot;batch_rewards&quot;, vals_ref_v, step_idx)
                      tb_tracker.track(&quot;loss_entropy&quot;, entropy_loss_v, step_idx)
                      tb_tracker.track(&quot;loss_policy&quot;, loss_policy_v, step_idx)
                      tb_tracker.track(&quot;loss_value&quot;, loss_value_v, step_idx)
                      tb_tracker.track(&quot;loss_total&quot;, loss_v, step_idx)
      finally:
          for p in data_proc_list:
              p.terminate()
              p.join()
  </code></pre>
  <p><strong>main function:</strong></p>
  <pre><code>if __name__ == &quot;__main__&quot;:
      mp.set_start_method('fork')
      device = torch.device(&quot;cuda:0&quot; if use_cuda else &quot;cpu&quot;)
  
      with open(r'sweep_config.yaml') as file:
          sweep_config = yaml.load(file, Loader=yaml.FullLoader)
  
      logs_dir_name = &quot;a3c_stock&quot;
      wandb.tensorboard.patch(root_logdir=logs_dir_name)
  
      sweep_id = wandb.sweep(sweep_config, project=&quot;sweep_project&quot;, entity=&quot;vildnex&quot;)
      wandb.init(config=config_default)
  
      config = wandb.config
  
      writer = SummaryWriter(comment=logs_dir_name)
  
      env = make_env(config)
      net = commune.AtariA2C(env.observation_space.shape, env.action_space.n).to(device)
      net.share_memory()
  
      if not os.path.isdir(SAVING_FOLDER):
          os.mkdir(SAVING_FOLDER)
  
      if os.path.isfile(os.path.join(SAVING_FOLDER, PROJECT_NAME)):
          net.load_state_dict(torch.load(os.path.join(SAVING_FOLDER, PROJECT_NAME), map_location=device))
  
      optimizer = optim.RMSprop(net.parameters(), lr=config.learning_rate, eps=1e-3)
  
      train_queue = mp.Queue(maxsize=config.processes_count)
      data_proc_list = []
      dict_conf = dict(config)
      for _ in range(config.processes_count):
          data_proc = mp.Process(target=data_func, args=(net, device, train_queue, dict_conf))
          data_proc.start()
          data_proc_list.append(data_proc)
  
      wandb.agent(sweep_id, lambda: train(dict_conf))
  </code></pre>
  <p><strong>Error message:</strong></p>
  <pre><code>Exception in thread Thread-6:
  Traceback (most recent call last):
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/agents/pyagent.py&quot;, line 303, in _run_job
      self._function()
    File &quot;&lt;PATH&gt;/RL_TraningBot/EXPERIMENTS/A3C_TEST.py&quot;, line 191, in &lt;lambda&gt;
      wandb.agent(sweep_id, lambda: train(dict_conf))
    File &quot;&lt;PATH&gt;/RL_TraningBot/EXPERIMENTS/A3C_TEST.py&quot;, line 105, in train
      tracker.profits(train_entry.total_profit, train_entry.curr_profit, step_idx)
    File &quot;&lt;PATH&gt;/RL_TraningBot/EXPERIMENTS/commune.py&quot;, line 118, in profits
      self.writer.add_scalar(&quot;total_profit&quot;, total_profit, frame)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/torch/utils/tensorboard/writer.py&quot;, line 344, in add_scalar
      self._get_file_writer().add_summary(
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/torch/utils/tensorboard/writer.py&quot;, line 250, in _get_file_writer
      self.file_writer = FileWriter(self.log_dir, self.max_queue,
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/torch/utils/tensorboard/writer.py&quot;, line 60, in __init__
      self.event_writer = EventFileWriter(
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/integration/tensorboard/monkeypatch.py&quot;, line 157, in __init__
      _notify_tensorboard_logdir(logdir, save=save, root_logdir=root_logdir_arg)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/integration/tensorboard/monkeypatch.py&quot;, line 167, in _notify_tensorboard_logdir
      wandb.run._tensorboard_callback(logdir, save=save, root_logdir=root_logdir)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/wandb_run.py&quot;, line 804, in _tensorboard_callback
      self._backend.interface.publish_tbdata(logdir, save, root_logdir)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/interface/interface.py&quot;, line 202, in publish_tbdata
      self._publish(rec)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/interface/interface.py&quot;, line 518, in _publish
      raise Exception(&quot;The wandb backend process has shutdown&quot;)
  Exception: The wandb backend process has shutdown
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.9/threading.py&quot;, line 954, in _bootstrap_inner
      self.run()
    File &quot;/usr/lib/python3.9/threading.py&quot;, line 892, in run
      self._target(*self._args, **self._kwargs)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/agents/pyagent.py&quot;, line 308, in _run_job
      wandb.finish(exit_code=1)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/wandb_run.py&quot;, line 2374, in finish
      wandb.run.finish(exit_code=exit_code)
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/wandb_run.py&quot;, line 1144, in finish
      if self._wl and len(self._wl._global_run_stack) &gt; 0:
    File &quot;&lt;PATH&gt;/venv/lib/python3.9/site-packages/wandb/sdk/wandb_setup.py&quot;, line 234, in __getattr__
      return getattr(self._instance, name)
  AttributeError: 'NoneType' object has no attribute '_global_run_stack'
  </code></pre>
  <p><strong>Environment</strong></p>
  <ul>
  <li>OS: Manjaro 5.21.5</li>
  <li>Environment: PyCharm Local</li>
  <li>Python Version: 3.9</li>
  </ul>