@@ -111,7 +111,9 @@ JL_DLLEXPORT void jl_set_newly_inferred(jl_value_t* _newly_inferred)
111111 assert (_newly_inferred == NULL || _newly_inferred == jl_nothing || jl_is_array (_newly_inferred ));
112112 if (_newly_inferred == jl_nothing )
113113 _newly_inferred = NULL ;
114+ JL_LOCK (& newly_inferred_mutex );
114115 newly_inferred = (jl_array_t * ) _newly_inferred ;
116+ JL_UNLOCK (& newly_inferred_mutex );
115117}
116118
117119static jl_array_t * queue_external_cis (jl_array_t * list , jl_query_cache * query_cache );
@@ -138,9 +140,14 @@ JL_DLLEXPORT void jl_push_newly_inferred(jl_value_t* ci)
138140 jl_atomic_store_relaxed (& mi -> flags , miflags | JL_MI_FLAGS_MASK_PRECOMPILED );
139141 }
140142 JL_LOCK (& newly_inferred_mutex );
141- size_t end = jl_array_nrows (newly_inferred );
142- jl_array_grow_end (newly_inferred , 1 );
143- jl_array_ptr_set (newly_inferred , end , ci );
143+ // re-check under the lock: a concurrent jl_set_newly_inferred may have
144+ // cleared or replaced the array since the unlocked fast-path check above
145+ jl_array_t * arr = newly_inferred ;
146+ if (arr != NULL ) {
147+ size_t end = jl_array_nrows (arr );
148+ jl_array_grow_end (arr , 1 );
149+ jl_array_ptr_set (arr , end , ci );
150+ }
144151 JL_UNLOCK (& newly_inferred_mutex );
145152}
146153
0 commit comments