You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dev Server: Save a registry entry atomically - #2937
ServerEntry#save writes ~/.herb/dev-servers/<pid>.json in place with File.write, which truncates the file before it writes the JSON. Every herb dev, herb dev status, herb dev stop and embedded boot scans the registry with ServerEntry.all. A scan that reads the file in that moment gets an empty or partial file, load_file rescues the JSON::ParserError and deletes the file, and the live server drops out of the registry for good, since a server saves its entry only once when it starts.
Changes
ServerEntry#save writes atomically. The JSON goes to a temporary file next to the entry, named so the *.json glob of ServerEntry.all never picks it up, and File.rename moves it over the entry path. The rename is atomic on POSIX, so a scan sees the old entry or the new one, never a partial file.
A failed save leaves no temporary file. When the write or the rename raises, save removes the temporary file and re-raises.
Testing
New test/dev/server_entry_test.rb:
A thread reads the entry file in a loop while the main thread saves the same entry 500 times, and counts the reads that differ from the entry's JSON. On main it failed 10 of 10 runs, and a separate run counted 235 partial reads. With this change, 10 of 10 runs pass.
With a directory at the entry path, save raises and leaves no temporary file. Fails without the cleanup.
test/dev/*_test.rb: 117 runs pass, with one existing skip in protocol_test.rb. RuboCop on the touched files, rake rbs_inline (signatures unchanged), rbs -Isig validate, steep check and srb tc pass.
This change is independent of #2936 and complements it: #2936 decides when an entry is stale, and this one keeps a live entry from being read as unparseable. Both add test/dev/server_entry_test.rb, so whichever merges second resolves an add/add conflict by keeping both sets of tests. With both merged and the tests combined, test/dev passes, including #2936's test that a directory at the entry path makes Server#start raise.
ServerEntry#save wrote the entry JSON in place with File.write, which truncates the file before it writes. A ServerEntry.all running in another process at that moment reads an empty or partial file, load_file rescues the JSON::ParserError and deletes the file, and the live server drops out of the registry for good, since a server saves its entry once when it starts. `herb dev stop`, `herb dev status` and the next `herb dev` for the project no longer see it.
The entry is now written to a temporary file in the same directory and renamed over the entry path, which is atomic on POSIX, so a scan sees either the old entry or the new one. A failed write or rename removes the temporary file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ServerEntry#save writes JSON to a temporary file whose name includes the destination path, process ID, and thread object ID. It then renames the temporary file to the destination. If writing or renaming raises a StandardError, it removes the temporary file and re-raises the error. Tests cover concurrent reads during repeated saves and cleanup after a failed save.
This change makes registry entry saves atomic, so readers no longer see partially written files. The remaining risk is confined to a test that briefly creates a directory in the real registry folder. It could momentarily disturb other local processes that read the registry while the test runs. This is safe to merge once the test is isolated.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "Dev Server: Save a registry entry atomically" matches the main change. ServerEntry#save now writes JSON to a temporary file and renames it over the entry path, so readers never see a partia…
Description check
The description matches the changeset. It explains the partial-read problem in ServerEntry#save, describes the temporary-file-and-rename change, cleanup on failure, the new test file test/dev/server_e…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "Dev Server: Save a registry entry atomically" matches the main change. ServerEntry#save now writes JSON to a temporary file and renames it over the entry path, so readers never see a partial file. The title is short, specific, and names the primary change.
Full details: Description check
Explanation
The description matches the changeset. It explains the partial-read problem in ServerEntry#save, describes the temporary-file-and-rename change, cleanup on failure, the new test file test/dev/server_entry_test.rb, and the relation to #2936.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
Some tools did not complete. Review the errors below.
Brakeman (8.1.0)
Brakeman reported 1 analysis error(s); security analysis is incomplete
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 1dd18884-c43e-41de-9042-f5523bb8a81d
Commits
Reviewing files that changed from the base of the PR and between 9b42ca5 and 51b948f.
Files selected for processing: 2
lib/herb/dev/server_entry.rb
test/dev/server_entry_test.rb
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Stability & Availability · Quick win
Isolate the registry fixtures from the user's registry.
This creates a *.json directory inside ~/.herb/dev-servers. The test helper does not redirect that directory. If another process calls ServerEntry.all before cleanup, load_file reads the directory and raises an unhandled Errno::EISDIR on Linux. The registry query then fails. (raw.githubusercontent.com)
Use a temporary ServerEntry::SERVERS_DIR for both tests and restore it afterward. This preserves the rename-failure test without exposing fixtures to user processes.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dev-serverHerb dev server and template hot-reloadingrubyRuby source for the gem and its librariesrubygemThe herb RubyGem and its packaging
1 participant
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ServerEntry#savewrites~/.herb/dev-servers/<pid>.jsonin place withFile.write, which truncates the file before it writes the JSON. Everyherb dev,herb dev status,herb dev stopand embedded boot scans the registry withServerEntry.all. A scan that reads the file in that moment gets an empty or partial file,load_filerescues theJSON::ParserErrorand deletes the file, and the live server drops out of the registry for good, since a server saves its entry only once when it starts.Changes
ServerEntry#savewrites atomically. The JSON goes to a temporary file next to the entry, named so the*.jsonglob ofServerEntry.allnever picks it up, andFile.renamemoves it over the entry path. The rename is atomic on POSIX, so a scan sees the old entry or the new one, never a partial file.saveremoves the temporary file and re-raises.Testing
New
test/dev/server_entry_test.rb:mainit failed 10 of 10 runs, and a separate run counted 235 partial reads. With this change, 10 of 10 runs pass.saveraises and leaves no temporary file. Fails without the cleanup.test/dev/*_test.rb: 117 runs pass, with one existing skip inprotocol_test.rb. RuboCop on the touched files,rake rbs_inline(signatures unchanged),rbs -Isig validate,steep checkandsrb tcpass.Relation to #2936
This change is independent of #2936 and complements it: #2936 decides when an entry is stale, and this one keeps a live entry from being read as unparseable. Both add
test/dev/server_entry_test.rb, so whichever merges second resolves an add/add conflict by keeping both sets of tests. With both merged and the tests combined,test/devpasses, including #2936's test that a directory at the entry path makesServer#startraise.