Commits

Denis Bilenko  committed 2f2d631

fix close(): previously it might have caused receive() to hang

- also make send_frame() raise WebSocketError if the connection was already closed

  • Participants
  • Parent commits f3b4410
  • Branches stable

Comments (0)

Files changed (1)

File geventwebsocket/websocket.py

                 return -1
 
         def close(self):
-            if self.closed:
+            if self._sock is None:
                 return
+            self._sock.close()
+            self._sock = None
             RawIOBase.close(self)
-            self._sock = None
 
     def makefile(socket):
         return BufferedReader(SocketIO(socket))
 
     def send_frame(self, message, opcode):
         """Send a frame over the websocket with message as its payload"""
+        if self.socket is None:
+            raise WebSocketError('The connection was closed')
+
         header = chr(0x80 | opcode)
 
         if isinstance(message, unicode):
         """Close the websocket, sending the specified code and message"""
         if self.socket is not None:
             message = self._encode_text(message)
-            self.send_frame(struct.pack('!H%ds' % len(message), code, message), opcode=self.OPCODE_CLOSE)
-            self._close()
+            try:
+                self.send_frame(struct.pack('!H%ds' % len(message), code, message), opcode=self.OPCODE_CLOSE)
+            finally:
+                self._close()
 
     def _close(self):
         if self.socket is not None:
-            self.socket._sock.close()
             self.socket = None
             self._write = None
-            fobj = self.fobj
+            if not self._reading:
+                self.fobj.close()
             self.fobj = None
-            if not self._reading:
-                fobj.close()
+