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

Add missing call to finalize the interpreter on NatSpeak shutdown #129

Merged
merged 1 commit into from
Sep 2, 2022

Conversation

drmfinlay
Copy link
Member

@drmfinlay drmfinlay commented Aug 29, 2022

This pull request adds a missing call to Py_Finalize() in COM\appsupp.cpp. This should allow things like the atexit module to work.

I have set this as a draft PR for the moment. Still need to compile and test that this works fine.

This should allow things like the built-in *atexit* module to work.
@drmfinlay drmfinlay marked this pull request as ready for review September 2, 2022 11:54
@drmfinlay
Copy link
Member Author

Had some trouble setting Natlink up in order to test this, but I can confirm the change hasn't broken anything. The atexit module works properly now, as do other tasks normally performed prior to the termination of python.exe. This PR is ready to be merged.

I did notice an interesting caveat in the Python/C API Reference Manual that applies to Natlink:

Notice that Py_Finalize() does not free all memory allocated by the Python interpreter, e.g. memory allocated by extension modules currently cannot be released.

Embedding Python (final paragraph)
https://docs.python.org/2/c-api/intro.html#embedding-python

@LexiconCode LexiconCode merged commit a4c2ca5 into master Sep 2, 2022
@drmfinlay drmfinlay deleted the fix/py_finalize branch September 2, 2022 13:55
@LexiconCode LexiconCode added the bug Something isn't working label Sep 2, 2022
@LexiconCode
Copy link
Member

LexiconCode commented Sep 2, 2022

Had some trouble setting Natlink up in order to test this, but I can confirm the change hasn't broken anything. The atexit module works properly now, as do other tasks normally performed prior to the termination of python.exe. This PR is ready to be merged.

I did notice an interesting caveat in the Python/C API Reference Manual that applies to Natlink:

Notice that Py_Finalize() does not free all memory allocated by the Python interpreter, e.g. memory allocated by extension modules currently cannot be released.

Embedding Python (final paragraph) docs.python.org/2/c-api/intro.html#embedding-python

I did mention this to Doug on telegram, he thought it wasn't too much of a concern as it's not reinitialized too often. For reference

https://stackoverflow.com/questions/42971734/memory-leak-when-embedding-python-into-my-application

Could PyGC_Collect() be called right before Py_Finalize()?

@LexiconCode
Copy link
Member

LexiconCode commented Sep 2, 2022

I do not think this is do to the pr.

Just a heads up, I think there's a bug when NatLink shuts down.

So I loaded it with an empty _grammar.py. loads correctly. First load. Shut down dragon through the tray. Reload dragon after the tray icon disappears:

debug says triggering load/reload process
skipping unchanged loaded module: _grammar.

Process from 'messaging from natlink' is still running. Despite dragon being closed. Once closed from task manager when dragon restarts it functions as expected.

@drmfinlay
Copy link
Member Author

drmfinlay commented Sep 2, 2022 via email

@drmfinlay
Copy link
Member Author

Could PyGC_Collect() he called right before Py_Finalize()?

There is no need. Py_Finalize(), or, more directly, Py_FinalizeEx(), already calls this function. See pylifecycle.c line 1830.

@drmfinlay
Copy link
Member Author

Yes, the behaviour you mention is unrelated. I didn't get this when I tested the change yesterday.

I have previously seen natspeak.exe hang around after it should have exited. Not sure there's anything that can be done about that. Maybe it's doing some maintenance.

@quintijn
Copy link
Contributor

quintijn commented Jun 6, 2024

The issue #184 points to this issue, to not forget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants