Skip to content

Dev Server: Save a registry entry atomically - #2937

Open
viktorianer wants to merge 1 commit into
marcoroth:mainfrom
viktorianer:dev-server-atomic-save
Open

viktorianer wants to merge 1 commit into
marcoroth:mainfrom
viktorianer:dev-server-atomic-save

Conversation

@viktorianer

Copy link
Copy Markdown
Contributor

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.

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/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>
@github-actions github-actions Bot added ruby Ruby source for the gem and its libraries rubygem The herb RubyGem and its packaging dev-server Herb dev server and template hot-reloading labels Nov 26, 2026
@viktorianer
viktorianer marked this pull request as ready for review November 26, 2026 21:45
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Low · up to 51b94

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.


test "a save that fails leaves no temporary file behind" do
entry = Herb::Dev::ServerEntry.new(pid: Process.pid, port: 65_002, project: "/tmp/project")
FileUtils.mkdir_p(entry_path)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dev-server Herb dev server and template hot-reloading ruby Ruby source for the gem and its libraries rubygem The herb RubyGem and its packaging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant