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

ValueError: I/O operation on closed file #146

Closed
joshuasiler opened this issue Feb 3, 2020 · 10 comments
Closed

ValueError: I/O operation on closed file #146

joshuasiler opened this issue Feb 3, 2020 · 10 comments

Comments

@joshuasiler
Copy link

I have a multiprocess system that I'm trying to move from Blessings to Blessed. Currently, the code calls term = Terminal() in the main process, and then again in each forked process allowing everyone to write to the screen.

When I changed the import to Blessed, the system now fails on term = Terminal() in the forked processes with the following error:

ValueError: I/O operation on closed file

Any idea why there's a difference in operation?

@jquast
Copy link
Owner

jquast commented Feb 3, 2020

I'm curious what line number / source code this ValueError raised from? I've been going back & forth on 2 things at this line number, that is maybe relevant:

https://github.com/jquast/blessed/blob/master/blessed/terminal.py#L185

                curses.setupterm(kind, open(os.devnull).fileno())

The previous version is just like blessings was,

                curses.setupterm(self._kind, self._init_descriptor)

which you're welcome to try to see if it makes any difference

@jquast jquast changed the title Multiprocess regression from Blessings ValueError: I/O operation on closed file Feb 3, 2020
@jquast
Copy link
Owner

jquast commented Feb 3, 2020

I've been seeing this at the end of pytest, I don't know if its similar or not, its just a side-effect I guess,

Exception ignored in: <_io.FileIO name='/dev/null' mode='rb' closefd=True>
Traceback (most recent call last):
  File "/Users/jq/Code/blessed/blessed/terminal.py", line 185, in __init__
    curses.setupterm(self._kind, open(os.devnull).fileno())
ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>

which goes away if I revert to the previous setupterm(), I'm going to revert this and release a 1.17.2 soon, whether it helps you or not :)

@joshuasiler
Copy link
Author

joshuasiler commented Feb 3, 2020

Here is the stack trace, hopefully that helps.

File "/home/ubuntu/dev/AlgoTrader/lib/strategy_runner.py", line 10, in run
    term = Terminal()

File "/home/ubuntu/.local/share/virtualenvs/AlgoTrader-i7wbyDWF/lib/python3.8/site-packages/blessed/terminal.py", line 166, in __init__
    self.__init__streams()

File "/home/ubuntu/.local/share/virtualenvs/AlgoTrader-i7wbyDWF/lib/python3.8/site-packages/blessed/terminal.py", line 237, in __init__streams
    self._keyboard_fd = sys.__stdin__.fileno()

ValueError: I/O operation on closed file

@jquast
Copy link
Owner

jquast commented Feb 3, 2020

Ah, well, in this case, stdin has some strange issue, we can safely ignore it with the understanding that inkey() and other keyboard functions would not work, you wouldn't be able to use it deep within multiprocessing anyway, this is a shared resource that is getting mucked, thanks for the bug report, i'll try something

@joshuasiler
Copy link
Author

So catch the exception and ignore... I'm happy to contribute, should I try to put together a PR for you?

@jquast
Copy link
Owner

jquast commented Feb 3, 2020

I'm already done and rolling to pypi, #147

jquast added a commit that referenced this issue Feb 3, 2020
- move "styles" section from colors.rst to terminal.rst
- add term.link("https://example.com", "example.com") hyperlink support
- add bin/cnn.py news example
- revert this setupterm() experiment #59, discussed in #146
- ignore ValueError for multiprocessing environments #146
@avylove
Copy link
Collaborator

avylove commented Feb 3, 2020

@jquast , We probably want to append the exception to errors rather than just pass. That way we have some history when it's not working unexpectedly.

@jquast
Copy link
Owner

jquast commented Feb 3, 2020

I'm afraid you're too late, you'll have to prepare it for the next release, the twine finished just before I saw your message.

@jquast
Copy link
Owner

jquast commented Feb 3, 2020

you're welcome to try from pypi @joshuasiler

@joshuasiler
Copy link
Author

Updated and it now works. Thanks!

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

No branches or pull requests

3 participants