Repository navigation
Dev Server: Treat a registry entry as stale when nothing listens on its port #2936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
viktorianer
wants to merge
2
commits into
marcoroth:main
Choose a base branch
from
viktorianer:dev-server-stale-entry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| 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 |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pruning deletes registry files without checking their current contents.
ServerEntry.allprobes the entry it loaded earlier, then deletesP.jsonby 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 fromfind_by_port,find_by_project, andstop_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
saveandall.There was a problem hiding this comment.
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.allnow re-reads the file and deletes it only when it still holds the entry it probed, and a new test inserver_entry_test.rbwrites 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.There was a problem hiding this comment.
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.