Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/herb/dev/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ def initialize(port: DEFAULT_PORT, project_path: nil, kind: "standalone")

#: () -> void
def start
@entry = ServerEntry.new(pid: Process.pid, port: @port, project: @project_path, kind: @kind)
@entry.save
@server = TCPServer.new("0.0.0.0", @port)
@entry = ServerEntry.new(pid: Process.pid, port: @port, project: @project_path, kind: @kind)

begin
@entry.save
rescue StandardError
safely_close(@server)
raise
end

@accept_thread = Thread.new do
loop do
Expand Down
28 changes: 22 additions & 6 deletions lib/herb/dev/server_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "json"
require "fileutils"
require "socket"

module Herb
module Dev
Expand Down Expand Up @@ -46,7 +47,7 @@ def remove

#: () -> bool
def alive?
self.class.process_alive?(pid)
self.class.process_alive?(pid) && self.class.port_listening?(port)

@coderabbitai coderabbitai Bot Nov 26, 2026 •

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 · 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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?

Thank you for using CodeRabbit!

You are interacting with an AI system.

end

#: () -> Hash[Symbol, untyped]
Expand Down Expand Up @@ -85,11 +86,7 @@ def all
if entry&.alive?
entry
else
begin
File.delete(path)
rescue StandardError
nil
end
delete_stale(path, entry)
nil
end
end
Expand Down Expand Up @@ -127,8 +124,27 @@ def process_alive?(pid)
false
end

#: (Integer?) -> bool
def port_listening?(port)
return false unless port

Socket.tcp("127.0.0.1", port, connect_timeout: 1).close
true
rescue SystemCallError, IOError
false
end

private

#: (String, ServerEntry?) -> void
def delete_stale(path, entry)
return if entry && load_file(path)&.to_hash != entry.to_hash

File.delete(path)
rescue StandardError
nil
end

#: (String) -> ServerEntry?
def load_file(path)
data = JSON.parse(File.read(path))
Expand Down
6 changes: 6 additions & 0 deletions sig/herb/dev/server_entry.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion test/dev/boot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,43 @@ def logged
test "boots nothing when a standalone server owns the project" do
Dir.mktmpdir do |root|
expanded = File.realpath(root)
entry = Herb::Dev::ServerEntry.new(pid: Process.pid, port: 65_001, project: expanded)
listener = TCPServer.new("127.0.0.1", 0)
entry = Herb::Dev::ServerEntry.new(pid: Process.pid, port: listener.addr[1], project: expanded)
entry.save

begin
assert_nil Herb::Dev.boot(root, environment: "development", logger: quiet)
assert(logged.any? { |message| message.include?("already running") })
ensure
entry.remove
listener.close
end
end
end

test "a stale entry whose PID now belongs to another process is cleared before booting on its port" do
Dir.mktmpdir do |root|
Dir.mktmpdir do |other|
other_project = File.realpath(other)
unrelated = Process.spawn(RbConfig.ruby, "-e", "sleep")
port = Herb::Dev::Server.find_available_port
entry = Herb::Dev::ServerEntry.new(pid: unrelated, port: port, project: other_project)
entry.save

begin
embedded = Herb::Dev.boot(root, environment: "development", logger: quiet)

skip "cruise unavailable" if embedded.nil? && logged.any? { |message| message.include?("cruise") }

assert_equal port, embedded.server.port
assert_nil Herb::Dev::ServerEntry.find_by_project(other_project)
refute File.exist?(File.join(Herb::Dev::ServerEntry::SERVERS_DIR, "#{unrelated}.json"))
ensure
Herb::Dev.shutdown
entry.remove
Process.kill("KILL", unrelated)
Process.wait(unrelated)
end
end
end
end
Expand Down
71 changes: 71 additions & 0 deletions test/dev/server_entry_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require "socket"
require "tmpdir"

require_relative "../test_helper"
require_relative "../../lib/herb/dev/server_entry"

module Dev
class ServerEntryTest < Minitest::Spec
def free_port
server = TCPServer.new("127.0.0.1", 0)
server.addr[1]
ensure
server&.close
end

test "an entry whose process is alive but whose port has no listener is stale" do
Dir.mktmpdir do |root|
entry = Herb::Dev::ServerEntry.new(pid: Process.pid, port: free_port, project: root)
entry.save

begin
assert_nil Herb::Dev::ServerEntry.find_by_project(root)
refute File.exist?(File.join(Herb::Dev::ServerEntry::SERVERS_DIR, "#{Process.pid}.json"))
ensure
entry.remove
end
end
end

test "an entry whose process is alive and listens on its port is found" do
Dir.mktmpdir do |root|
listener = TCPServer.new("127.0.0.1", 0)
entry = Herb::Dev::ServerEntry.new(pid: Process.pid, port: listener.addr[1], project: root)
entry.save

begin
assert_equal entry.port, Herb::Dev::ServerEntry.find_by_project(root)&.port
ensure
entry.remove
listener.close
end
end
end

test "a stale entry replaced while it is probed keeps the replacement" do
Dir.mktmpdir do |root|
stale_port = free_port
replacement = Herb::Dev::ServerEntry.new(pid: Process.pid, port: free_port, project: root)
registry = Class.new(Herb::Dev::ServerEntry) do
define_singleton_method(:port_listening?) do |port|
next super(port) unless port == stale_port

replacement.save
false
end
end
Herb::Dev::ServerEntry.new(pid: Process.pid, port: stale_port, project: root).save

begin
registry.all

assert_equal replacement.to_json, File.read(File.join(Herb::Dev::ServerEntry::SERVERS_DIR, "#{Process.pid}.json"))
ensure
replacement.remove
end
end
end
end
end
13 changes: 13 additions & 0 deletions test/dev/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def register(server, client)
server.instance_variable_get(:@clients) << client
end

test "a server that cannot register itself releases its port" do
port = Herb::Dev::Server.find_available_port(65_000)
blocked_entry = File.join(Herb::Dev::ServerEntry::SERVERS_DIR, "#{Process.pid}.json")
FileUtils.mkdir_p(blocked_entry)

begin
assert_raises(Errno::EISDIR) { Herb::Dev::Server.new(port: port).start }
assert Herb::Dev::Server.port_available?(port)
ensure
Dir.rmdir(blocked_entry)
end
end

test "the welcome payload reports what the server was told is broken" do
server = build_server
server.on_welcome { [{ file: "a.html.erb" }] }
Expand Down
Loading