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

Unsafe double-checked locking in SpelExpression#compileExpression #24265

Closed
PascalSchumacher opened this issue Dec 25, 2019 · 4 comments
Closed
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: bug A general bug
Milestone

Comments

@PascalSchumacher
Copy link
Contributor

PascalSchumacher commented Dec 25, 2019

The double-checked locking SpelExpression#compileExpression does not seem to be thread safe, because compiledAst is not volatile, see:

https://github.com/spring-projects/spring-framework/blob/master/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelExpression.java#L506

Or am I missing something?

Edit:
Also SpelExpression#compileExpression uses this.expression (a String) as a lock object. Shouldn't this be avoided, as Strings objects are shared throughout the program?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 25, 2019
@sbrannen
Copy link
Member

@aclement, can you please take a look at this?

@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Dec 26, 2019
@aclement
Copy link
Contributor

aclement commented Jan 6, 2020

Yep @PascalSchumacher I think you are right. Syncing on string has a chance to cause trouble (but I'm not aware of anyone reporting it actually did). Also, I think ideally we should make compiledAst volatile - although I don't think that causes us breaking issues other than not being optimal in this case (I am not an expert on java concurrency though!). We just might compile the same expression twice (the latest one winning), but It think there is no harm in that other than creating an unnecessary duplicate class representing the compiled expression, things shouldn't blow up.

Want to submit a PR?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jan 7, 2020
@jhoeller jhoeller self-assigned this Jan 7, 2020
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 7, 2020
@jhoeller jhoeller added this to the 5.2.3 milestone Jan 7, 2020
@jhoeller
Copy link
Contributor

jhoeller commented Jan 7, 2020

@aclement @PascalSchumacher Thanks for the report and for the analysis! I'll take care of this, aligning it with similar arrangements in other parts of the codebase, and also backporting it. So no need for a PR, however, happy to take any further insight into account...

@jhoeller jhoeller changed the title Unsafe double-checked locking in SpelExpression#compileExpression? Unsafe double-checked locking in SpelExpression#compileExpression Jan 7, 2020
@jhoeller jhoeller added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Jan 7, 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 Jan 7, 2020
@PascalSchumacher
Copy link
Contributor Author

PascalSchumacher commented Jan 7, 2020

@aclement @jhoeller Thank you very much for looking into this! (I do not have an CLA with Pivotal so I wasn't planning to submit a pull request.)

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: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants