Repository: lanpa/tensorboardX (language: python)

Issue:
AttributeError: 'NoneType' object has no attribute 'log_metric'
I have no idea about the following error, looking forward to someone's help, thank you! ~~

```
Traceback (most recent call last):
  File "disfluency_detection/pipelines/sentseg_train.py", line 427, in <module>
    main()
  File "disfluency_detection/pipelines/sentseg_train.py", line 394, in main
    metrics = trainer.evaluate()
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer.py", line 2044, in evaluate
    self.log(output.metrics)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer.py", line 1709, in log
    self.control = self.callback_handler.on_log(self.args, self.state, self.control, logs)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer_callback.py", line 371, in on_log
    return self.call_event("on_log", args, state, control, logs=logs)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer_callback.py", line 388, in call_event
    **kwargs,
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/integrations.py", line 354, in on_log
    self.tb_writer.add_scalar(k, v, state.global_step)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/tensorboardX/writer.py", line 453, in add_scalar
    self.comet_logger.log_metric(tag, display_name, scalar_value, global_step)
AttributeError: 'NoneType' object has no attribute 'log_metric'
```


Discussion / hints:
I have found the solution to this problem in [one transformers issue](https://github.com/huggingface/transformers/issues/12962)
Thanks for the reporting, seems that it's caused by the new introduced feature. Let me reopen this issue for tracking.
The error still persists! The commit didn't fix it.
same problem
@cdhx I found the error for my code - as I was using Jupyter notebook, I didn't realize I closed the writer and then tried to write again in another cell.
@cdhx @CyanideBoy  Hi, which version are you using?
I used  `with summary_writer:summary_writer.add_graph( )`  before  `.add_scalar( )` 
After deleting `with summary_writer: `,  my code could get correct output.

I encountered a similar problem: 'NoneType' object has no attribute 'log_metrics'. 
The code works fine after deleting `with writer:` like **grafaraway** suggested. 
My package version:
pytorch                     1.8.2               py3.8_cpu_0  [cpuonly]  pytorch-lts
tensorboardx              2.4                pyhd8ed1ab_0    conda-forge
@mwck46 Can you provide a code snippet to reproduce the error with `with...`? Thank you.
@lanpa hope this help
```
import torch
import torch.nn as nn
import torch.utils.data as dset
from torchvision import datasets, transforms
from tensorboardX import SummaryWriter  

dataset_path = "./data"
log_path = "./log"
device = "cpu"

class ANNModel(nn.Module):
    def __init__(self):
        super(ANNModel, self).__init__()
        self.main = nn.Sequential(
            nn.Linear(in_features=28*28, out_features=128),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=10),
            nn.LogSoftmax(dim=1))
    def forward(self, input):
        return self.main(input)

def load_data(batch_size, transform):
    trainset = datasets.MNIST(root=dataset_path, download=True, train=True, transform=transform)
    testset = datasets.MNIST(root=dataset_path, download=True, train=False, transform=transform)
    trainloader = dset.DataLoader(trainset, batch_size=batch_size, shuffle=True)
    testloader = dset.DataLoader(testset, batch_size=batch_size, shuffle=False)
    return trainloader, testloader

def main():
    writer = SummaryWriter(log_path)
    
    transform = transforms.Compose([transforms.ToTensor(),])
    trainloader, testloader = load_data(batch_size = 64, transform=transform)
    model = ANNModel().to(device)
    train_data_sample, _ = iter(trainloader).next()
    
    with writer:
        train_data_sample = train_data_sample.view(train_data_sample.shape[0], -1)
        writer.add_graph(model, train_data_sample.to(device))  # model graph, with input

    epochs = 3
    for epoch in range(epochs):
        epoch_loss = 0.1
        
        writer.add_scalars("Loss/Epoch", {"train": epoch_loss}, epoch)
        print("epoch[%02d/%02d] | training loss: %.4f" % (epoch + 1, epochs, epoch_loss))

if __name__ == "__main__":
    main()
```

My conda env [env.txt](https://github.com/lanpa/tensorboardX/files/7520547/env.txt)

@mwck46 According to your `env.txt`, maybe you are using the previous tensorboardX (v2.4) from conda-forge
Will `pip install 'git+https://github.com/lanpa/tensorboardX'` resolve your problem?


Hi everyone
I got the same error with tensorboard 2.4. someone could help me to solve it, please?
I appreciate your time 
@DrraBL Can you try whether pip install `git+https://github.com/lanpa/tensorboardX` helps?

Fix patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 8cad0fd..938bf89 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,5 +1,9 @@
 History
 =======
+2.4.1 (2021-11-20)
+-------------------
+* Fix a comet plugin bug if writer is reused. (#639)
+
 2.4 (2021-06-30)
 -----------------
 * Remove a dependency issue. (#631)
diff --git a/setup.py b/setup.py
index a817b49..861f086 100644
--- a/setup.py
+++ b/setup.py
@@ -32,7 +32,7 @@ def run(self):
     history = history_file.read()
 
 preparing_PyPI_package = 'sdist' in sys.argv or 'bdist_wheel' in sys.argv
-version_git = version = '2.4'
+version_git = version = '2.4.1'
 
 if not preparing_PyPI_package:
     if os.path.exists('.git'):
@@ -96,5 +96,4 @@ def run(self):
 # git commit -m 'prepare for release'
 # add tag
 # twine upload dist/*
-# git push
-
+# git push -u origin HEAD


Test patch:
diff --git a/tests/test_writer.py b/tests/test_writer.py
index 992bb76..cf1ed53 100644
--- a/tests/test_writer.py
+++ b/tests/test_writer.py
@@ -71,6 +71,12 @@ def test_flush(self):
     def test_auto_close(self):
         pass
 
+    def test_reuse_writer_after_closed(self):
+        w = SummaryWriter()
+        w.close()
+        w.add_text("reuse writer", "dont reuse without creating a new writer", 0)
+
+
     def test_writer(self):
         with SummaryWriter() as writer:
             sample_rate = 44100