Repository navigation
[rust] Write se-metadata.json atomically - #18200
Conversation
Several Selenium Manager processes can share one cache, most commonly when a test suite runs in parallel workers that each start a browser at the same moment. write_metadata wrote the file in place, so a process reading it in the meantime could see it truncated or half written. Since SeleniumHQ#18101 that read is reported as "Metadata file ... cannot be parsed: EOF while parsing ...", once per affected worker. Write the metadata to a sibling temporary file and rename it over se-metadata.json. The rename is atomic, so a reader sees either the previous or the new content.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
/agentic_review |
Code Review by Qodo
1. Concurrent threads can corrupt metadata
|
|
@UweKubosch Thanks for this fix! I pushed 2334c7b so the new test runs on Windows too: there |
titusfortner
left a comment
There was a problem hiding this comment.
Should be good to merge once the tests finish.
|
Code review by qodo was updated up to the latest commit 2334c7b |
🔗 Related Issues
Follow-up to #18101, which made unparseable metadata a warning. Related to #14898, whose cache lock covers driver and browser downloads but not the metadata file.
💥 What does this PR do?
write_metadatawrotese-metadata.jsonin place (fs::writetruncates, then writes). When several Selenium Manager processes share a cache, one of them can read the file between the truncate and the write, or halfway through it. This happens routinely in parallel test suites (e.g. Rails system tests with one worker per core), where every worker resolves the driver at the same moment. Since #18101 each such read logs:With 18 workers we see about six of these per run, each pointing at a different line and column, one for each half-written file a reader caught.
This PR writes the metadata to a sibling temporary file and renames it over
se-metadata.json. The rename is atomic within a filesystem, so a reader always sees either the previous or the new content.🔧 Implementation Notes
se-metadata.json.<pid>.tmpin the cache folder, so the rename never crosses filesystems. It is created withfs::write, so the file mode follows the umask exactly as before.tempfile::NamedTempFilewould create it0600, which would change behaviour for caches shared between users.fs::renamereplaces an existing target on Windows too (MoveFileExWwithMOVEFILE_REPLACE_EXISTING). If a reader holds the file open there, the rename can fail. The old metadata then stays in place and the existing warning is logged, which is no worse than today.concurrent_reader_never_sees_partially_written_metadata(Unix only) reads the metadata in a loop while another thread rewrites it 500 times, and checks that every read returns the full content and that no temporary files are left behind. Without the change tometadata.rsit fails on the first read; with it, it passed 10 runs out of 10 locally (macOS, arm64).🤖 AI assistance
write_metadata, the new test, and this description💡 Additional Considerations
The same race can, in principle, lose updates: two processes that read, modify and write the metadata concurrently each write back only their own view. That costs one extra version lookup and is out of scope here.
🔄 Types of changes