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

MutablePropertySources will not find or remove proxied sources #25369

Closed
philwebb opened this issue Jul 8, 2020 · 4 comments
Closed

MutablePropertySources will not find or remove proxied sources #25369

philwebb opened this issue Jul 8, 2020 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@philwebb
Copy link
Member

philwebb commented Jul 8, 2020

MutablePropertySources has get and remove methods that look like this:

@Override
@Nullable
public PropertySource<?> get(String name) {
	int index = this.propertySourceList.indexOf(PropertySource.named(name));
	return (index != -1 ? this.propertySourceList.get(index) : null);
}

These attempt to find the index of a property source index by checking for the name. The PropertySource.named method returns a ComparisonPropertySource which depends on the inherited PropertySource equals and hashCode implementations.

The equals method looks like this:

	@Override
	public boolean equals(@Nullable Object other) {
		return (this == other || (other instanceof PropertySource &&
				ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) other).name)));
	}

If you are using a library such as jasypt, it's possible that your property source will be a proxy instance and won't actually contain a populated name field. The equals method could use ((PropertySource<?>) other).getName())) which would solve this issue.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 8, 2020
@philwebb
Copy link
Member Author

philwebb commented Jul 8, 2020

Another, potentially better way to solve this would be to rewrite the MutablePropertySources get method entirely. I believe that it currently could suffer from a race condition where another thread could change the index of the element before this.propertySourceList.get(index) is called.

Since propertySourceList is a CopyOnWriteArrayList array, I suspect it would be just as efficient to use list.toArray() and then search the array directly. Another option might be to use an array directly and replicate what CopyOnWriteArrayList currently does.

@philwebb
Copy link
Member Author

philwebb commented Jul 8, 2020

/cc @smaldini

@philwebb
Copy link
Member Author

philwebb commented Jul 8, 2020

I have a branch here that can be reviewed and cherry-picked if suitable.

philwebb added a commit to philwebb/spring-framework that referenced this issue Jul 8, 2020
Replace the `CopyOnWriteArrayList` in `MutablePropertySources` with
a direct array implementation that we can lock during operations.

Prior to this commit it was possible that the underlying list would
be updated in the middle of certain operations.

Closes spring-projectsgh-25369
@jhoeller jhoeller self-assigned this Jul 8, 2020
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 8, 2020
@jhoeller jhoeller added this to the 5.2.8 milestone Jul 8, 2020
@jhoeller jhoeller added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Jul 17, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Jul 17, 2020
@jhoeller
Copy link
Contributor

After some back and forth, in addition to delegating to the getName() method in PropertySource itself (addressing the main point of this issue), I went with a revision that preserves the CopyOnWriteArrayList, performing manual iteration (relying on the stable COW iterator) in the MutablePropertySources.get and contains implementation... and manually synchronizing on the list in the mutator methods. This seems straightforward enough that it's also worth backporting to 5.1.x. To be committed ASAP.

jhoeller added a commit that referenced this issue Jul 17, 2020
Includes synchronization for mutators on MutablePropertySources.

Closes gh-25369

(cherry picked from commit 01bab89)
FelixFly pushed a commit to FelixFly/spring-framework that referenced this issue Aug 16, 2020
Includes synchronization for mutators on MutablePropertySources.

Closes spring-projectsgh-25369
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
Includes synchronization for mutators on MutablePropertySources.

Closes spring-projectsgh-25369

(cherry picked from commit 01bab89)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants