PyO3 / PyO3/pyo3

IPython ignores Control + C after loading Rust module built with PyO3/Maturin

Open
#4,121 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

Posted in https://github.com/ipython/ipython/issues/14417, duplicated below:


I wrote a Rust module battlesnake_gym and loaded it into the virtual environment. I ran the script below in IPython and hit Control + C after about 5 seconds, but the script was not interrupted. In fact, IPython now ignores Control + C completely. More information later.

from time import sleep
import battlesnake_gym  # This is a Rust module built with PyO3/Maturin.

for _ in range(100):
    sleep(0.1)

However, if I run this script in python the default REPL, it gets interrupted by Control + C normally. Therefore, this should have something to do with IPython, not just PyO3/Maturin.

Steps to Reproduce

I was trying to provide a minimum reproducible example, but failed with the bare PyO3 examples. Below are the rough steps to reproduce with my codebase, which would be nicer for someone who already have Rye to try out (sorry about the heavy dependencies).

# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh  ## Install Rust.
# curl -sSf https://rye-up.com/get | bash  ## Install Rye.

## Clone codebase.
git clone https://github.com/SichangHe/STATS402_course_project.git
cd STATS402_course_project
git submodule update --init

# Build Rust module.
cd battlesnake_train
rye sync

## Run IPython in the virtual environment.
# Remember to add the appropriate extension to the following command.
. .venv/bin/activate
ipython
## Below are my outputs. I hit control + C ~5sec after `In [1]`.
## Notice that there are no `KeyboardInterrupt` exceptions.
## Actually, it wasn't interrupted.
# Python 3.11.8 (main, Feb 25 2024, 04:18:18) [Clang 17.0.6 ]
# Type 'copyright', 'credits' or 'license' for more information
# IPython 8.23.0 -- An enhanced Interactive Python. Type '?' for help.
#
# In [1]: from time import sleep
#    ...: import battlesnake_gym
#    ...:
#    ...: for _ in range(100):
#    ...:     sleep(0.1)
#    ...:
# ^C
# In [2]:

Control C's search keys: control + c, ctrl c, ctrl + c, control c, ctrl c.

Backtrace

No response

Your operating system and version

Debian 6.1.38-2 (2023-07-27) x86_64 GNU/Linux

Your Python version (python --version)

Python 3.11.8

Your Rust version (rustc --version)

rustc 1.77.2 (25ef9e3d8 2024-04-09)

Your PyO3 version

0.21.2

How did you install python? Did you use a virtualenv?

I installed Python via Rye. I did use a virtualenv provided by Rye.

Additional Info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the provided IPython snippet with the PyO3/Maturin module, then compare its Control-C behavior with the default Python REPL and trace the interrupt-handling interaction. Done means Control-C interrupts the running loop and IPython continues responding afterward, with regression coverage for the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.