ipython: ValueError: I/O operation on closed file

c:\python\python27\lib\site-packages\ipykernel\__main__.py:5: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 167, in flush
    data = self._flush_buffer()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 242, in _flush_buffer
    data = self._buffer.getvalue()
ValueError: I/O operation on closed file
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 167, in flush
    data = self._flush_buffer()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 242, in _flush_buffer
    data = self._buffer.getvalue()
ValueError: I/O operation on closed file
ERROR:tornado.application:Exception in callback None
Traceback (most recent call last):
  File "c:\python\python27\lib\site-packages\tornado\ioloop.py", line 883, in start
    handler_func(fd_obj, events)
  File "c:\python\python27\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "c:\python\python27\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "c:\python\python27\lib\site-packages\ipykernel\kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 167, in flush
    data = self._flush_buffer()
  File "c:\python\python27\lib\site-packages\ipykernel\iostream.py", line 242, in _flush_buffer
    data = self._buffer.getvalue()
ValueError: I/O operation on closed file

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 3
  • Comments: 23 (9 by maintainers)

Most upvoted comments

I’m not sure why this issue is marked closed since lots of people are still running into it. I hit it frequently when running Keras, as do other people based on comments above

Working on a 4.4 release shortly.

@minrk It appears to be happening at random, but always at some print(...) statement. My function doesn’t really do a lot of I/O, mostly reading csv files with pandas.read_csv() and writing some csv files with pandas.DataFrame.to_csv(), no direct file I/O access.

The only obvious and direct I/O in my own code would be a few print() statements, some with str.format(...), some with just a simple string.

Let me see if I can reliability replicate this in a standalone script.

Just to add, this code runs in a single process, no multi-threading or multi-processing in the code.

In case it helps anyone, I just ran into this issue while using https://github.com/tqdm/tqdm in a version of the ipykernel that doesn’t include https://github.com/ipython/ipykernel/pull/123