[Bug]: A context without native access takes the C extension slot, so later contexts get SystemError instead of ImportError
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 55/100
調査の方向性
Start in CApiContext.loadCApi, identified in the issue around line 1081 of tag graal-25.5.5.0.3, and trace how native access checks interact with nativeCAPILoaded.compareAndSet. Reproduce with the supplied Java example that creates two contexts and imports getpass. Done means both contexts handle the unavailable termios extension consistently, without the second context raising SystemError.
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
When a context with allowNativeAccess(false) imports a C extension module, the import fails with ImportError: Cannot run any C extensions because native access is not allowed. That is expected. But the failed import still takes the process-wide C API slot, and never gives it back.
After that, every later context in the same process gets SystemError: Option python.IsolateNativeModules is set to 'false' and a second GraalPy context attempted to load a native module ... for the same kind of import, instead of the ImportError. None of these contexts asked for native access.
Code that handles the ImportError then breaks in the second context. For example import getpass (which tries import termios and catches ImportError), or try: import ctypes / except ImportError:.
It happens when the context can read the native module files, for example with IOAccess.ALL or with FileSystem.allowInternalResources(...). With the default IOAccess.NONE, the module file is not found (ModuleNotFoundError), and no slot is taken.
Operating system
macOS
CPU architecture
ARM64
GraalPy version
25.5.5.0.3 (org.graalvm.python:python-language:25.5.5.0.3)
JDK version
GraalVM CE 25.5.5.0.3 (OpenJDK 25.0.5.0.3)
Context configuration
Context.newBuilder("python")
.allowNativeAccess(false)
.allowIO(IOAccess.ALL)
.build();
python.IsolateNativeModules is not set (default false).
Steps to reproduce
Run this with the GraalPy 25.5.5.0.3 jars on the class path (java -cp "<jars>/*" Repro.java):
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.io.IOAccess;
public class Repro {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
try (Context context = Context.newBuilder("python")
.allowNativeAccess(false)
.allowIO(IOAccess.ALL)
.build()) {
context.eval("python", "import getpass");
System.out.println("context " + i + ": import getpass works");
} catch (PolyglotException e) {
System.out.println("context " + i + ": " + e.getMessage());
}
}
}
}
The first context is closed before the second one is created.
Expected behavior
Both contexts print import getpass works. A context without native access should not take the C API slot, so the import fails the same way (with the ImportError that getpass handles) in every context.
Stack trace
context 1: import getpass works
context 2: SystemError: Option python.IsolateNativeModules is set to 'false' and a second GraalPy context attempted to load a native module 'termios' from path '<python-home>/lib/graalpy25.5/modules/termios.graalpy253-313-native-aarch64-darwin.so'. At least one context in this process runs with 'IsolateNativeModules' set to false. Depending on the order of context creation, this means some contexts in the process cannot use native module.
at <python> <module>(Unnamed:1:0-13)
Additional context
In CApiContext.loadCApi (tag graal-25.5.5.0.3, around line 1081, and the same on master), nativeCAPILoaded.compareAndSet(NO_NATIVE_CONTEXT, GLOBAL_NATIVE_CONTEXT) runs before the C API library is loaded. Native access is only checked in the catch blocks after the load fails (context.isNativeAccessAllowed()). So a context that can never load the C API still takes the slot.
We see the same on Linux ARM64 (Docker), in an embedding that creates several contexts in one process.
Our workaround: in contexts without native access, make C extension imports fail before GraalPy reaches the C API:
import importlib.machinery as _m
def _no_c_ext(self, spec):
raise ImportError("Cannot run any C extensions because native access is not allowed.", name=spec.name)
_m.ExtensionFileLoader.create_module = _no_c_ext
Related: #437 (multi-context support for C extensions) and #563 (IsolateNativeModules crash). This issue is only about contexts without native access.
- 主要言語
- Python
- スター
- 1.7k
- フォーク
- 167
- 平均マージ
- 3日 11時間
- マージ済み PR(30日)
- 68
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
oracle/graalpython のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
oracle/graalpython#1105 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
oracle/graalpython#1102 ·
メンテナーはふだん 1 日以内に返信
-
mmap.mmap.move() is missing対応中かも @deronnax が 3 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
oracle/graalpython#1368 ·
メンテナーはふだん 1 日以内に返信
-
mmap extended slicing ignores the step; negative steps raise java.lang.IndexOutOfBoundsExceptionオープン
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
oracle/graalpython#1367 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
oracle/graalpython#1366 ·
メンテナーはふだん 1 日以内に返信
oracle/graalpython の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
bug improve content
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
opensafely/documentation#2102 ·
メンテナーはふだん 1 日以内に返信
-
difficulty:1 documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
MakazhanAlpamys/Soup#3533 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
onyx-dot-app/onyx#18730 ·
メンテナーはふだん 1 日以内に返信