Closed
Bug 204454
Opened 23 years ago
Closed 23 years ago
Exception 'No Context associated with current Thread' while throwing an exception in ScriptRuntime.getProp
Categories
(Rhino Graveyard :: Core, defect)
Tracking
(Not tracked)
VERIFIED
INVALID
1.5R5
People
(Reporter: bc1-bugzilla, Assigned: norrisboyd)
References
()
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4a; MultiZilla v1.4.0.4A) Gecko/20030401
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4a; MultiZilla v1.4.0.4A) Gecko/20030401
There seems to be a bug (or a feature?) in ScriptRuntime.getProp.
I have a script, that defines a function, i.e.
<some script>
...
function a(b) {
...
-- some error ---
}
...
</some script>
This function is later on assigned to a field in a java class:
Java class:
protected Function f;
public jsSet_f(Function f) {
this.f = f;
}
Javascript code:
myClass.f = a;
The context is created, the code is executed, the function is assigned. The
object stays in memory.
Later on, a new request comes along, in a new thread.
The function is executed in the java class file:
f.call(cx, scope, js_wizard, args);
Somewhere along this happens:
Scriptable start;
if (obj instanceof Scriptable) {
start = (Scriptable) obj;
} else {
start = toObject(scope, obj);
}
if (start == null || start == Undefined.instance) {
String msg = start == null ? "msg.null.to.object"
: "msg.undefined";
throw NativeGlobal.constructError(
Context.getContext(), "ConversionError",
ScriptRuntime.getMessage0(msg),
scope);
}
Object result = ScriptableObject.getProperty(start, id);
if (result != Scriptable.NOT_FOUND)
return result;
return Undefined.instance;
It seems that an error happens and that NativeGlobal.constructError is called.
But, native global calls Context.getContext(), which throws another error,
java.lang.RuntimeException: No Context associated with current Thread, and the
real error is hidden from the user.
Reproducible: Always
Steps to Reproduce:
See details.
Actual Results:
java.lang.RuntimeException: No Context associated with current Thread
at org.mozilla.javascript.Context.getContext(Context.java:1802)
at org.mozilla.javascript.ScriptRuntime.toObject(ScriptRuntime.java:489)
at org.mozilla.javascript.ScriptRuntime.getProp(ScriptRuntime.java:713)
at org.mozilla.javascript.gen.c14.call(183:83)
at com.parsek.forms.impl.WizardImpl.exitPage(WizardImpl.java:479)
Expected Results:
throw NativeGlobal.constructError(
Context.getContext(), "ConversionError",
ScriptRuntime.getMessage0(msg),
scope);
A bit later establishing, that probable cause of error is the following combination:
Classes:
public class SelectPageEvent extends WizardEvent {
public Page fromPage = null;
public Page toPage = null;
public SelectPageEvent(Object sender, Map context) {
super(sender, context);
}
}
public class WizardEvent {
public Object sender = null;
public Map context = null;
public WizardEvent(Object sender, Map context) {
this.sender = sender;
this.context = context;
}
}
Calling code:
SelectPageEvent spe = new SelectPageEvent(newPage, context);
spe.fromPage = oldPage;
spe.toPage = newPage;
Object args[] = {spe};
newPage.getOnEnter().call(cx, scope, js_wizard, args);
everything dies because:
if (start == null || start == Undefined.instance) {
So, my guess is that start is null. Is this because one of the parameters
(fromPage, toPage) is null or something else? Sadly I yet have not so much
in-depth knowledge of Rhino to say where's the problem.
Comment 2•23 years ago
|
||
cc'ing Igor -
Comment 3•23 years ago
|
||
Contex instances should always be used according to the pattern:
Context cx = Context.enter()
try {
...
} finally {
Context.exit();
}
They are not allowed to be passed between threads. If you need Context instance
on another thread, simple create one when necessary.
Marking as invalid.
Status: NEW → RESOLVED
Closed: 23 years ago
Resolution: --- → INVALID
Let me try explaining how I use the scripting:
I have a service running on an application server. The code is first accessed,
it reads a script from a text, creates new context and executes it. It returns
something to the client.
Later on, the client posts next request. Now, this request requires, that some
javascript events be fired, that have been assigned to a java field (of type
Function).
I call this functions (or events) as needed.
The context in which the functions are running must be the same; I cannot just
'forget' any global variables that the user might have set in the previous
iteration.
Furthermore, since thread pooling is controled by the application server, I
cannot guarantee that the Function will be called in the same thread.
Or is there any way to "store" the state of the context in one thread and read
it in in another thread?
Comment 6•23 years ago
|
||
Context does NOT store any variables or functions! This is what a scope does and
it is perfectly OK to share it between threads. I suggest to read
http://mozilla.org/rhino/scopes.html for details, but the following is a simple
way to initialize / use scope that would work in that case:
Scriptable scopeForWorkWithClient;
1. Initialization for the given client:
Context cx = Context.enter();
try {
scopeForWorkWithClient = cx.initStandardObjects(null);
// populate scopeForWorkWithClient with custom objects
} finally {
Context.exit();
}
2. Evaluation of a script related to that client on any thread:
Context cx = Context.enter();
try {
cx.evaluateString(scopeForWorkWithClient, ...);
} finally {
Context.exit();
}
You need to log in
before you can comment on or make changes to this bug.
Description
•