Suzhou-Tongyuan / jnumpy

Writing Python C extensions in Julia within 5 minutes.
MIT License
234 stars 8 forks source link

Python REPL: KeyboardInterrupt stops working after import #74

Closed nrontsis closed 1 year ago

nrontsis commented 1 year ago

As described in the title, KeyboardInterrupt stops working after importing the basic example of this repo.

How to reproduce:

Using the Dockerfile below run:

$ docker build -t jnumpy .
$ docker run -it jnumpy python

and then

Python 3.9.15 (main, Oct 25 2022, 05:49:37)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>     <- Ctrl-C triggers KeyboardInterrupt
KeyboardInterrupt
>>> import basic
Can not find julia.
... [Installing julia]
>>>    <- Ctrl-C does not trigger KeyboardInterrupt anymore

Dockerfile:

FROM python:3.9-slim
RUN apt-get update
RUN apt-get install -y git gpg
RUN pip install julia-numpy jill
RUN git clone https://github.com/Suzhou-Tongyuan/jnumpy
ENV PYTHONPATH=/jnumpy/demo
johnnychen94 commented 1 year ago

Weird. I could only reproduce this on the first try.

The only place I see try...catch through the downloading call stack is https://github.com/johnnychen94/jill.py/blob/cfc14a6a700172833487bdc1ef2204b14d4cb88a/jill/download.py#L50-L69, but it's limited to network exceptions.

wget.download directly calls urllib.request.urlretrieve. Maybe it's something there since it's a legacy interface?

songjhaha commented 1 year ago

@johnnychen94 I think it's a jnumpy's issue, I could reproduce in my compute.

  In [1]: import basic # success
  In [2]: import wget
  In [3]: wget.download("https://wrongURL")
  # <- Ctrl-C does not work here

My envrionment:

os: win10
python: 3.10
johnnychen94 commented 1 year ago

I assume you're saying it's a wget issue?

In [1]: import wget

In [2]: wget.download("https://hi")
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C
KeyboardInterrupt

This Python package isn't maintained since 2015, so maybe this issue is a good reason to drop wget-python in jill.

songjhaha commented 1 year ago

Other task also fail to use KeyboardInterrupt too, so maybe something of jnumpy breaks the signal handling

songjhaha commented 1 year ago

Register a signal handler may solve this problem, but I could not find where jnumpy breaks this yet, and not sure if other signal handler works fine. @thautwarm could you have a look?

  import time
  import signal

  default_sigint_handler = signal.getsignal(signal.SIGINT)
  print(default_sigint_handler) # <built-in function default_int_handler>

  import jnumpy as jnp
  jnp.init_jl()

  print(signal.getsignal(signal.SIGINT)) # still <built-in function default_int_handler> ???

  signal.signal(signal.SIGINT, default_sigint_handler) # if we don't call this, the Ctrl-C would not work below

  # Ctrl-C works here
  for i in range(10):
      print(i)
      time.sleep(1)

ps: it's weird. the code above works fine in my archlinux system, but fails in my win10 system :(

songjhaha commented 1 year ago

We need to disable the signal handle of julia in python mode:

  SessionCtx.JULIA_START_OPTIONS = ["--handle-signals=no", jl_opts_proj, *jl_opts]

This solves the problem.