Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix multiprocessing hang on FSErrors
  • Loading branch information
Joseph Atkins-Turkish committed Jul 8, 2018
commit 93893c520a68b584f91a1ae2ad3c37fab69e78da
24 changes: 24 additions & 0 deletions fs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ class MissingInfoNamespace(AttributeError):

def __init__(self, namespace):
# type: (Text) -> None
self.namespace=namespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny pep8 thing, should be whitespace around =

msg = "namespace '{}' is required for this attribute"
super(MissingInfoNamespace, self).__init__(
msg.format(namespace)
)

def __reduce__(self):
return type(self), (self.namespace,)


@six.python_2_unicode_compatible
class FSError(Exception):
Expand Down Expand Up @@ -121,6 +125,10 @@ def new_func(*args, **kwargs):
raise cls(exc=e)
return new_func # type: ignore

def __reduce__(self):
return type(self), (self._msg, self.exc)


class PathError(FSError):
"""Base exception for errors to do with a path string.
"""
Expand All @@ -132,6 +140,9 @@ def __init__(self, path, msg=None):
self.path = path
super(PathError, self).__init__(msg=msg)

def __reduce__(self):
return type(self), (self.path, self._msg)


class NoSysPath(PathError):
"""The filesystem does not provide *sys paths* to the resource.
Expand All @@ -151,6 +162,9 @@ def __init__(self, path, purpose, msg=None):
self.purpose = purpose
super(NoURL, self).__init__(path, msg=msg)

def __reduce__(self):
return type(self), (self.path, self.purpose, self._msg)


class InvalidPath(PathError):
"""Path can't be mapped on to the underlaying filesystem.
Expand Down Expand Up @@ -184,6 +198,9 @@ def __init__(self,
self.errno = getattr(exc, "errno", None)
super(OperationFailed, self).__init__(msg=msg)

def __reduce__(self):
return type(self), (self.path, self.exc, self._msg)


class Unsupported(OperationFailed):
"""Operation not supported by the filesystem.
Expand Down Expand Up @@ -239,6 +256,9 @@ def __init__(self, path, exc=None, msg=None):
self.exc = exc
super(ResourceError, self).__init__(msg=msg)

def __reduce__(self):
return type(self), (self.path, self.exc, self._msg)


class ResourceNotFound(ResourceError):
"""Required resource not found.
Expand Down Expand Up @@ -325,7 +345,11 @@ class IllegalBackReference(ValueError):

def __init__(self, path):
# type: (Text) -> None
self.path = path
_msg = \
"path '{path}' contains back-references outside of filesystem"
_msg = _msg.format(path=path)
super(IllegalBackReference, self).__init__(_msg)

def __reduce__(self):
return type(self), (self.path,)
22 changes: 22 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import multiprocessing
import unittest

from six import text_type
Expand All @@ -26,6 +27,27 @@ def test_unsupported(self):
"not supported"
)

def test_raise_in_multiprocessing(self):
# Without the __reduce__ methods in FSError subclasses, this test will hang forever.
tests = [
[errors.ResourceNotFound, 'some_path'],
[errors.FilesystemClosed],
[errors.CreateFailed],
[errors.NoSysPath, 'some_path'],
[errors.NoURL, 'some_path', 'some_purpose'],
[errors.Unsupported]
]
try:
pool = multiprocessing.Pool(1)
for args in tests:
with self.assertRaises(args[0]):
pool.apply(_multiprocessing_test_task, args)
finally:
pool.close()

def _multiprocessing_test_task(err, *args):
raise err(*args)


class TestCreateFailed(unittest.TestCase):

Expand Down