-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Reinstall signal handler when Python signal handler called #228
base: main
Are you sure you want to change the base?
Conversation
24aa6c2
to
231c981
Compare
These two tests are failing: The test can be fixed by adding appropriate On the other hand a test could be added. It could be as simple as
|
231c981
to
df2b512
Compare
The point of having a os-level interrupt handler is to rollback the process (using longjmp) to a state where the python signal handler can be called. IOW, I don't think this will fix anything until the python handler is called; the problem is when the python handler is NOT called (this is the reason why cysignals needs to use a os-level interrupt handler). I think there's something special about
I believe the only way to fix this is for matplotlib to restore the os-level interrupt handler. See prompt-toolkit/python-prompt-toolkit#1576 for a similar issue and prompt-toolkit/python-prompt-toolkit#1822 for the fix. A different workaround might be to make sure the signal handler is correctly installed at each prompt (I'm not sure ipython has a hook for that... I think prompt_toolkit has it, but we don't use prompt_toolkit directly afair). This would not be good for scripts but at least an improvement for interactive use. |
|
It's not even that hard, see my fix for prompt_toolkit. It went through a lot of iterations to make it so simple: pure python (thanks to ctypes) and using only the stable abi. This was fundamental to convince upstream to merge it.
This is the whole (only?) point of using cysignals in the first place.
We don't need cysignals for this. Whether this is more or less common is debatable, but the whole point of sagemath is to integrate external libraries which do non-trivial computations.
This is free software with an open issue tracker: we can go fix matplotlib. Maybe we can even come up with something useful to add to python signal module (and documentation!) that will make it easier to preserve signals. Note that the original bug in prompt_toolkit was they did not restore the sigint handler at all (not even the python one) and so your workaround would not have done anything in that situation either. Could you try to save the os-level sigint handler at https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/backend_bases.py#L1635 (you can adapt my changes in prompt-toolkit/python-prompt-toolkit#1822)
Reentrancy is hard. I'm not sure calling |
Yes, but that's not the point: my point is [matplotlib + no cysignals] leaves interruption of Python code working, but [matplotlib + cysignals] breaks interruption of Python code in this specific case.
I mean, yes we're lucky in this case, but the next time some user uses some other library that also backups/restores the signal handler the same way we should probably at least print out a warning message to tell the user what to do instead of making them going through maybe an hour of debugging. If there is a reliable way of detecting this condition, that is. Looks like the function is already called without any |
I understand what you mean. But by the same reasoning, [cysignals + no matplotlib] leaves interruption of python and non-python code working, but [cysignals + matplotlib] breaks interruption of python code and non-python code. While "fixing" this on the cysignals side might be worthwhile, it won't fix the interruption of non-python code. OTOH, fixing this on the matplotlib side should be very easy and fix everything.
Another library may change the signal handler and not restore it. For instance, prompt_toolkit used to do that. In this case, matplotlib does it, but not completely. Easy to fix.
I think this goes back to the idea of doing something at each prompt: we could check the signal handler to see if it has been modified, and in that case warn the user (and fix it). This may be better than doing it in the interrupt handler itself, because (a) it avoids reentrancy (b) it will work in more cases (c) it will warn the user. Another option, perhaps, is to replace the |
This does sound like a reasonable idea: it will catches similar error cases.
Possible but might have unintended side effect? (might happen if someone try to |
This would fix sagemath/sage#39601 . Not sure if it has any downside.