From 8339fc6061cff7f654970be5ecefc56d19827197 Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Fri, 27 May 2016 21:29:13 +0100 Subject: [PATCH] BROOKLYN-284: fix deadlock in entity initialisation --- .../brooklyn/core/entity/AbstractEntity.java | 5 ++++- .../brooklyn/core/entity/EntityTypeTest.java | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java index bde505cc30..62a4bf8265 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java @@ -1580,7 +1580,10 @@ protected SubscriptionTracker getSubscriptionTracker() { } @Override - public synchronized ExecutionContext getExecutionContext() { + public ExecutionContext getExecutionContext() { + // NB May 2016 - removed synch keyword above due to deadlock (see https://issues.apache.org/jira/browse/BROOKLYN-284). + // As with getManagementContext(), it also synchs in ManagementSupport.getExecutionContext(); + // no apparent reason why it was here also. return getManagementSupport().getExecutionContext(); } diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java index 641f97b2c7..c6e3936dcd 100644 --- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityTypeTest.java @@ -264,7 +264,7 @@ public void testHasSensor() throws Exception { } // Previously EntityDynamicType's constructor when passed `entity` during the entity's construction (!) - // would pass this to EntityDynamicType.findEffectors, which would do log.warn in some cirumstances, + // would pass this to EntityDynamicType.findEffectors, which would do log.warn in some circumstances, // with entity.toString as part of the log message. But if the toString called getConfig() this would // fail because we were still in the middle of constructing the entity.entityType! @Test @@ -272,7 +272,21 @@ public void testEntityDynamicTypeDoesNotCallToStringDuringConstruction() throws entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).impl(EntityWithToStringAccessingConfig.class)); entity.toString(); } - + + /** + * Previously this deadlocked when calling toString() during entity initialisation (but only if the config key + * had a value - this would trigger a call by EntityConfigMap.getConfig() to getExecutionContext). + * + * See https://issues.apache.org/jira/browse/BROOKLYN-284 + */ + @Test + public void testEntityDoesNotCallToStringDuringConstruction() throws Exception { + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) + .impl(EntityWithToStringAccessingConfig.class) + .configure(TestEntity.CONF_NAME, "myval")); + entity.toString(); + } + public static class EntityWithToStringAccessingConfig extends TestEntityImpl { // to cause warning to be logged: non-static constant