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

Synchronize state in AbstractVariable and Argv #8232

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ abstract class AbstractVariable implements BiVariable {
*/
protected final IRubyObject receiver;
protected final String name;
protected Object javaObject = null;
protected Class javaType = null;
protected IRubyObject rubyObject = null;
protected boolean fromRuby;
protected volatile Object javaObject;
protected volatile Class javaType;
protected volatile IRubyObject rubyObject;
protected volatile boolean fromRuby;

/**
* Constructor used when this variable is originaed from Java.
Expand Down Expand Up @@ -94,7 +94,7 @@ static RubyObject getTopSelf(final IRubyObject receiver) {
return (RubyObject) receiver.getRuntime().getTopSelf();
}

protected void updateByJavaObject(final Ruby runtime, Object... values) {
protected synchronized void updateByJavaObject(final Ruby runtime, Object... values) {
assert values != null;
javaObject = values[0];
if (javaObject == null) {
Expand All @@ -108,7 +108,7 @@ protected void updateByJavaObject(final Ruby runtime, Object... values) {
fromRuby = false;
}

protected void updateRubyObject(final IRubyObject rubyObject) {
protected synchronized void updateRubyObject(final IRubyObject rubyObject) {
if ( rubyObject == null ) return;
this.rubyObject = rubyObject;
this.javaType = null;
Expand All @@ -134,7 +134,7 @@ public String getName() {
return name;
}

public Object getJavaObject() {
public synchronized Object getJavaObject() {
if (rubyObject == null) return javaObject;

if (javaType != null) { // Java originated variables
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/embed/variable/Argv.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static boolean isValidName(Object name) {
* invoked during EvalUnit#run() is executed.
*/
@Override
public void inject() {
public synchronized void inject() {
final Ruby runtime = getRuntime();

final RubyArray argv = RubyArray.newArray(runtime);
Expand All @@ -141,7 +141,7 @@ else if ( javaObject instanceof String[] ) {
* this variable in top self.
*/
@Override
public void remove() {
public synchronized void remove() {
this.javaObject = new ArrayList();
inject();
}
Expand Down Expand Up @@ -173,7 +173,7 @@ private static void updateARGV(final IRubyObject receiver, final BiVariableMap v
}

// ARGV appears to require special treatment, leaving javaType intact
protected void updateRubyObject(final IRubyObject rubyObject) {
protected synchronized void updateRubyObject(final IRubyObject rubyObject) {
if ( rubyObject == null ) return;
this.rubyObject = rubyObject;
}
Expand All @@ -193,7 +193,7 @@ public static void retrieveByKey(RubyObject receiver, BiVariableMap vars, String

@Override
@SuppressWarnings("unchecked")
public Object getJavaObject() {
public synchronized Object getJavaObject() {
if ( rubyObject == null || ! fromRuby ) return javaObject;

final RubyArray ary = (RubyArray) rubyObject;
Expand Down