@@ -216,6 +216,12 @@ private static final class AtExitHook {
216
216
/* for fast access to the PythonThreadState object by the owning thread */
217
217
private final ContextThreadLocal <PythonThreadState > threadState ;
218
218
219
+ /*
220
+ * Workaround for GR-30072 - we cannot use the threadState above during native image build time,
221
+ * so we use a temporary one just for the build
222
+ */
223
+ private final PythonThreadState buildThreadState ;
224
+
219
225
/* map of thread IDs to indices for array 'threadStates' */
220
226
private Map <Thread , PythonThreadState > threadStateMapping = Collections .synchronizedMap (new WeakHashMap <>());
221
227
@@ -257,6 +263,11 @@ private static final class AtExitHook {
257
263
258
264
public PythonContext (PythonLanguage language , TruffleLanguage .Env env , Python3Core core , ContextThreadLocal <PythonThreadState > threadState ) {
259
265
this .language = language ;
266
+ if (ImageInfo .inImageBuildtimeCode ()) {
267
+ this .buildThreadState = new PythonThreadState (this , Thread .currentThread ());
268
+ } else {
269
+ this .buildThreadState = null ;
270
+ }
260
271
this .threadState = threadState ;
261
272
this .core = core ;
262
273
this .env = env ;
@@ -978,7 +989,7 @@ void acquireGil() {
978
989
try {
979
990
globalInterpreterLock .lockInterruptibly ();
980
991
} catch (InterruptedException e ) {
981
- if (threadState .get ().isShuttingDown ()) {
992
+ if (! ImageInfo . inImageBuildtimeCode () && threadState .get ().isShuttingDown ()) {
982
993
// This is a thread being killed during normal context shutdown. This thread
983
994
// should exit now. This should usually only happen for daemon threads on
984
995
// context shutdown. This is the equivalent to the logic in pylifecycle.c and
@@ -1096,7 +1107,7 @@ public Thread[] getThreads() {
1096
1107
}
1097
1108
1098
1109
private PythonThreadState getThreadState () {
1099
- PythonThreadState curThreadState = threadState . get ();
1110
+ PythonThreadState curThreadState = getThreadStateInternal ();
1100
1111
if (curThreadState .isShuttingDown ()) {
1101
1112
// we're shutting down, just release and die
1102
1113
if (ownsGil ()) {
@@ -1107,9 +1118,17 @@ private PythonThreadState getThreadState() {
1107
1118
return curThreadState ;
1108
1119
}
1109
1120
1121
+ private PythonThreadState getThreadStateInternal () {
1122
+ if (ImageInfo .inImageBuildtimeCode ()) {
1123
+ return buildThreadState ;
1124
+ } else {
1125
+ return threadState .get ();
1126
+ }
1127
+ }
1128
+
1110
1129
private void applyToAllThreadStates (Consumer <PythonThreadState > action ) {
1111
1130
if (language .singleThreadedAssumption .isValid ()) {
1112
- action .accept (threadState . get ());
1131
+ action .accept (getThreadStateInternal ());
1113
1132
} else {
1114
1133
synchronized (this ) {
1115
1134
for (PythonThreadState ts : threadStateMapping .values ()) {
0 commit comments