-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
src/sage/misc/latex.py: fix view() #36529
Conversation
Nope :
==> |
Well that's cute. I was sort of expecting the asynchronous subprocess example from the asynchronous I/O documentation to run asynchronously. I just clobbered my sage install by switching to this clean branch but I'll try again as soon as I rebuild sagelib. |
30357fd
to
60d5d57
Compare
Attempt number 3. |
This one does display the expression and returns. Seems a bit slower than the original version, but my system is currently loaded (running I wait for the end of tests to give positive review... |
Nice little gag : a Sage console won't Similarly, from the (bash) command line, May be a hindrance... |
Rewriting the view() function to use python's tempfile module instead of sage's own tmp_dir() accidentally broke this function, because the viewer program needs the file to be slightly less temporary than we made it. The viewer would launch, but then view() would immediately return, causing the file that the viewer was about to look for to be deleted. To fix it, we now launch the viewer program synchronously within a helper function, but launch that helper function asynchronously in a new thread. This allows us to clean up the temporary directory only after the subprocess has completed, but still lets view() return immediately. Closes: sagemath#36526
60d5d57
to
a859ac2
Compare
I would not have guessed that this was going to be the hardest thing I did today. Attempt number four! |
I'll have a look now, but full testing will have to wait tomorrow. |
Well...
Sorry ! "Cent fois sur le métier remettez votre ouvrage"... |
FWIW : attempt number three did not introduce any new HTH, |
Documentation preview for this PR (built with commit a859ac2; changes) is ready! 🎉 |
Because of https://github.com/sagemath/sage/wiki/Sage-10.2-Release-Tour#open-blocker-prs-are-applied-automatically-in-ci-workflows, let's set this to "blocker" only after the fix works |
I just reverted everything, and this never worked to begin with. We have conflicting requirements if we want sage to,
When both of those are satisfied, There's a similar problem with plot/show. In that case the solution I've always used is to |
Maybe #36436 wasn't such a great idea, after all : the invoked "security reasons" are a tad nebulous to me... |
Shouldn't BTW : |
What security reasons? The goal of #36436 is to minimize how much junk sage leaves on the filesystem. For example if you call
Do you mean anything specific by independent? In general its independence is limited by using a temporary file created by sage and that needs to be cleaned up by sage. The current version should not make anything worse, and has the added benefit of cleaning up the files when the viewer is closed. |
From your initial message of #36436 :
Those still are a tad nebulous to my (admittedly slow and retarded) mind.
Agreed
Not necessarily : Possible algorithm : I'll check the curent behaviour of 10.2.beta7 (IIRC), but I cannot do this before Friday 27. HTH, |
#36436 didn't change anything in that regard. I was trying to document why we need a temporary directory instead of a temporary file, but a directory was always used -- just a less temporary one. At first glance, the latex build process looks like it only needs a temporary file because it turns Just for an example: someone else could create tl;dr I didn't want someone to see the code and think "this only needs a temporary file!" and change it.
Should sage refuse to exit for that reasonable delay? :) |
FWIW the behavior before #36436 is to stick the file in a new temporary directory, launch the viewer, and then forget about it. The temporary directory is only removed when sage exits and either the Relative to that, the current PR makes only an improvement: everything else is the same, but the directory will also be removed when the viewer closes, which is usually sooner. |
I was thinking milliseconds (time to be sure tohave a viewer opening (and thus locking) the temporary file |
I played with this idea, not intending to keep it, but just to see if it would work. On my machine the "viewer" is xdg-open, which is a shell script, so sage has to launch /bin/sh, find xdg-open, run xdg-open, launch exo-open (xdg-open eventually does this), and then finally launch the real PDF viewer which then has to open the temporary file. That all has to work on old machines under heavy load, so for |
Can we merge this for 10.2 to get |
Sorry for the delay to answer : I have been ... interrupded, say.
I have been able to check against 10.2.beta5 :
Your fourth attempt is therefore a reasonable fix to the initial problem, that can be incorporated in 10.2. I wonder if the new functionality I envisioned would be useful. It does not semm that it was previously lacking. I might have succumbed to the temptation of wanting anything possible from a console also possible from command line. Let's say that if I feel this useful, I'll introduce a new issnue. Okay with you ? Again, thank you very much for your attention. |
I think it would be useful, it's just not clear to me how to accomplish it. I would be happy to have a better solution though. I will probably encounter this same problem a few more times while cleaning up the remaining uses of |
Works for me (ubuntu 22.04, emacs 27.1) Martin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM: when merged into Sage 10.2.rc0, the view()
functionality is restored. Thank you for the fix!
Rewriting the view() function to use python's tempfile module instead of sage's own tmp_dir() accidentally broke this function, because the viewer program needs the file to be slightly less temporary than we made it. The viewer would launch, but then view() would immediately return, causing the file that the viewer was about to look for to be deleted. To fix it, we now launch the viewer program with a coroutine and wait for it (asynchronously) to return. Only when it has returned will we delete the file it's viewing. Closes: sagemath#36526 URL: sagemath#36529 Reported by: Michael Orlitzky Reviewer(s): Eric Gourgoulhon
Sorry to arrive a bit late here, but while working on See https://github.com/sagemath/sage/blob/develop/src/sage/misc/latex_standalone.py#L745 |
I'm sure there are similar calls all over sage. Hopefully no one reports any problems with this approach and it can be reused or factored out into a separate function. The calls in |
Ok I see. So I guess what you have done will need to be done also in latex_standalone.py at some point. |
Yeah, assuming it works :) |
As I posted in sage-support, I think that there is still an issue here. In more detail, on a mac the following usually leads to the error from sage.misc.viewer import viewer
viewer.pdf_viewer('open')
view(crystals.LSPaths( RootSystem(['A',4]).weight_space().basis()[1] ) ) Adding a def run_viewer():
run([viewer, output_file], capture_output=True)
#tmp.cleanup() # put after t.join() below
# ...but we execute it asynchronously so that view() completes
# immediately. The "daemon" flag is important because, without it,
# sage won't quit until the viewer does.
from threading import Thread
t = Thread(target=run_viewer, daemon=True)
t.start()
t.join() # wait for the thread to exit
tmp.cleanup() but, unfortunately, this does not solve my problem. The problem is that the viewer.pdf_viewer('/Applications/Skim.app/Contents/MacOS/Skim') Now everything works as expected. So, perhaps it is enough for the documentation to warn against using generic file opening commands, such as |
That's one of the problems I was trying to fix, but I guess it doesn't work the same on macOS or with how
TBH I'd rather actually fix it, even if that means adding a special case for macOS. But it took a lot of experimenting to find a solution that worked here and I don't have access to a mac to try things. As a last resort, we could always (on macOS) go back to the way things worked before I broke everything. I.e. leave the file there indefinitely until/unless sage closes gracefully. The downside to that is that it potentially leaves junk on the filesystem if sage crashes or if you write a long-running program, but it may be the lesser of two evils. |
I am happy to help troubleshoot on a mac. Unfortunately, I can't give you log in access to one. |
Rewriting the view() function to use python's tempfile module instead of sage's own tmp_dir() accidentally broke this function, because the viewer program needs the file to be slightly less temporary than we made it. The viewer would launch, but then view() would immediately return, causing the file that the viewer was about to look for to be deleted.
To fix it, we now launch the viewer program with a coroutine and wait for it (asynchronously) to return. Only when it has returned will we delete the file it's viewing.
Closes: #36526