Skip to content

Fix SIGBUS crash on macOS ARM64 by removing unsafe pointer extraction#36

Closed
cbjuan wants to merge 2 commits intomainfrom
fixing-sigbus-issue
Closed

Fix SIGBUS crash on macOS ARM64 by removing unsafe pointer extraction#36
cbjuan wants to merge 2 commits intomainfrom
fixing-sigbus-issue

Conversation

@cbjuan
Copy link
Copy Markdown
Member

@cbjuan cbjuan commented Jan 7, 2026

Summary

  • Remove unsafe __extract_env__() method that caused memory corruption across different compiled modules
  • Add clone() method to PyBaseEnv for safe environment cloning via Python interface
  • Add PyBaseEnvWrapper for external Rust environments (like GridWorld) that extend PyBaseEnv
  • Fixes BUG: SIGBUS crash during RLSynthesis.learn() on macOS ARM #35

Description

Problem

When external packages like qiskit-gym statically link TwisteRL and create their own Rust environments, calling evaluate() or collect() would cause a SIGBUS crash (exit code 138) on macOS ARM64. This happened even with num_cores=1, ruling out parallelism as the cause. The root cause was the unsafe pointer extraction pattern in __extract_env__() which cast raw pointers between different compilation units with incompatible vtable layouts.

Solution

  • Remove unsafe pointer extraction - Delete extract_env() method entirely
  • Add clone() method to PyBaseEnv - Uses dyn_clone::clone_box() to safely clone the internal Rust environment. This method is inherited by ALL PyBaseEnv subclasses, including external ones like GridWorld.
  • Add PyBaseEnvWrapper - A new wrapper for external Rust environments that:
  • Uses PyBaseEnv-compatible method names (step, reward, reset)
  • Clones via the new clone() method
  • Detects interface type by checking for step vs next methods
  • Keep PyEnvImpl - For pure Python environments with custom interface (next, value, copy)

@cbjuan
Copy link
Copy Markdown
Member Author

cbjuan commented Jan 8, 2026

After the last release of qiskit-gym https://pypi.org/project/qiskit-gym/0.2.0/, this issue does not appear anymore. Closing this PR for now

@cbjuan cbjuan closed this Jan 8, 2026
@cbjuan cbjuan deleted the fixing-sigbus-issue branch January 8, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: SIGBUS crash during RLSynthesis.learn() on macOS ARM

1 participant