New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race conditions resulting in KeyError in module reloading #6404
Comments
Thanks for providing the reproduction code, I was able to see the attribute error after tweaking the edit script to run a bit faster. Since there are some reasons to want the path to be updated, it might not be viable to switch to |
Thanks for the fast response. What are the reasons for keeping the sys modifications? Is it only to allow the I do understand you can't just switch behaviour, some user code could break for sure. Myself I would be happy with a configuration option to skip sys modifications that defaults to current behaviour if that's possible. Depends on how deep this goes? |
If it's only to set
|
The only use I could find mentioned in the commit history is setting the name, but that is used for both the I tried out switching to run_module (and removing the sys path modification), but when running the repro program I continued to get the errors. This'll take further investigation, but I don't want to get too nerd-sniped by it right now (already spent more time revisiting the import machinery documentation than I intended). If you want to try out some changes and get something that works on the repro repo, I'll gladly work with you to verify that it still works for the known usecases. Otherwise, I will probably revisit this in a week or two, when I have slightly less competing for my time and attention. |
Hi I know this is a little old now but I wanted to +1 that I am encountering this same issue. For me it is not a breaking issue as far as I can tell |
Hi. I've run into this issue on a project that uses the streamlit module watcher to reload the page from a thread (by modifying an imported file) To try and debug I excepted the KeyError in streamlits script_runner.py (~L555 on the exec call is where it happens): with modified_sys_path(self._main_script_path), self._set_execing_flag():
# Run callbacks for widgets whose values have changed.
if rerun_data.widget_states is not None:
self._session_state.on_script_will_rerun(rerun_data.widget_states)
ctx.on_script_start()
prep_time = timer() - start_time
print("Y", module.__dict__.keys())
exec(code, module.__dict__)
self._session_state.maybe_check_serializable()
self._session_state[SCRIPT_RUN_WITHOUT_ERRORS_KEY] = True
except KeyError:
print("X", module.__dict__.keys())
raise Which logs something like this:
Since I want to rerun anyway the easiest solution I found was to except the KeyError at the root of my streamlit app and call st.rerun() or similar - it would be good to have this more thread safe though. |
Checklist
Summary
The streamlit module reloading has race conditions that lead to "KeyError: 'mylib'" from deep inside python import machinery. It has been mentioned in passing in other issues before but here I'm making it reproducible.
Reproducible Code Example
Steps To Reproduce
Expected Behavior
No exceptions.
Current Behavior
Is this a regression?
Debug info
Additional Information
I won't pretend to comprehend the full picture here, this is tricky stuff! Some clues I've found:
The KeyError issue was mentioned in passing in the comments on this issue in October 2022 but that issue is about another topic:
#4595
A possible source of the problem was pointed out in this issue in 2020:
#1430
The problem probably stems from non-threadsafe manipulation of sys.modules such as in this code:
https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/runtime/scriptrunner/script_runner.py#L558
This is similar to what you can optionally have the runpy module do with alter_sys=True, explained in the python documentation as not threadsafe here:
https://docs.python.org/3/library/runpy.html#runpy.run_module
A better solution then might be using runpy.run_module with alter_sys=False. This will however have some side effects, as you mention in comments in script_runner.py. I may be willing to help out fixing this but these side effect issues seem to run deep so the streamlit core developers would need to give their blessing to the approach.
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: