Skip to content

Add api to signal whether Stream owns or borrows an FD#25

Merged
dcbaker merged 5 commits intomasterfrom
submit/dont-close-an-fd-we-didnt-open
Oct 30, 2020
Merged

Add api to signal whether Stream owns or borrows an FD#25
dcbaker merged 5 commits intomasterfrom
submit/dont-close-an-fd-we-didnt-open

Conversation

@dcbaker
Copy link
Copy Markdown
Owner

@dcbaker dcbaker commented Oct 30, 2020

The Stream class assumes that it owns the fd that it's writing into, which is only assured to be true when it opens the fd itself. If it didn't open the fd, it may not be appropriate for it to close it. This is not the ideal solution, the ideal would be that if you pass an fd you have to tell us Stream if it owns the fd or not. But that would be a backwards incompatible change.

The solution this uses then is to create an api with a deprecation warning. If you don't tell Stream we assume we own it, but in 1.0 that will switch and we'll assume the caller owns it, and we're just borrowing it.

Fixes: #23
Fixes: #24

@dcbaker dcbaker changed the title Add control of fd ownership Add api to signal whether Stream owns or borrows an FD Oct 30, 2020
The Stream class has always assumed that it has ownership of the
file-like object it is writing into. This isn't (and can't be) true in
some cases, like writing to stdout. This commit adds a new argument to
the Stream class, close_fd, which signals to the Stream class whether it
owns the fd or not, and thus whether it should close it.

Fixes: #23
This would have happened in fd.close(), and if the fd happens to be in
in use multiple places then you can get mixed output. While it probably
makes sense if you control the fd to create a locking wrapper, this is
difficult for things like `sys.stdout`, which would require monkey
patching to guard with locks if using any external modules (including
the standard library).

Fixes: #24
@dcbaker dcbaker force-pushed the submit/dont-close-an-fd-we-didnt-open branch from c839b20 to b657cb0 Compare October 30, 2020 20:23
@dcbaker
Copy link
Copy Markdown
Owner Author

dcbaker commented Oct 30, 2020

@JonoYang, Does this solve your issues?

@JonoYang
Copy link
Copy Markdown
Contributor

@dcbaker Yes, these changes addresses the problems I ran into in #23 and #24. Thank you for fixing these issues, I really appreciate it!

Side note: when will a new version of jsonstreams be available on pypi? I'd like to use this in my project for its new release.

@dcbaker dcbaker merged commit 8d62db1 into master Oct 30, 2020
@dcbaker dcbaker deleted the submit/dont-close-an-fd-we-didnt-open branch October 30, 2020 23:21
@dcbaker
Copy link
Copy Markdown
Owner Author

dcbaker commented Oct 30, 2020

I'm not sure when the next release will be. I'm still thinking about #20. but probably soonish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants