Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

fix race condition which could leave a thing in INITIALIZING #3088

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public ClassLoader run() {
*
* @param object the argument for the action
*/
public void applyActionFor(final T object) {
public void applyActionFor(final Class<?> classFromWatchedBundle, final T object) {
boolean veto = false;
Bundle bundle = getBundle(object.getClass());
Bundle bundle = getBundle(classFromWatchedBundle);
long bundleId = bundle.getBundleId();
for (BundleProcessor proc : bundleProcessors) {
if (!proc.hasFinishedLoading(bundle)) {
Expand All @@ -110,7 +110,7 @@ public void applyActionFor(final T object) {
}
}
if (veto) {
if (!queue.containsEntry(bundle, object)) {
if (!queue.containsEntry(bundleId, object)) {
logger.trace("Queueing '{}' in bundle '{}'", object, bundle.getSymbolicName());
queue.put(bundleId, object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent)
if (oldThing != thing) {
thing.setHandler(thingHandler);
}
if (ThingHandlerHelper.isHandlerInitialized(thing)) {
if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
try {
// prevent infinite loops by not informing handler about self-initiated update
if (!thingUpdatedLock.contains(thingUID)) {
Expand Down Expand Up @@ -615,34 +615,41 @@ private void initializeHandler(Thing thing) {
if (!isHandlerRegistered(thing)) {
return;
}
initializationVetoManager.applyActionFor(thing.getHandler());
synchronized (thing) {
if (ThingHandlerHelper.isHandlerInitialized(thing)) {
logger.debug("Attempt to initialize the already initialized thing '{}' will be ignored.",
thing.getUID());
return;
}
if (isInitializing(thing)) {
logger.debug("Attempt to initialize a handler twice for thing '{}' at the same time will be ignored.",
thing.getUID());
return;
}
if (thing.getHandler().getThing() != thing) {
logger.debug("The model of {} is inconsistent [thing.getHandler().getThing() != thing]",
thing.getUID());
}
ThingType thingType = getThingType(thing);
applyDefaultConfiguration(thing, thingType);
if (isInitializable(thing, thingType)) {
setThingStatus(thing, buildStatusInfo(ThingStatus.INITIALIZING, ThingStatusDetail.NONE));
doInitializeHandler(thing.getHandler());
} else {
logger.debug("Thing '{}' not initializable, check required configuration parameters.", thing.getUID());
setThingStatus(thing,
buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING));
}
}
}

private final BundleProcessorVetoManager<ThingHandler> initializationVetoManager = new BundleProcessorVetoManager<>(
new BundleProcessorVetoManager.Action<ThingHandler>() {
private final BundleProcessorVetoManager<Thing> initializationVetoManager = new BundleProcessorVetoManager<>(
new BundleProcessorVetoManager.Action<Thing>() {
@Override
public void apply(ThingHandler thingHandler) {
Thing thing = thingHandler.getThing();
synchronized (thing) {
if (!isInitializing(thing)) {
ThingType thingType = getThingType(thing);
applyDefaultConfiguration(thing, thingType);
if (isInitializable(thing, thingType)) {
setThingStatus(thing,
buildStatusInfo(ThingStatus.INITIALIZING, ThingStatusDetail.NONE));
doInitializeHandler(thingHandler);
} else {
logger.debug("Thing '{}' not initializable, check required configuration parameters.",
thing.getUID());
setThingStatus(thing, buildStatusInfo(ThingStatus.UNINITIALIZED,
ThingStatusDetail.HANDLER_CONFIGURATION_PENDING));
}
} else {
logger.debug(
"Attempt to initialize a handler twice for thing '{}' at the same time will be ignored.",
thing.getUID());
}
}
public void apply(final Thing thing) {
final ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
registerHandler(thing, thingHandlerFactory);
initializeHandler(thing);
}
});

Expand Down Expand Up @@ -967,8 +974,7 @@ protected void addThingHandlerFactory(ThingHandlerFactory thingHandlerFactory) {

private void registerAndInitializeHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
if (thingHandlerFactory != null) {
registerHandler(thing, thingHandlerFactory);
initializeHandler(thing);
initializationVetoManager.applyActionFor(thingHandlerFactory.getClass(), thing);
} else {
logger.debug("Not registering a handler at this point since no handler factory for thing '{}' found.",
thing.getUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.smarthome.model.thing.tests"/>
Expand Down
Loading