Skip to content

Commit 68ec965

Browse files
committed
fix starting multiple threads again
1 parent db8eac0 commit 68ec965

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
127127

128128
private static final Object[] CONTEXT_INSENSITIVE_SINGLETONS = new Object[]{PNone.NONE, PNone.NO_VALUE, PEllipsis.INSTANCE, PNotImplemented.NOT_IMPLEMENTED};
129129

130+
/*
131+
* We need to store this here, because the check is on the language and can
132+
* come from a thread that has no context, but we enable or disable threads
133+
* with a context option. So we store this here when a context is created.
134+
*/
135+
private Boolean isWithThread = null;
136+
130137
public static int getNumberOfSpecialSingletons() {
131138
return CONTEXT_INSENSITIVE_SINGLETONS.length;
132139
}
@@ -159,14 +166,14 @@ protected void finalizeContext(PythonContext context) {
159166
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
160167
// internal sources were marked during context initialization
161168
return (firstOptions.get(PythonOptions.ExposeInternalSources).equals(newOptions.get(PythonOptions.ExposeInternalSources)) &&
169+
// we cache WithThread on the lanugage
170+
firstOptions.get(PythonOptions.WithThread).equals(newOptions.get(PythonOptions.WithThread)) &&
162171
// we cache CatchAllExceptions hard on TryExceptNode
163172
firstOptions.get(PythonOptions.CatchAllExceptions).equals(newOptions.get(PythonOptions.CatchAllExceptions)));
164173
}
165174

166175
private boolean areOptionsCompatibleWithPreinitializedContext(OptionValues firstOptions, OptionValues newOptions) {
167176
return (areOptionsCompatible(firstOptions, newOptions) &&
168-
// we cache WithThread in SysConfigModuleBuiltins
169-
firstOptions.get(PythonOptions.WithThread).equals(newOptions.get(PythonOptions.WithThread)) &&
170177
// disabling TRegex has an effect on the _sre Python functions that are
171178
// dynamically created
172179
firstOptions.get(PythonOptions.WithTRegex).equals(newOptions.get(PythonOptions.WithTRegex)));
@@ -186,6 +193,8 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
186193

187194
@Override
188195
protected PythonContext createContext(Env env) {
196+
assert this.isWithThread == null || this.isWithThread == PythonOptions.isWithThread() : "conflicting thread options in the same language!";
197+
this.isWithThread = PythonOptions.isWithThread(env);
189198
ensureHomeInOptions(env);
190199
Python3Core newCore = new Python3Core(new PythonParserImpl());
191200
return new PythonContext(this, env, newCore);
@@ -614,7 +623,7 @@ protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
614623
if (singleThreaded) {
615624
return super.isThreadAccessAllowed(thread, singleThreaded);
616625
}
617-
return PythonOptions.isWithThread();
626+
return isWithThread;
618627
}
619628

620629
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.oracle.graal.python.PythonLanguage;
2929
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3030
import com.oracle.truffle.api.Option;
31+
import com.oracle.truffle.api.TruffleLanguage.Env;
3132

3233
import org.graalvm.options.OptionCategory;
3334
import org.graalvm.options.OptionDescriptors;
@@ -162,6 +163,11 @@ public static <T> T getOption(PythonContext context, OptionKey<T> key) {
162163
return context.getOptions().get(key);
163164
}
164165

166+
@TruffleBoundary
167+
public static <T> T getOption(Env env, OptionKey<T> key) {
168+
return env.getOptions().get(key);
169+
}
170+
165171
@TruffleBoundary
166172
public static int getIntOption(PythonContext context, OptionKey<Integer> key) {
167173
if (context == null) {
@@ -202,6 +208,10 @@ public static boolean isWithThread() {
202208
return getOption(PythonLanguage.getContextRef().get(), WithThread);
203209
}
204210

211+
public static boolean isWithThread(Env env) {
212+
return getOption(env, WithThread);
213+
}
214+
205215
public static boolean getEnableForcedSplits() {
206216
return getOption(PythonLanguage.getContextRef().get(), EnableForcedSplits);
207217
}

graalpython/lib-graalpython/_thread.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def _set_sentinel():
5555

5656

5757
def load():
58-
import sys
59-
filename = sys.graal_python_stdlib_home + ("/_dummy_thread.py")
60-
_dummy_thread = __import__(filename, "_thread")
6158
if not _sysconfig.get_config_vars().get('WITH_THREAD'):
59+
import sys
60+
filename = sys.graal_python_stdlib_home + ("/_dummy_thread.py")
61+
_dummy_thread = __import__(filename, "_thread")
6262
sys.modules["_thread"] = _dummy_thread
6363
load()
6464
del load

0 commit comments

Comments
 (0)