Skip to content

Commit

Permalink
Use select() before accept() to prevent infinite blocking. refs #117
Browse files Browse the repository at this point in the history
  • Loading branch information
Barthelemy Dagenais committed Feb 10, 2013
1 parent c2c57ce commit cdf2b6f
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions py4j-python/src/py4j/java_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
from pydoc import ttypager
import select
import socket
from subprocess import Popen, PIPE
import sys
Expand Down Expand Up @@ -1014,19 +1015,26 @@ def run(self):
logger.info('Socket listening on {0}'.
format(smart_decode(self.server_socket.getsockname())))

read_list = [self.server_socket]
while not self.is_shutdown:
socket_instance, _ = self.server_socket.accept()
input = socket_instance.makefile('rb', 0)
connection = CallbackConnection(self.pool, input,
socket_instance,
self.gateway_client)
with self.lock:
if not self.is_shutdown:
self.connections.append(connection)
connection.start()
else:
quiet_shutdown(connection.socket)
quiet_close(connection.socket)
readable, writable, errored = select.select(read_list, [], [])

if self.is_shutdown:
break

for s in readable:
socket_instance, _ = self.server_socket.accept()
input = socket_instance.makefile('rb', 0)
connection = CallbackConnection(self.pool, input,
socket_instance,
self.gateway_client)
with self.lock:
if not self.is_shutdown:
self.connections.append(connection)
connection.start()
else:
quiet_shutdown(connection.socket)
quiet_close(connection.socket)
except Exception:
if self.is_shutdown:
logger.info('Error while waiting for a connection.')
Expand Down

0 comments on commit cdf2b6f

Please sign in to comment.