Skip to content

Conversation

@sfc-gh-tteixeira
Copy link
Contributor

@sfc-gh-tteixeira sfc-gh-tteixeira commented Jan 9, 2025

Add retry to watcher.util.path_modification_time to try and fix race condition. I have no guarantee this fixes it, but from the error message it seems right. And it doesn't hurt anyway.

Describe your changes

I just added some retries around an os.stat(path).st_mtime that could otherwise lead to a race condition:

Screenshot 2025-01-08 at 23 16 28

I think this race condition is more likely to happen when you have tooling that runs on save. In my case, I have a linter/formatter that fixes issues and saves the file again. So sometimes when we get to the os.stat(path).st_mtime line the file is in the process of being modified, leading to the error above.

BTW, as part of this work I also created two helper functions to do retries in the watcher code, since retries can be tricky.

GitHub Issue Link (if applicable)

Testing Plan

  • Explanation of why no additional tests are needed
  • Unit Tests (JS and/or Python): None. I am unable to reproduce the original problem. But the watcher code is well-tested enough that I'm confident this at least doesn't break anything.
  • E2E Tests: None
  • Any manual testing needed? Yes. Please try modifying your Streamlit app on Mac, Linux, Windows, using different editors (VS Code, PyCharm, etc). Even better if you can turn on auto-formatting plugins as well.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@vdonato vdonato added the security-assessment-completed Security assessment has been completed for PR label Jan 10, 2025
@sfc-gh-tteixeira sfc-gh-tteixeira enabled auto-merge (squash) January 13, 2025 20:30
@kmcgrady kmcgrady added change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Jan 14, 2025
@sfc-gh-tteixeira sfc-gh-tteixeira merged commit c685680 into develop Jan 14, 2025
40 of 43 checks passed
@sfc-gh-tteixeira sfc-gh-tteixeira deleted the retries branch January 14, 2025 21:01
edegp pushed a commit to edegp/streamlit that referenced this pull request Jan 19, 2025
…condition. (streamlit#10148)

Add retry to watcher.util.path_modification_time to **try** and fix race
condition. I have no guarantee this fixes it, but from the error message
it seems right. And it doesn't hurt anyway.

## Describe your changes

I just added some retries around an `os.stat(path).st_mtime` that could
otherwise lead to a race condition:

![Screenshot 2025-01-08 at 23 16
28](https://github.com/user-attachments/assets/194cd007-d3a4-4dff-84b2-083438f511b4)

I think this race condition is more likely to happen when you have
tooling that runs on save. In my case, I have a linter/formatter that
fixes issues and saves the file again. So sometimes when we get to the
`os.stat(path).st_mtime` line the file is in the process of being
modified, leading to the error above.

BTW, as part of this work I also created two helper functions to do
retries in the watcher code, since retries can be tricky.


## GitHub Issue Link (if applicable)

## Testing Plan

- ~~Explanation of why no additional tests are needed~~
- Unit Tests (JS and/or Python): **None**. I am unable to reproduce the
original problem. But the watcher code is well-tested enough that I'm
confident this at least doesn't break anything.
- E2E Tests: **None**
- Any manual testing needed? **Yes**. Please try modifying your
Streamlit app on Mac, Linux, Windows, using different editors (VS Code,
PyCharm, etc). Even better if you can turn on auto-formatting plugins as
well.

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants