davsclaus commented on code in PR #28069:
URL: https://github.com/apache/camel/pull/28069#discussion_r4310716140


##########
components/camel-coap/src/main/java/org/apache/camel/coap/CoAPObserver.java:
##########
@@ -63,13 +65,67 @@ public void onLoad(CoapResponse response) {
         }
     }
 
+    @Override
+    protected void doStop() throws Exception {
+        // cancel the observation, or the server keeps sending notifications 
to this consumer after it was stopped, and
+        // a route that is started again then gets every notification twice 
(once from the stopped consumer)
+        Observation current = observation;
+        observation = null;
+        if (current != null) {
+            current.cancel();
+        }
+        if (client != null) {
+            client.shutdown();
+            client = null;
+        }
+        super.doStop();
+    }
+
     @Override
     public void onError() {
         getExceptionHandler().handleException(new IOException("CoAP request 
timed out or has been rejected by the server"));
-        startObserve();
+        if (isRunAllowed()) {
+            startObserve();
+        }
     }
 
     private void startObserve() {
-        this.client.observe(this);
+        Observation next = new Observation();
+        this.observation = next;
+        next.relation = this.client.observe(next);
+    }

Review Comment:
   A lock-free alternative to the lock: read `client` once, and check again 
after the relation is set. `doStop()` runs after the status became STOPPING, 
and both `status` and `observation`/`relation`/`cancelled` are volatile. So 
either `doStop()` sees this observation with its relation set, or this check 
sees `!isRunAllowed()` / `cancelled` and cancels it. `proactiveCancel()` is 
CAS-guarded, so a double cancel is harmless.
   
   ```suggestion
       private void startObserve() {
           CoapClient current = this.client;
           if (current == null) {
               return;
           }
           Observation next = new Observation();
           this.observation = next;
           next.relation = current.observe(next);
           if (next.cancelled || !isRunAllowed()) {
               // doStop() ran while this observation was being set up
               next.cancel();
           }
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to