title: Solving STOMP Error "There is no current subscription with tag 'T-...''' in RabbitMQ
body: <p>I'm facing the error message 'There is no current subscription with tag <code>'T_1'</code>. Due to this error, my connections are terminated which decreases the performance of my system.
  First of, the environment I'm running:</p>
  <ol>
  <li>Client app (Created in c#)</li>
  <li>Server app (Created with spring boot version 2.6.0)</li>
  <li>RabbitMQ (3.9.11) as message broker. Using STOMP plugin.</li>
  </ol>
  <p>These 3 services works great in general. I'm not having any issues with these besides the error message described above (There is no current subscription with tag <code>'T_1'</code>). Please also note that the tag can be any subscription ID, not neccesairy <code>T_1</code>, but also e.g <code>T_100</code></p>
  <p><strong>When does the error occur:</strong>
  I've noticed that the error usually occurs during a particular state of my program. In this state, the C# client app should subscribe to a topic named 'xxx.pong'. Empty messages are frequently sent to this topic by other client applications. Once retrieved a pong message, the C# client app should unsubscribe to this topic.</p>
  <p><strong>How I replicated the issue:</strong>
  I created a test case similar to the description above. I've added the code as an attachment. The error occurs around 1 out of 10 test runs and it's the consumer (which subscribes and unsubscribes) that retrieves the error message. The publisher (which frequently sends pong messages) does not retrieve errors.</p>
  <p><strong>Logs when the error occurs:</strong></p>
  <p><strong>Spring boot log:</strong></p>
  <pre><code>2022-01-06 14:04:12.165 ERROR 25116 --- [ent-scheduler-7] o.s.m.s.s.StompBrokerRelayMessageHandler : Received ERROR {message=[Subscription not found], content-type=[text/plain], version=[1.0,1.1,1.2], content-length=[48]} session=0009197b-1281-0fdb-08fe-3f5de331fdb9, user=XXX) text/plain payload=There is no current subscription with tag 'T_1'.
  </code></pre>
  <p><strong>RabbitMQ log (Note that the RabbitMQ serves timezone is 1 hour behind my local PC zone):</strong></p>
  <pre><code>2022-01-06 13:04:06.450156+00:00 [info] &lt;0.9710.0&gt; Supervisor {&lt;0.9710.0&gt;,rabbit_connection_helper_sup}: child heartbeat_sender started (&lt;0.9728.0&gt;): {rabbit_heartbeat,start_heartbeat_sender,[#Port&lt;0.1398&gt;,10,#Fun&lt;rabbit_stomp_reader.1.31184097&gt;,{heartbeat_sender,unknown}]}
  2022-01-06 13:04:06.450237+00:00 [info] &lt;0.9710.0&gt; Supervisor {&lt;0.9710.0&gt;,rabbit_connection_helper_sup}: child heartbeat_receiver started (&lt;0.9729.0&gt;): {rabbit_heartbeat,start_heartbeat_receiver,[#Port&lt;0.1398&gt;,55,#Fun&lt;rabbit_stomp_reader.2.31184097&gt;,{heartbeat_receiver,unknown}]}
  2022-01-06 13:04:11.359597+00:00 [info] &lt;0.9733.0&gt; Supervisor {&lt;0.9733.0&gt;,rabbit_amqqueue_sup}: child rabbit_amqqueue started (&lt;0.9734.0&gt;): {rabbit_prequeue,start_link,[{amqqueue,{resource,&lt;&lt;&quot;/&quot;&gt;&gt;,queue,&lt;&lt;&quot;stomp-subscription-gItDy9sxnW3_TUCGLOs8PQ&quot;&gt;&gt;},false,true,none,[],none,[],[],[],undefined,undefined,[],[],live,0,[],&lt;&lt;&quot;/&quot;&gt;&gt;,#{user =&gt; &lt;&lt;&quot;guest&quot;&gt;&gt;},rabbit_classic_queue,#{}},declare,&lt;0.9732.0&gt;]}
  2022-01-06 13:04:11.403234+00:00 [dbug] &lt;0.9734.0&gt; Deleting auto-delete queue 'stomp-subscription-gItDy9sxnW3_TUCGLOs8PQ' in vhost '/' because its last consumer with tag 'T_1' was cancelled
  2022-01-06 13:04:11.412333+00:00 [info] &lt;0.9687.0&gt; closing STOMP connection &lt;0.9687.0&gt; (172.17.0.1:57136 -&gt; 172.17.0.2:61613)
  2022-01-06 13:04:11.412428+00:00 [dbug] &lt;0.9741.0&gt; Closing all channels from connection '172.17.0.1:57136 -&gt; 172.17.0.2:61613' because it has been closed
  </code></pre>
  <p>This is the test which sometimes invokes the error:</p>
  <pre><code>using Microsoft.VisualStudio.TestTools.UnitTesting;
  
  namespace MyProject.Rabbitmq
  {
      [TestClass]
      public class RabbitMqTests
      {
          private int _pongCounter;
          private int _pingCounter;
          private bool shouldStop = false;
  
          [TestMethod]
          public void TestTags()
          {
              var internalWs = new InternalWebsocket(&quot;Consumer&quot;);
              internalWs.Open();
  
              Thread.Sleep(10000);
  
              List&lt;StompSubscription&gt; subscriptions = new List&lt;StompSubscription&gt;();
  
              var task = new Task(() =&gt; SpamPong(), TaskCreationOptions.LongRunning);
              task.Start();
  
              Thread.Sleep(10000);
  
              var sub = internalWs.Subscribe(
                  &quot;xxx.pong&quot;,
                  OnPongMessage, true);
  
              while (_pongCounter == 0)
              {
                  Thread.Sleep(1);
              }
  
              internalWs.Unsubscribe(sub);
  
              Thread.Sleep(10000);
          }
  
          private void SpamPong()
          {
              var internalWs = new InternalWebsocket(&quot;producer&quot;);
              internalWs.Open();
  
              Thread.Sleep(10000);
  
  
              while (!shouldStop)
              {
                  try
                  {
                      internalWs.Send(new object(), &quot;xxx.pong&quot;);
                  }
                  catch (Exception e)
                  {
                      Logger.Log.Info(&quot;Has failed to send message&quot;);
                  }
  
                  Thread.Sleep(30);
              }
          }
  
  
          public virtual void OnPongMessage(object sender, InternalMessageEventArgs args)
          {
              Interlocked.Increment(ref _pongCounter);
          }
  
          public virtual void OnPingMessage(object sender, InternalMessageEventArgs args)
          {
              Interlocked.Increment(ref _pingCounter);
          }
      }
  </code></pre>