Skip to content

Scripted beans not updated after a script compilation error in getScriptedObjectType [SPR-14007] #18579

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

Closed
spring-projects-issues opened this issue Mar 1, 2016 · 2 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: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 1, 2016

Anton Rokhmistrov opened SPR-14007 and commented

This is a duplicate of #9034.
It does not work again because "ScriptFactory.requiresScriptedObjectRefresh(ScriptSource)" is not used in "GroovyScriptFactory#getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)"


Affects: 3.2.16, 4.2.2, 4.2.5

Issue Links:

Referenced from: commits c2eb5e1, 0597ff1, 05ab769

Backported to: 3.2.17

2 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

Anton Rokhmistrov commented

I think come clarification is needed because it's not exactly the duplicate.

The problem happens when groovy script is invalid (syntax error etc.). Then result of a call to the groovy bean is unpredictable, it can execute old version of the script or throw an exception depending on whether AbstractApplicationContext.getBeansOfType() was called or not.

Example scenario:
Call to getScriptedObjectType

  1. scriptSource.lastModified is updated
  2. getGroovyClassLoader().parseClass throws CompilationException [1]
  3. this.scriptClass is not updated

Call to getScriptedObject

  1. scriptSource.isModified() return false [2]
  2. returns outdated class [3]
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException {
....
if (this.scriptClass == null || scriptSource.isModified()) {
	// New script content...
	this.wasModifiedForTypeCheck = true;
	this.scriptClass = getGroovyClassLoader().parseClass( [1]
				scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

	if (Script.class.isAssignableFrom(this.scriptClass)) {
			// A Groovy script, probably creating an instance: let's execute it.
			Object result = executeScript(scriptSource, this.scriptClass);
			this.scriptResultClass = (result != null ? result.getClass() : null);
			this.cachedResult = new CachedResultHolder(result);
	}
	else {
		this.scriptResultClass = this.scriptClass;
	}
}
....
}
@Override
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
			throws IOException, ScriptCompilationException {
    if (this.scriptClass == null || scriptSource.isModified()) { [2]
       // New script content...
       this.scriptClass = getGroovyClassLoader().parseClass(
			scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

	if (Script.class.isAssignableFrom(this.scriptClass)) {
	        // A Groovy script, probably creating an instance: let's execute it.
		Object result = executeScript(scriptSource, this.scriptClass);
		this.scriptResultClass = (result != null ? result.getClass() : null);
		return result;
	}
	else {
		this.scriptResultClass = this.scriptClass;
	}
	}
	scriptClassToExecute = this.scriptClass; [3]
}

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good point: We're resetting the scriptClass, scriptResultClass and cachedResult fields in GroovyScriptFactorty when we run into a CompilationFailedException now, both in getScriptedObject and getScriptedObjectType. Analogously in BshScriptFactory as well.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: backported An issue that has been backported to maintenance branches in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.2.6 milestone Jan 11, 2019
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

2 participants