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 #633

Closed
hannosch opened this issue Jul 7, 2015 · 3 comments
Closed

ValueError: I/O operation on closed file #633

hannosch opened this issue Jul 7, 2015 · 3 comments

Comments

@hannosch
Copy link

hannosch commented Jul 7, 2015

Python 2.6, redis-py 2.10.3

During connection.disconnect I get a ValueError and the connection cleanup fails.

Traceback, with app specific first couple stackframes removed:

Stacktrace (most recent call last):
  ...
  File "redis/client.py", line 2578, in execute
    return execute(conn, stack, raise_on_error)
  File "redis/client.py", line 2447, in _execute_transaction
    connection.send_packed_command(all_cmds)
  File "redis/connection.py", line 543, in send_packed_command
    self.disconnect()
  File "redis/connection.py", line 519, in disconnect
    self._parser.on_disconnect()
  File "redis/connection.py", line 216, in on_disconnect
    self._buffer.close()
  File "redis/connection.py", line 182, in close
    self.purge()
  File "redis/connection.py", line 176, in purge
    self._buffer.seek(0)

send_packed_command encountered a socket.error (error(9, 'Bad file descriptor')). It tries to call self.disconnect() but gets a ValueError inside the close/purge of the SocketBuffer used by the PythonParser, so it never gets to raise ConnectionError. The relevant bit from Connection.send_packed_command is:

        except socket.error:
            e = sys.exc_info()[1]
            self.disconnect()
            if len(e.args) == 1:
                _errno, errmsg = 'UNKNOWN', e.args[0]
            else:
                _errno, errmsg = e.args
            raise ConnectionError("Error %s while writing to socket. %s." %
                                  (_errno, errmsg))

I think the SocketBuffer.close should catch and ignore this error or possibly just log it. If the SocketBuffer._buffer isn't there anymore while we try to remove it, so be it.

hannosch added a commit to mozilla/ichnaea that referenced this issue Jul 7, 2015
@andymccurdy
Copy link
Contributor

Sorry for the late reply.

Do you happen to recall the error message associated with the ValueError that was raised by buffer.seek(0)? It sounds like somehow the buffer isn't getting initialized correctly, but I fail to see how that can happen (it's initialized in SocketBuffer.__init__).

I tried injecting raise socket.error(9, 'Bad file descriptor') in Connection.send_packed_command manually which worked as expected. A ConnectionError was properly raised and the SocketBuffer was cleaned up appropriately.

Given that we're setting the instance's buffer to None after closing it's probably find to ignore errors while cleaning up the buffer. I'll go ahead and make the change but if you have more information on how this is happening I'd be interested in knowing.

Thanks for reporting this.

@Grokzen
Copy link

Grokzen commented Dec 27, 2018

@andymccurdy If the referenced commit you made above resolves the issue, this issue could be closed as resolved.

@andymccurdy
Copy link
Contributor

Ya, closing this as it seems to have been resolved.

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