Skip to content

Commit

Permalink
Merge pull request #2207 from malakaganga/fix_dyn
Browse files Browse the repository at this point in the history
Maintaining thread safety for class med properties
  • Loading branch information
malakaganga authored Jul 25, 2024
2 parents 8648562 + a62a96b commit d7930d3
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import org.apache.synapse.mediators.MediatorProperty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The class mediator delegates the mediation to a single instance of a specified
Expand All @@ -54,7 +52,7 @@ public class ClassMediator extends AbstractMediator implements ManagedLifecycle
private final List<MediatorProperty> properties = new ArrayList<MediatorProperty>();

/** Contains Dynamic Expressions/not */
private boolean hasContentAwareProperties = false;
private boolean hasDynamicProperties = false;

/**
* Don't use a new instance... do one instance of the object per instance of
Expand Down Expand Up @@ -89,7 +87,7 @@ public boolean mediate(MessageContext synCtx) {
boolean result;

try {
if (hasContentAwareProperties) {
if (hasDynamicProperties) {
synchronized (mediator) {
result = updateInstancePropertiesAndMediate(synCtx);
}
Expand Down Expand Up @@ -144,11 +142,17 @@ public void addProperty(MediatorProperty property) {

public void addAllProperties(List<MediatorProperty> propertyList) {
properties.addAll(propertyList);

for (MediatorProperty property : properties) {
SynapsePath expression = property.getExpression();
if (expression != null && expression.isContentAware()) {
hasContentAwareProperties = true;
// Since expressions such as expression="get-property('...')" or expression="$trp:..." are also dynamic
// and their values can change with each request, we face a potential race condition when these
// expressions alter class mediator instance variables using setters. Given that only one instance of the
// mediator is created and shared among multiple concurrent requests, this shared state can lead to
// inconsistent behavior and data corruption. Therefore, we need to ensure synchronization not only
// when the content is altered but also for each evaluation of these dynamic expressions to maintain
// thread safety and data integrity.
if (expression != null) {
hasDynamicProperties = true;
break;
}
}
Expand Down

0 comments on commit d7930d3

Please sign in to comment.