Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot stop rust program while running python code #3218

Closed
ktjd123 opened this issue Jun 8, 2023 · 5 comments
Closed

Cannot stop rust program while running python code #3218

ktjd123 opened this issue Jun 8, 2023 · 5 comments
Labels

Comments

@ktjd123
Copy link

ktjd123 commented Jun 8, 2023

Bug Description

When I run python code in fast loop from rust and want to stop the program, python takes interrupt so rust program won't exit.

Steps to Reproduce

Run python code in Rust

Backtrace

No response

Your operating system and version

tested in mac, linux

Your Python version (python --version)

Python 3.11.3

Your Rust version (rustc --version)

rustc 1.70.0 (90c541806 2023-05-31)

Your PyO3 version

0.19.0

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

mac with brew

and apt for linux

Additional Info

Thank you.

@ktjd123 ktjd123 added the bug label Jun 8, 2023
@adamreichold
Copy link
Member

Could you share some code showing what exactly you are doing?

For example, doesn't Python raise an exception when the signal is received? Where is that exception handled?

@ktjd123
Copy link
Author

ktjd123 commented Jun 8, 2023

Sorry for few information.

I think the CTRL-C is captured at python side, and thought That shouldn't happen since It's blocking input to main process, But I'll investigate more and re-open issue.

Thank you for reply

@ktjd123 ktjd123 closed this as completed Jun 8, 2023
@birkenfeld
Copy link
Member

You can disable Python's SIGINT handler with

signal.signal(signal.SIGINT, signal.SIG_DFL)

@ktjd123
Copy link
Author

ktjd123 commented Jun 12, 2023

@birkenfeld Thank you for your kind help :)

@Jgfrausing
Copy link
Contributor

To do it from Rust:

        Python::with_gil(|py| {
            py.run(
                r#"
import signal
print("[python] Ignoring system signals")
signal.signal(signal.SIGINT, signal.SIG_DFL)
              "#,
              None,
              None,
          )
          .unwrap();
      });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants