Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport: error handling for unexpected error in DefaultRegistrationEngine #944

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.observer.LwM2mClientObserverAdapter;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.listener.ObjectsListenerAdapter;
Expand Down Expand Up @@ -691,6 +692,7 @@ public void objectAdded(LwM2mObjectEnabler object) {
LOG.info("Object {} enabled.", object.getId());
}
});
client.addObserver(new ShutdownOnUnexpectedErrorObserver(client));

// Display client public key to easily add it in demo servers.
if (clientPublicKey != null) {
Expand Down Expand Up @@ -807,4 +809,18 @@ public void run() {
}
}
}

private static class ShutdownOnUnexpectedErrorObserver extends LwM2mClientObserverAdapter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I'm not too boring with this but are you sure we should keep the backward compatibility behavior ?
As I said in #941 (comment), we could maybe consider the current behavior as a bug rather as a behavior which should not be changed ?

WDYT ?

Of course if you insist we can only add this behavior to LeshanClientDemo but it's hard to me to find why keeping a kind of process zombie alive should be a good idea.(#933 (comment))

Copy link
Contributor Author

@moznion moznion Dec 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this has been a point for consideration.

The previous pull-request (#941) was for the new major version 2.0, on the other hand, this pull-request aims to update the current stable version. So I'm a little bit afraid of breaking the backward compatibility...
But, as you mentioned, it is difficult to find out the reason to encourage keeping the living-dead client process.

This is just my opinion, it would be better to set the hook in LeshanClinet; but anyway I'd like to follow and respect the decision of this project. Let's do the same thing with #941?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so I cherrypick the commit from master instead of the one in this PR.

final LeshanClient client;

public ShutdownOnUnexpectedErrorObserver(final LeshanClient client) {
this.client = client;
}

@Override
public void onUnexpectedError(Throwable unexpectedError) {
LOG.error("unexpected error occurred. destroy the leshan-client", unexpectedError);
client.destroy(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mResource;
Expand All @@ -23,17 +24,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyDevice extends BaseInstanceEnabler {
public class MyDevice extends BaseInstanceEnabler implements Destroyable {

private static final Logger LOG = LoggerFactory.getLogger(MyDevice.class);

private static final Random RANDOM = new Random();
private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 9, 10, 11, 13, 14, 15, 16, 17, 18,
19, 20, 21);

private final Timer timer;

public MyDevice() {
// notify new date each 5 second
Timer timer = new Timer("Device-Current Time");
timer = new Timer("Device-Current Time");
timer.schedule(new TimerTask() {
@Override
public void run() {
Expand Down Expand Up @@ -209,4 +212,9 @@ private long getMemoryTotal() {
public List<Integer> getAvailableResourceIds(ObjectModel model) {
return supportedResources;
}

@Override
public void destroy() {
timer.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.response.ExecuteResponse;
import org.eclipse.leshan.core.response.ReadResponse;
import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RandomTemperatureSensor extends BaseInstanceEnabler {
public class RandomTemperatureSensor extends BaseInstanceEnabler implements Destroyable {

private static final Logger LOG = LoggerFactory.getLogger(RandomTemperatureSensor.class);

Expand Down Expand Up @@ -113,4 +114,9 @@ private void resetMinMaxMeasuredValues() {
public List<Integer> getAvailableResourceIds(ObjectModel model) {
return supportedResources;
}

@Override
public void destroy() {
scheduler.shutdown();
}
}