Fixed reference issue in LatexManager#5144
Fixed reference issue in LatexManager#5144kwankyu wants to merge 1 commit intomatplotlib:masterfrom kwankyu:pgf_backend_issue
Conversation
|
attn @pwuertz |
|
Unfortunately i wont be alle to review this due to a 2 week vacation starting total. I remember using weak references for similar reasons, maybe I forgot it at this place. So, +1 probably :) |
|
Hm, wouldnt it be better to keep a strong reference to os.path within Latex Manager then? Wouldnt that be the only way to ensure that the module required in del isnt unloaded by python? |
|
The 'cleanup', that is what the del does, is actually done by 'atexit' by
|
|
This is not what I had in mind. I'd like the temporary directory to be deleted as soon as the LatexManager instance that created it is deleted. So ideally the atexit approach shouldn't be required at all. Deleting files and directories on the windows file system however turns out to be pretty difficult due to locking mechanisms. So the idea is that "hopefully" the locks are gone at the time atexit is called and I attempt to delete any residual temp files there. The thing I missed is that in python it's better / more safe to keep references to imported modules if they are used within |
|
Does that other fix also solve the problem for you? |
|
There will be no problem if all LatexManager instances are cleaned up and deleted before the atexit is called. When atexit is called, as you said, you want to delete (cleaning up the temporary diretories) all remaining LatexManager instances. But the LatexManager instance, whose strong reference is kept in LatexManagerFactory, is not deleted by this atexit mechanism. When this instance is finally deleted when the python interpreter shuts down and I think that changing the strong reference kept in LatexManagerFactory to a weak reference is simple and more elegant way to fix the issue than keeping references to the imported modules. I also worry that these new references themselves are safe to rely on at shutdown time... But I don't know well about python at this level. Testing your fix in Sage is tricky to me. It would take some time for me to figure out how to do this... |
I think replacing the normal reference with a weak reference defeats the whole point of LatexManagerFactory. If you change the reference to a weak ref, python is free to delete the LatexManager at any time once the external references are dropped (maybe in between two figsaves). Starting up a new latex process takes a lot of time and I'd like to reuse an already running LatexManager if possible.
You can't delete python instances on purpose. At exit I just make sure that cleanup is called for any objects that are still alive, tracked by the weakref set. Python by the way doesn't even guarantee that del is called at all before the the whole process is terminated.
Maybe, I think I encountered this problem one or two times before, and I can't remember if this fixed the problem or if I had to reimport the modules again. I also forgot how to provoke such a situation for testing purposes, which is why I asked if you could test this solution.
But didn't you write and test your solution in sage too? Using another |
|
Just wondering... couldn't we use a context manager to create/destroy the On Thu, Oct 15, 2015 at 2:09 PM, Peter Würtz notifications@github.com
|
|
The LatexManager and latex process ideally transcends multiple savefig calls. For that reason you can't run it in a temporary directory for which the lifetime is tied to savefig. |
|
Ok, crazy idea, but how about wrapping the latex process with a tempdir On Thu, Oct 15, 2015 at 2:31 PM, Peter Würtz notifications@github.com
|
|
That could be a solution, but at this point I don't see the benefit in putting much effort into a new wrapper. The current solution for cleaning up the tempdirs works, has been tested, and has only a simple aesthetic flaw on exit that can be fixed easily, right? |
|
I tested your fix. It works fine in Sage as far as I am concerned. On the other hand, I also tested my fix again to see if LatexManagerFactory loses the LatexManager instance kept by a weak reference. It did not in my test. Of course, this does not defeat your concern. Anyway, it is up to you to choose which fix to merge to solve the issue I raised. I respect your expertise :-) |
|
True, I'm sure that in many cases some reference will be floating around and keep the LatexManager instance alive, but yes, my concern is that only a reference within the Factory guarantees it. The second concern is that you can reason how the use of weakref and the atexit cleanup affect the lifetime of LatexManager and prevent the situation of For those two reasons I'm in favour of fixing the Other than that the idea from @WeatherGod is really appealing. Running the tempdir context within another interpreter additionally has the benefit of being more robust in case of application segfaults too. I just can't pursue this at the time. |
|
Oh, and thanks @kwankyu for reporting and working on this bug. If I read your bug tracker correctly this was the last item on the list, other issues were fixed in earlier matplotlib releases. If anything else should pop up just give me a ping. |
This fixes a potential bug in the pgf backend. The issue is that the class LatexManagerFactory keeps a strong reference to a LatexManager object while the class LatexManager has
__del__method to clean up its objects. While the python interpreter shuts down, the LatexManager destructor (__del__) will run because of the presence of the strong reference, but this potentially can lead to an exception (but ignored) since it relies on, for instance,os.pathmodule, which possibly can be in the state of destruction.Actually this causes an issue in matplotlib embedded in Sage. Look http://trac.sagemath.org/ticket/14798
The fix is to use weak referencing.