Skip to content

Conversation

cfergeau
Copy link
Contributor

This is quite wasteful, but after hostsfile.Clean() has been called,
the parsed hostfile is no longer valid, and adding more data to it
causes some of its content to disappear, see
#23 (comment)

This issue can only be seen when the admin-helper daemon is used, and
when there's a limit to the number of entries per line, which means this
can only be reproduced on Windows.

By reparsing the hostfile each time we get a request, we workaround this
bug.

@praveenkumar
Copy link
Member

@cfergeau unit test fails which need update

# github.com/code-ready/admin-helper/pkg/api [github.com/code-ready/admin-helper/pkg/api.test]
pkg\api\mux_test.go:14:30: too many arguments in call to Mux
	have (nil)
	want ()
note: module requires Go 1.15
FAIL	github.com/code-ready/admin-helper/pkg/api [build failed]

@gbraad
Copy link
Contributor

gbraad commented Nov 25, 2021

I'll take a look at this.

The process on Windows is long-running and likely gets out of sync.

@cfergeau
Copy link
Contributor Author

An alternative fix (did not test it) is:

diff --git a/pkg/hosts/hosts.go b/pkg/hosts/hosts.go
index 16a2b82..b956dc2 100644
--- a/pkg/hosts/hosts.go
+++ b/pkg/hosts/hosts.go
@@ -71,6 +71,12 @@ func (h *Hosts) Add(ip string, hosts []string) error {
        // 9 domain entry in a single line
        if runtime.GOOS == "windows" {
                h.File.Clean()
+               h.File.Flush()
+               file, err := hostsfile.NewHosts()
+               if err != nil {
+                       return err
+               }
+               h.File = &file
        }
        return h.File.Flush()
 }

This is quite wasteful, but after hostsfile.Clean() has been called,
the parsed hostfile is no longer valid, and adding more data to it
causes some of its content to disappear, see
crc-org#23 (comment)

This issue can only be seen when the admin-helper daemon is used, and
when there's a limit to the number of entries per line, which means this
can only be reproduced on Windows.

By reparsing the hostfile each time we get a request, we workaround this
bug.
return
}
if err := http.Serve(ln, api.Mux(hosts)); err != nil {
if err := http.Serve(ln, api.Mux()); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@praveenkumar praveenkumar merged commit e2d4733 into crc-org:master Nov 25, 2021
@cfergeau cfergeau deleted the manyhosts branch December 21, 2023 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants