From f68922d0434ae3368a961c6eb520d5c92074bbba Mon Sep 17 00:00:00 2001 From: Amog Kamsetty Date: Thu, 7 Jan 2021 13:40:44 -0800 Subject: [PATCH] [Tune] Improve error message for Session Detection (#13255) * Improve error message * log once --- python/ray/tune/session.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/python/ray/tune/session.py b/python/ray/tune/session.py index 3fbecf912..1bf05d2b9 100644 --- a/python/ray/tune/session.py +++ b/python/ray/tune/session.py @@ -1,6 +1,10 @@ from contextlib import contextmanager +import inspect import os import logging +import traceback + +from ray.util.debug import log_once logger = logging.getLogger(__name__) @@ -16,9 +20,17 @@ def is_session_enabled() -> bool: def get_session(): global _session if not _session: - logger.warning( - "Session not detected. You should not be calling this function " - "outside `tune.run` or while using the class API. ") + function_name = inspect.stack()[1].function + # Log traceback so the user knows where the offending func is called. + # E.g. ... -> tune.report() -> get_session() -> logger.warning(...) + # So we shouldn't print the last 2 functions in the trace. + stack_trace_str = "".join(traceback.extract_stack().format()[:-2]) + if log_once(stack_trace_str): + logger.warning( + "Session not detected. You should not be calling `{}` " + "outside `tune.run` or while using the class API. ".format( + function_name)) + logger.warning(stack_trace_str) return _session