Skip to content

Commit 0a8c342

Browse files
authored
Merge pull request #1492 from Giskard-AI/feature/gsk-1947-typeerror-while-running-the-scan
Fix the excepthook for threads
2 parents f93407d + 49d49a5 commit 0a8c342

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

giskard/utils/analytics_collector.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import platform
55
import sys
66
import threading
7-
import traceback
7+
from traceback import TracebackException
8+
from types import TracebackType
89
import uuid
910
from functools import wraps
10-
from threading import Lock
11-
from typing import Dict, Optional
11+
from threading import ExceptHookArgs, Lock
12+
from typing import Dict, Optional, Type
1213

1314
import requests
1415
from mixpanel import Mixpanel
@@ -82,8 +83,8 @@ def get_dataset_properties(dataset):
8283
}
8384

8485

85-
def _report_error(e, error_type = "python error"):
86-
exc = traceback.TracebackException.from_exception(e)
86+
def _report_error(e, error_type="python error"):
87+
exc = TracebackException.from_exception(e)
8788
is_giskard_error = False
8889
for frame in exc.stack:
8990
if not is_giskard_error and "giskard" in frame.filename:
@@ -225,7 +226,7 @@ def initialize_geo(self):
225226

226227

227228
def add_exception_hook(original_hook=None):
228-
def _exception_hook(type, value, traceback):
229+
def _exception_hook(type: Type[BaseException], value: BaseException, traceback: Optional[TracebackType]):
229230
try:
230231
_report_error(value)
231232
except BaseException: # noqa NOSONAR
@@ -239,8 +240,23 @@ def _exception_hook(type, value, traceback):
239240
return _exception_hook
240241

241242

243+
def add_thread_exception_hook(original_hook=None):
244+
def _thread_exception_hook(args: ExceptHookArgs):
245+
try:
246+
_report_error(args.exc_value)
247+
except BaseException: # noqa NOSONAR
248+
pass
249+
250+
if original_hook:
251+
return original_hook(args)
252+
else:
253+
return sys.__excepthook__(args.exc_type, args.exc_value, args.exc_traceback)
254+
255+
return _thread_exception_hook
256+
257+
242258
if not settings.disable_analytics:
243259
sys.excepthook = add_exception_hook(sys.excepthook)
244-
threading.excepthook = add_exception_hook(threading.excepthook)
260+
threading.excepthook = add_thread_exception_hook(threading.excepthook)
245261

246262
analytics = GiskardAnalyticsCollector()

0 commit comments

Comments
 (0)