Repository: streamlit/streamlit

Issue:
Clear cache results in: Thread 'MainThread': missing ScriptRunContext
### Checklist

- [X] I have searched the [existing issues](https://github.com/streamlit/streamlit/issues) for similar issues.
- [X] I added a very descriptive title to this issue.
- [X] I have provided sufficient information below to help reproduce this issue.

### Summary

When Clearing the Streamlit Cache using the Hamburger menu (or pressing `C`), I get a `Thread 'MainThread': missing ScriptRunContext` error, even for the most simple programs.

### Reproducible Code Example

```Python
import streamlit as st

st.title("test")
```


### Steps To Reproduce

1. In the browser, use the Hamburger menu to press `Clear cache` or just press `C` on you keyboard.

### Expected Behavior

No errors

### Current Behavior

Error message:
```
2023-03-27 12:05:17.219 Thread 'MainThread': missing ScriptRunContext
```

### Is this a regression?

- [ ] Yes, this used to work in a previous version.

### Debug info

- Streamlit version: 1.20.0
- Python version: 3.9.16
- Operating System: MacOS Ventura 13.2.1
- Browser: Google Chrome
- Virtual environment: pipenv


### Additional Information

_No response_

### Are you willing to submit a PR?

- [ ] Yes, I am willing to submit a PR!


Discussion:
@sTomerG Thanks for reporting this issue. I can confirm this issue. I believe @kajarenc is already looking into this.

Resolving patch:
diff --git a/lib/streamlit/runtime/metrics_util.py b/lib/streamlit/runtime/metrics_util.py
--- a/lib/streamlit/runtime/metrics_util.py
+++ b/lib/streamlit/runtime/metrics_util.py
@@ -277,7 +277,7 @@ def wrapped_func(*args, **kwargs):
         # get_script_run_ctx gets imported here to prevent circular dependencies
         from streamlit.runtime.scriptrunner import get_script_run_ctx
 
-        ctx = get_script_run_ctx()
+        ctx = get_script_run_ctx(suppress_warning=True)
 
         tracking_activated = (
             ctx is not None
diff --git a/lib/streamlit/runtime/scriptrunner/script_run_context.py b/lib/streamlit/runtime/scriptrunner/script_run_context.py
--- a/lib/streamlit/runtime/scriptrunner/script_run_context.py
+++ b/lib/streamlit/runtime/scriptrunner/script_run_context.py
@@ -29,7 +29,6 @@
 
 LOGGER: Final = get_logger(__name__)
 
-
 UserInfo: TypeAlias = Dict[str, Optional[str]]
 
 
@@ -141,8 +140,12 @@ def add_script_run_ctx(
     return thread
 
 
-def get_script_run_ctx() -> Optional[ScriptRunContext]:
+def get_script_run_ctx(suppress_warning: bool = False) -> Optional[ScriptRunContext]:
     """
+    Parameters
+    ----------
+    suppress_warning : bool
+        If True, don't log a warning if there's no ScriptRunContext.
     Returns
     -------
     ScriptRunContext | None
@@ -153,9 +156,9 @@ def get_script_run_ctx() -> Optional[ScriptRunContext]:
     ctx: Optional[ScriptRunContext] = getattr(
         thread, SCRIPT_RUN_CONTEXT_ATTR_NAME, None
     )
-    if ctx is None and runtime.exists():
-        # Only warn about a missing ScriptRunContext if we were started
-        # via `streamlit run`. Otherwise, the user is likely running a
+    if ctx is None and runtime.exists() and not suppress_warning:
+        # Only warn about a missing ScriptRunContext if suppress_warning is False, and
+        # we were started via `streamlit run`. Otherwise, the user is likely running a
         # script "bare", and doesn't need to be warned about streamlit
         # bits that are irrelevant when not connected to a session.
         LOGGER.warning("Thread '%s': missing ScriptRunContext", thread.name)


Test patch: