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

[RPC] Better handle tempdir if subprocess killed. #3574

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions python/tvm/contrib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ class TempDirectory(object):

Automatically removes the directory when it went out of scope.
"""
def __init__(self):
self.temp_dir = tempfile.mkdtemp()
def __init__(self, custom_path=None):
if custom_path:
os.mkdir(custom_path)
self.temp_dir = custom_path
else:
self.temp_dir = tempfile.mkdtemp()
self._rmtree = shutil.rmtree

def remove(self):
Expand Down Expand Up @@ -69,15 +73,20 @@ def listdir(self):
return os.listdir(self.temp_dir)


def tempdir():
def tempdir(custom_path=None):
"""Create temp dir which deletes the contents when exit.

Parameters
----------
custom_path : str, optional
Manually specify the exact temp dir path

Returns
cbalint13 marked this conversation as resolved.
Show resolved Hide resolved
-------
temp : TempDirectory
The temp directory object
"""
return TempDirectory()
return TempDirectory(custom_path)


class FileLock(object):
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def load(path, fmt=""):
_cc.create_shared(path + ".so", path)
path += ".so"
elif path.endswith(".tar"):
tar_temp = _util.tempdir()
tar_temp = _util.tempdir(custom_path=path.replace('.tar', ''))
_tar.untar(path, tar_temp.temp_dir)
files = [tar_temp.relpath(x) for x in tar_temp.listdir()]
_cc.create_shared(path + ".so", files)
Expand Down
20 changes: 12 additions & 8 deletions python/tvm/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@

logger = logging.getLogger('RPCServer')

def _server_env(load_library):
def _server_env(load_library, work_path=None):
"""Server environment function return temp dir"""
temp = util.tempdir()
if work_path:
temp = work_path
else:
temp = util.tempdir()

# pylint: disable=unused-variable
@register_func("tvm.rpc.server.workpath")
Expand All @@ -76,16 +79,15 @@ def load_module(file_name):
temp.libs = libs
return temp


def _serve_loop(sock, addr, load_library):
def _serve_loop(sock, addr, load_library, work_path=None):
"""Server loop"""
sockfd = sock.fileno()
temp = _server_env(load_library)
temp = _server_env(load_library, work_path)
base._ServerLoop(sockfd)
temp.remove()
if not work_path:
temp.remove()
logger.info("Finish serving %s", addr)


def _parse_server_opt(opts):
# parse client options
ret = {}
Expand Down Expand Up @@ -196,9 +198,10 @@ def _accept_conn(listen_sock, tracker_conn, ping_period=2):
raise exc

# step 3: serving
work_path = util.tempdir()
logger.info("connection from %s", addr)
server_proc = multiprocessing.Process(target=_serve_loop,
args=(conn, addr, load_library))
args=(conn, addr, load_library, work_path))
server_proc.deamon = True
server_proc.start()
# close from our side.
Expand All @@ -208,6 +211,7 @@ def _accept_conn(listen_sock, tracker_conn, ping_period=2):
if server_proc.is_alive():
logger.info("Timeout in RPC session, kill..")
server_proc.terminate()
work_path.remove()


def _connect_proxy_loop(addr, key, load_library):
Expand Down