Skip to content

Commit fb7b0a7

Browse files
committed
Do not allow reuse of C api libraries because dlclose is not reliable
1 parent 376c67d commit fb7b0a7

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/NativeLibraryLocator.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
import java.io.IOException;
4949
import java.nio.file.StandardOpenOption;
50-
import java.util.concurrent.atomic.AtomicLong;
50+
import java.util.concurrent.atomic.AtomicInteger;
5151

5252
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ApiInitException;
5353
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ImportException;
@@ -67,11 +67,14 @@
6767
* guaranteed to work with the matching GraalPy version.
6868
*/
6969
public final class NativeLibraryLocator {
70+
private static final int MAX_CEXT_COPIES = 64;
71+
7072
/**
7173
* Bitset for which copied C extension to use when {@link PythonOptions#IsolateNativeModules} is
7274
* enabled.
7375
*/
74-
private static AtomicLong CEXT_COPY_INDICES = new AtomicLong(-1);
76+
private static final AtomicInteger CEXT_COPY_INDICES = new AtomicInteger(MAX_CEXT_COPIES);
77+
7578

7679
/**
7780
* The suffix to add to C extensions when loading. This allows us to support native module
@@ -101,9 +104,10 @@ public final class NativeLibraryLocator {
101104
public NativeLibraryLocator(PythonContext context, TruffleFile capiLibrary, boolean isolateNative) throws ApiInitException {
102105
this.capiOriginal = capiLibrary.getName();
103106
if (isolateNative) {
104-
this.capiSlot = Long.numberOfTrailingZeros(Long.lowestOneBit(CEXT_COPY_INDICES.getAndUpdate((oldValue) -> oldValue ^ Long.lowestOneBit(oldValue))));
105-
if (this.capiSlot == Long.SIZE) {
106-
throw new ApiInitException(ErrorMessages.CAPI_ISOLATION_CAPPED_AT_D, Long.SIZE);
107+
this.capiSlot = MAX_CEXT_COPIES - CEXT_COPY_INDICES.getAndDecrement();
108+
if (this.capiSlot < 0) {
109+
CEXT_COPY_INDICES.set(0);
110+
throw new ApiInitException(ErrorMessages.CAPI_ISOLATION_CAPPED_AT_D, MAX_CEXT_COPIES);
107111
}
108112
this.capiCopy = resolve(context, capiLibrary, capiSlot, null);
109113
} else {
@@ -132,8 +136,6 @@ public String getCapiLibrary() {
132136
}
133137

134138
public void close() {
135-
// Return the C API slot for subsequent contexts to use.
136-
CEXT_COPY_INDICES.updateAndGet((oldValue) -> oldValue | (1L << capiSlot));
137139
}
138140

139141
/**

0 commit comments

Comments
 (0)