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: Treat a registry entry as stale when nothing listens on its port - #2936
herb dev records each server in ~/.herb/dev-servers/<pid>.json and treats an entry as alive while Process.kill(0, pid) succeeds. A server stopped with SIGKILL never removes its entry. Once the operating system hands that PID to an unrelated process, the next herb dev for the project exits 1 with "Herb dev server is already running for this project", and herb dev stop sends INT to the unrelated process.
This shows up in containers: PIDs start over from 1 after each restart, and docker stop ends with SIGKILL once the grace period passes. Under a process manager such as foreman, the exit stops every other process too.
Reproduce on main inside a container whose PID 1 runs as the same user:
echo'{"pid":1,"port":8592,"project":"/app","started_at":"2026-01-01T00:00:00Z","kind":"standalone"}'>~/.herb/dev-servers/1.json
bundle exec herb dev # in /app: exits 1, "already running (PID: 1, port: 8592)"
Changes
An entry is alive only when its port accepts a connection.ServerEntry#alive? now also requires a TCP connection to the recorded port on 127.0.0.1 to succeed (ServerEntry.port_listening?). Both standalone and embedded servers bind that port on 0.0.0.0, so the check covers both, needs no platform-specific code, and leaves the entry format unchanged. A reused PID without a listener on the port now reads as stale, and ServerEntry.all removes it as before.
Server#start binds before it saves the entry.ServerEntry.all deletes every entry that is not alive, so with the new check a second herb dev running between the save and the bind could have deleted the entry of a server still starting. When the entry cannot be saved, start closes the port again before it raises, so an embedding host that rescues the failure keeps no open listener.
ServerEntry.all re-reads a stale entry before it deletes it. A server whose PID was reused saves its entry at the same path as the stale one. When that save lands while the scan probes the stale entry, the scan now keeps the file, because it deletes it only when it still holds the entry it probed.
boot_test.rb: "boots nothing when a standalone server owns the project" registered its own PID on port 65001 with nothing listening. It now opens a real listener for that port, since an entry without one is stale.
An open port does not prove that the recorded PID owns it. Both start paths run ServerEntry.all before they bind (Runner#run, Herb::Dev.boot), so while a dead server's port is free, its stale entry is removed before another Herb server can take that port. Two cases remain: a reused PID while a process outside Herb listens on the recorded port, and an embedded server that takes the port of a server that dies in the moment between the embedded server's registry scan and its port probe, followed by reuse of the dead server's PID. The moment between the re-read and the delete in ServerEntry.all remains as well. Closing those needs proof that the recorded process owns the listener and its entry, such as a lock the server holds on its entry file, which this change leaves out.
Recording the process start time next to the PID was the other option. It needs /proc on Linux and ps on macOS, which slim container images often lack, and the existing started_at of an embedded entry is later than its host process start, so it cannot stand in for it.
Testing
New test/dev/server_entry_test.rb: an entry with a live PID but no listener on its port is stale and its file is removed (fails on main), the same entry with a listener is found, and a stale entry whose file is replaced while it is probed keeps the replacement (fails without the re-read).
New test in boot_test.rb: an entry whose PID belongs to an unrelated live process, on the port the embedded server then binds, is removed when that server boots (fails on main).
New test in server_test.rb: a server whose entry cannot be saved (a directory stands at the entry path) raises and leaves its port free (fails without the cleanup).
test/dev/*_test.rb: 119 runs pass, with one existing skip in protocol_test.rb.
RuboCop on the touched files, rake rbs_inline (signature regenerated), rbs -Isig validate, steep check, and srb tc pass.
…ts port
An entry in ~/.herb/dev-servers counted as alive while its PID answered Process.kill(0, pid). A server stopped with SIGKILL never removes its entry, and once the PID is reused by an unrelated process, the next herb dev exits with "already running" and herb dev stop sends INT to that process. In a container this happens after a restart, because PIDs start over from 1.
An entry now counts as alive only when its process exists and a TCP connection to its port on 127.0.0.1 succeeds. Server#start binds the port before it saves the entry, so a concurrent ServerEntry.all no longer deletes the entry of a server that is still starting, and it closes the port again when the entry cannot be saved.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No actionable comments were generated in the recent review.
Walkthrough
Server entries are considered alive only when the recorded process is alive and its port accepts a TCP connection on localhost. ServerEntry.port_listening? checks the port, and the RBS declaration and tests cover this behavior. Server#start now opens the listener before saving the entry and closes it if saving raises a StandardError.
Stale dev-server registry entries are now removed when nothing listens on their port. A narrow race remains in which a replacement entry saved at the same PID path could be deleted, leaving a live server temporarily undiscoverable. It is unlikely and low impact, so the change is mergeable with owner awareness.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: a registry entry is treated as stale when nothing listens on its recorded port. It matches the changes to ServerEntry#alive? and the port_listening? check, so the titl…
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.
Description check
The description explains the problem (stale registry entries after SIGKILL and PID reuse), the liveness change to ServerEntry#alive? that also requires a TCP connection to the recorded port, the bind-…
Full details: Title check
Explanation
The title names the main change: a registry entry is treated as stale when nothing listens on its recorded port. It matches the changes to ServerEntry#alive? and the port_listening? check, so the title is accurate and specific.
Full details: Description check
Explanation
The description explains the problem (stale registry entries after SIGKILL and PID reuse), the liveness change to ServerEntry#alive? that also requires a TCP connection to the recorded port, the bind-before-save ordering in Server#start, the stale-entry re-read in ServerEntry.all, and the tests added. It matches the changeset closely, so the check passes.
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.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 306e1a34-cb7a-48df-b0bc-4b2a31ebfd1a
Commits
Reviewing files that changed from the base of the PR and between 94b0ba7 and 661fc2a.
Files selected for processing: 3
lib/herb/dev/server_entry.rb
sig/herb/dev/server_entry.rbs
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; 3 remain after this review.
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: 48cee580-071a-49f8-9f63-3dd537ddecc3
Commits
Reviewing files that changed from the base of the PR and between e7f3706 and 94b0ba7.
Files selected for processing: 6
lib/herb/dev/server.rb
lib/herb/dev/server_entry.rb
sig/herb/dev/server_entry.rbs
test/dev/boot_test.rb
test/dev/server_entry_test.rb
test/dev/server_test.rb
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Data Integrity & Integration · Quick win
Pruning deletes registry files without checking their current contents.
ServerEntry.all probes the entry it loaded earlier, then deletes P.json by path. It does not reread the file. If a replacement entry for the same PID is saved after the scan loads the old entry and before the delete, the scan can remove the replacement. The live server then disappears from find_by_port, find_by_project, and stop_all.
The trigger requires the same PID to write a new registry entry during an active scan. That requires PID reuse after the earlier server exits, or a second server started in the same process. The source does not show a coordination point between the scan and save.
Re-read the entry immediately before deletion and remove the file only if its contents still match the probed entry. Alternatively, guard deletion with a lock shared by save and all.
The reason will be displayed to describe this comment to others. Learn more.
Good catch. With the port check, a live PID with a closed port is exactly what a reused-PID server looks like before it saves its entry, so this change made the race reachable. Fixed in 661fc2a: ServerEntry.all now re-reads the file and deletes it only when it still holds the entry it probed, and a new test in server_entry_test.rb writes a replacement during the probe and checks that it survives. The gap between that re-read and the delete remains. Closing it fully needs the lock left out in the description.
The reason will be displayed to describe this comment to others. Learn more.
@viktorianer Thanks for adding the re-read and test. As you noted, a save can still occur between the re-read and deletion. I’m not marking the finding as addressed.
If you found this review helpful, would you consider giving us a shout-out on X?
ServerEntry.all deleted a stale entry's file by path after probing the entry it had loaded earlier. With the port check, a live PID whose port is closed now reads as stale, which is also the state of a server whose PID was reused: it binds a different port and saves an entry at the same path. When that save lands during the probe, the scan deleted the new entry.
The scan now deletes the file only when it still holds the entry it probed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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-reloadingrbsRBS type signatures in sig/rubyRuby 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.
herb devrecords each server in~/.herb/dev-servers/<pid>.jsonand treats an entry as alive whileProcess.kill(0, pid)succeeds. A server stopped withSIGKILLnever removes its entry. Once the operating system hands that PID to an unrelated process, the nextherb devfor the project exits 1 with "Herb dev server is already running for this project", andherb dev stopsendsINTto the unrelated process.This shows up in containers: PIDs start over from 1 after each restart, and
docker stopends withSIGKILLonce the grace period passes. Under a process manager such as foreman, the exit stops every other process too.Reproduce on
maininside a container whose PID 1 runs as the same user:Changes
ServerEntry#alive?now also requires a TCP connection to the recorded port on127.0.0.1to succeed (ServerEntry.port_listening?). Both standalone and embedded servers bind that port on0.0.0.0, so the check covers both, needs no platform-specific code, and leaves the entry format unchanged. A reused PID without a listener on the port now reads as stale, andServerEntry.allremoves it as before.Server#startbinds before it saves the entry.ServerEntry.alldeletes every entry that is not alive, so with the new check a secondherb devrunning between the save and the bind could have deleted the entry of a server still starting. When the entry cannot be saved,startcloses the port again before it raises, so an embedding host that rescues the failure keeps no open listener.ServerEntry.allre-reads a stale entry before it deletes it. A server whose PID was reused saves its entry at the same path as the stale one. When that save lands while the scan probes the stale entry, the scan now keeps the file, because it deletes it only when it still holds the entry it probed.boot_test.rb: "boots nothing when a standalone server owns the project" registered its own PID on port 65001 with nothing listening. It now opens a real listener for that port, since an entry without one is stale.An open port does not prove that the recorded PID owns it. Both start paths run
ServerEntry.allbefore they bind (Runner#run,Herb::Dev.boot), so while a dead server's port is free, its stale entry is removed before another Herb server can take that port. Two cases remain: a reused PID while a process outside Herb listens on the recorded port, and an embedded server that takes the port of a server that dies in the moment between the embedded server's registry scan and its port probe, followed by reuse of the dead server's PID. The moment between the re-read and the delete inServerEntry.allremains as well. Closing those needs proof that the recorded process owns the listener and its entry, such as a lock the server holds on its entry file, which this change leaves out.Recording the process start time next to the PID was the other option. It needs
/procon Linux andpson macOS, which slim container images often lack, and the existingstarted_atof an embedded entry is later than its host process start, so it cannot stand in for it.Testing
test/dev/server_entry_test.rb: an entry with a live PID but no listener on its port is stale and its file is removed (fails onmain), the same entry with a listener is found, and a stale entry whose file is replaced while it is probed keeps the replacement (fails without the re-read).boot_test.rb: an entry whose PID belongs to an unrelated live process, on the port the embedded server then binds, is removed when that server boots (fails onmain).server_test.rb: a server whose entry cannot be saved (a directory stands at the entry path) raises and leaves its port free (fails without the cleanup).test/dev/*_test.rb: 119 runs pass, with one existing skip inprotocol_test.rb.rake rbs_inline(signature regenerated),rbs -Isig validate,steep check, andsrb tcpass.