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

handling TimeConstrained[] in SymPy evaluation #1160

Merged
merged 7 commits into from
Nov 13, 2024
Merged

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Nov 9, 2024

An idea to fix TimeConstrained and related functions, at least for evaluations involving sympy.
Still I can produce a core dump if I ask for a ridiculus task in Sympy, but is a problem of Sympy, not from Mathics

@mmatera
Copy link
Contributor Author

mmatera commented Nov 9, 2024

@davidar, ideas about how to implement this in Pyodide are welcome.

@davidar
Copy link
Collaborator

davidar commented Nov 9, 2024

Unfortunately I don't think there is a good solution to that in pyodide yet, at least not without cooperation from the javascript environment hosting the pyodide interpreter (https://pyodide.org/en/stable/usage/keyboard-interrupts.html). Might just need to disable it in pyodide for now.

@mmatera mmatera marked this pull request as ready for review November 9, 2024 13:09
mathics/eval/sympy.py Outdated Show resolved Hide resolved
@rocky
Copy link
Member

rocky commented Nov 10, 2024

Unfortunately I don't think there is a good solution to that in pyodide yet, at least not without cooperation from the javascript environment hosting the pyodide interpreter (https://pyodide.org/en/stable/usage/keyboard-interrupts.html). Might just need to disable it in pyodide for now.

@davidar Thanks for the information and link. It confirms what I had meant to look up.

What Mathics3 should do is provide a signal-handling mechanism that causes the current computation to stop after a certain amount of time (SIGALRM).

Mathics3 should also trap other signals like SIGINT. In wolframscript, Ctrl-C (SIGINT) goes into an Interrupt prompt. Later, we should provide a compatible (and possibly much better) interrupt interface.

If I have this correct, the link you have indicates that pyodide does provide a means for sending trapping and sending signals back to Python.

One other thing to note about Python's signal handling - only the main thread can trap signals. I don't think this is a problem, just something to note.

With this we can provide a somewhat coarse mechanism for handling timeouts that is compatible with pyodide.

@mmatera Now that you may have been just informed about signal handling and timeouts, please don't jump ahead and start implementing this in code. (If you want to experiment privately - by all means go ahead.)

If there is a need right now for TimeConstrained, I'd appreciate knowing what the deal is.
Otherwise, let's leave adding signal handlers in Mathics3 and having that work in pyodide for later.

@rocky rocky changed the title handlig timeout in sympy evaluation handling TimeConstrained in sympy evaluation Nov 10, 2024
@rocky rocky changed the title handling TimeConstrained in sympy evaluation handling TimeConstrained in SymPy evaluation Nov 10, 2024
@rocky
Copy link
Member

rocky commented Nov 10, 2024

@mmatera Please give me a couple of days for me to pull down this branch to try out. Thanks.

@rocky rocky changed the title handling TimeConstrained in SymPy evaluation handling TimeConstrained[] in SymPy evaluation Nov 10, 2024
@mmatera
Copy link
Contributor Author

mmatera commented Nov 11, 2024

@mmatera Please give me a couple of days for me to pull down this branch to try out. Thanks.

Sure, no hurry. I just put this because when I looked over the issue with Simplify I realized that this would be a way to support TimeConstrained in systems that support Threads.

mathics/builtin/datentime.py Outdated Show resolved Hide resolved

# Consider testing via unit tests.
# >> TimeConstrained[Integrate[Sin[x]^1000000,x],1]
# = $Aborted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried

TimeConstrained[Integrate[Sin[x]^1000000,x],1]

I am getting a RecursionError that is not handled. Worse is that calls with_traceback() with the wrong number of parameters. A PR has been created to address this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, with Pyston I got a core dump for that test. But it is reasonable: Sympy does not control by itself how much memory/work needs to converge.

mathics/core/evaluation.py Outdated Show resolved Hide resolved
mathics/eval/sympy.py Outdated Show resolved Hide resolved
@rocky rocky merged commit c1cf1a0 into master Nov 13, 2024
13 checks passed
@rocky rocky deleted the fixing_TimeConstrained branch November 13, 2024 00:28
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.

3 participants