-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attach traceback to Exception & Test disatpching process (#1036)
Summary: Partially fixes #969 ### Changes - Add `ExceptionWrapper` to attach traceback to the Exception - Reason: traceback is unserializable. So, it has to be passed by string - In order to provide informative Error message, pass name for each process like `dispatching process` and `worker process <id>`. - Add tests to validate Error propagation from the dispatching process - parametrize the tests - Fix a bug for `round_robin_demux` to return a list of DataPipe rather than a single DataPipe when `num_of_instances` is 1. Pull Request resolved: #1036 Reviewed By: NivekT Differential Revision: D43472709 Pulled By: ejguan fbshipit-source-id: e5c9e581ca881f523fb568b6f46bf16ecfc243d2
- Loading branch information
1 parent
6ca4402
commit f083d52
Showing
9 changed files
with
164 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import sys | ||
import traceback | ||
|
||
|
||
class KeyErrorMessage(str): | ||
r"""str subclass that returns itself in repr""" | ||
|
||
def __repr__(self): | ||
return self | ||
|
||
|
||
class ExceptionWrapper: | ||
r""" | ||
Wraps an exception with traceback to communicate across threads/processes | ||
""" | ||
|
||
def __init__(self, exc_info=None, where: str = "in background"): | ||
if exc_info is None: | ||
exc_info = sys.exc_info() | ||
self.exc_type = exc_info[0] | ||
self.exc_msg = "".join(traceback.format_exception(*exc_info)) | ||
self.where = where | ||
|
||
def reraise(self): | ||
r""" | ||
Reraises the wrapped exception in the current thread/process | ||
""" | ||
# Format a message such as: "Caught ValueError in DataLoader worker | ||
# process 2. Original Traceback:", followed by the traceback. | ||
msg = f"Caught {self.exc_type.__name__} {self.where}.\nOriginal {self.exc_msg}" | ||
if self.exc_type == KeyError: | ||
# KeyError calls repr() on its argument (usually a dict key). This | ||
# makes stack traces unreadable. It will not be changed in Python | ||
# (https://bugs.python.org/issue2651), so we work around it. | ||
msg = KeyErrorMessage(msg) | ||
elif getattr(self.exc_type, "message", None): | ||
# Some exceptions have first argument as non-str but explicitly | ||
# have message field | ||
raise self.exc_type(message=msg) | ||
try: | ||
exception = self.exc_type(msg) | ||
except TypeError: | ||
# If the exception takes multiple arguments, don't try to | ||
# instantiate since we don't know how to | ||
raise RuntimeError(msg) from None | ||
raise exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.