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

Call Py_Finalize at exit using libc::atexit. #943

Merged
merged 1 commit into from May 24, 2020

Conversation

m-ou-se
Copy link
Contributor

@m-ou-se m-ou-se commented May 23, 2020

This makes sure buffers are flushed, threads are joined, etc. when exiting the process.

@m-ou-se
Copy link
Contributor Author

m-ou-se commented May 23, 2020

Maybe this also removes the need for this hack:

pyo3/src/lib.rs

Lines 300 to 305 in 072be6c

e.print($py);
// So when this c api function the last line called printed the error to stderr,
// the output is only written into a buffer which is never flushed because we
// panic before flushing. This is where this hack comes into place
$py.run("import sys; sys.stderr.flush()", None, None)
.unwrap();

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Seems like a good idea to me. I wonder if this will also resolve #660

src/gil.rs Show resolved Hide resolved
@davidhewitt
Copy link
Member

Maybe this also removes the need for this hack:

Quite possibly. Does libc::atexit handler also get called on panic?

@m-ou-se
Copy link
Contributor Author

m-ou-se commented May 24, 2020

Does libc::atexit handler also get called on panic?

Yes.

@m-ou-se
Copy link
Contributor Author

m-ou-se commented May 24, 2020

Does libc::atexit handler also get called on panic?

Yes.

Well, not if the panic is caught and the program keeps running. But it runs if the panic causes the program to exit, yes.

@davidhewitt
Copy link
Member

Great, if you could please add a CHANGELOG and then I'm happy to merge this. I suggest just your commit message minus the implementation detail:

### Changed
- Call `Py_Finalize` at exit. [#943](https://github.com/PyO3/pyo3/pull/943)

But will take any other CHANGELOG message you think is good.

This makes sure buffers are flushed, threads are joined, etc. when
exiting the process.
@m-ou-se
Copy link
Contributor Author

m-ou-se commented May 24, 2020

Oh right, the changelog. Keep forgetting that file. Updated.

@davidhewitt
Copy link
Member

Great, thanks 😄

I think regarding the hack - you're right that it probably isn't needed any more. But I think order of output still will change. Currently it's flush buffers then panic handler runs, if we remove the hack it'll be panic handler then flush buffers during finalize. So let's leave it there for now, but we might clean up later.

@m-ou-se
Copy link
Contributor Author

m-ou-se commented May 24, 2020

Yeah. It's a shame that the CPython API doesn't have a Py_FlushStreams or something.

@davidhewitt davidhewitt merged commit 4355360 into PyO3:master May 24, 2020
@davidhewitt
Copy link
Member

Thank you!

@kngwyu
Copy link
Member

kngwyu commented May 24, 2020

Thank you!
I think it's OK to remove the import sys; sys.stderr.flush() hack, though we can test that it works.

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.

None yet

3 participants