Skip to content

Bug in clearing cache of cached function via .clear() in streamlit==1.20.0 #6310

@a-slice-of-py

Description

@a-slice-of-py

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

In streamlit==1.20.0 I am unable to clear cache of a cached functions via .clear() (this was possible in streamlit 1.19.0).

I suspect this is due to changes made in latest streamlit version to streamlit\runtime\caching\, and in particular to storage module: now, indeed, even if st.cache_data is used without any custom argument - and, then, with persist=None - streamlit try to clear also local cache, and searching for the local cache folder eventually raise the error FileNotFoundError: [Errno 2] No such file or directory / FileNotFoundError: [WinError 3] The system cannot find the path specified

Reproducible Code Example

import streamlit as st
import time

@st.cache_data
def dummy():
    st.write('Hello from dummy function! I will wait for 5 seconds...')
    time.sleep(5)
    st.write('Done!')

left, right = st.columns(2)
with left:
    if st.button('Run dummy function'):
        dummy()
with right:
    if st.button('Clear cache'):
        dummy.clear()

Steps To Reproduce

  1. pip install streamlit==1.20.0
  2. Copy above snippet to app.py
  3. streamlit run app.py
  4. Click a first time on "Run dummy function" button (-> see cache spinner)
  5. Click a second time on "Run dummy function" (-> confirm that function has been cached)
  6. Click on "Clear cache" button (-> FileNotFoundError should be raised)

Expected Behavior

As in previous streamlit 1.19.0 version, cache can be cleared without searching for local cache if persist = None | False

Current Behavior

Cannot properly clear cache due to FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\XXXXXXXXX\\.streamlit\\cache' (on local Windows machine) or FileNotFoundError: [Errno 2] No such file or directory: '/root/.streamlit/cache' (on remote containerized execution)

Full traceback:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\XXXXXXXXX\\.streamlit\\cache'

File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "XXXXXXXXX\app.py", line 16, in <module>
    dummy.clear()
File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 331, in clear
    cache.clear()
File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 137, in clear
    self._clear()
File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\caching\cache_data_api.py", line 659, in _clear
    self.storage.clear()
File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\caching\storage\in_memory_cache_storage_wrapper.py", line 107, in clear
    self._persist_storage.clear()
File "C:\Users\XXXXXXXXX\Miniconda3\envs\hc\lib\site-packages\streamlit\runtime\caching\storage\local_disk_cache_storage.py", line 174, in clear
    for file_name in os.listdir(cache_dir):

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.10.8
  • Operating System: Windows 10 Enterprise v22H2 build 19045.2604
  • Browser: Brave
  • Virtual environment: miniconda environment

Additional Information

No response

Are you willing to submit a PR?

  • Yes, I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:P2Medium-high priority - fix within one monthtype:bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions