Skip to content

Update dependency to github.com/cli/browser and survive failed browser open#943

Open
mikelolasagasti wants to merge 1 commit intoopenshift-online:mainfrom
mikelolasagasti:switch-to-cli-browser
Open

Update dependency to github.com/cli/browser and survive failed browser open#943
mikelolasagasti wants to merge 1 commit intoopenshift-online:mainfrom
mikelolasagasti:switch-to-cli-browser

Conversation

@mikelolasagasti
Copy link
Copy Markdown

@mikelolasagasti mikelolasagasti commented Apr 10, 2024

Module github.com/skratchdot/open-golang hasn't had any activity since 2020. Module github.com/cli/browser is more maintained, includes better support for Windows or WSL and is being used by GitHub's CLI or OpenTofu.

On OpenURL failure (headless, SSH, WSL, etc.), log the auth URL and keep waiting for the callback instead of returning early. That avoids leaving the local HTTP server running and still allows manual login.

@openshift-ci openshift-ci bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 18, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 48b95da3-4867-4860-9115-205b1bb4677d

📥 Commits

Reviewing files that changed from the base of the PR and between 98a85ec and 3c12c46.

⛔ Files ignored due to path filters (2)
  • examples/go.sum is excluded by !**/*.sum
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • authentication/auth.go
  • examples/go.mod
  • go.mod
✅ Files skipped from review due to trivial changes (2)
  • examples/go.mod
  • go.mod
🚧 Files skipped from review as they are similar to previous changes (1)
  • authentication/auth.go

Walkthrough

Swapped the browser-launch dependency from github.com/skratchdot/open-golang to github.com/cli/browser. authentication/auth.go now calls browser.OpenURL(url) and logs a failure while continuing; go.mod and examples/go.mod were updated accordingly.

Changes

Cohort / File(s) Summary
Authentication change
authentication/auth.go
Replaced open.Run(url) with browser.OpenURL(url) and changed failure handling to log the error and prompt the user to open the URL manually instead of returning early.
Module files
go.mod, examples/go.mod
Removed github.com/skratchdot/open-golang and added github.com/cli/browser v1.3.0 (in examples/go.mod as // indirect).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes: switching to github.com/cli/browser and improving error handling for failed browser opens.
Description check ✅ Passed The description clearly explains the rationale for the dependency switch (maintenance and better platform support) and the behavioral change (graceful handling of browser open failures).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci openshift-ci bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 18, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@authentication/auth.go`:
- Around line 106-109: The browser.OpenURL(url) error handling currently returns
early and skips shutting down the HTTP server; instead, catch the error from
browser.OpenURL in the auth flow (authentication/auth.go), log the failure and
the URL so the user can open it manually, and continue to the existing wait loop
rather than returning; ensure the existing shutdown(server) invocation
(shutdown(server)) still executes in all exit paths (success, manual completion,
or timeout) so the server is not leaked—modify the block around
browser.OpenURL(url) in the function that drives the auth flow to log and
continue rather than return on error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e928d580-c4ee-4a53-bcf8-762d82b115f8

📥 Commits

Reviewing files that changed from the base of the PR and between cbd0f3c and 0735bcc.

⛔ Files ignored due to path filters (2)
  • examples/go.sum is excluded by !**/*.sum
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • authentication/auth.go
  • examples/go.mod
  • go.mod

@mikelolasagasti mikelolasagasti changed the title Update dependency to github.com/cli/browser Update dependency to github.com/cli/browser and survive failed browser open Mar 18, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
authentication/auth.go (1)

22-23: ⚠️ Potential issue | 🔴 Critical

Synchronize authToken access and replace busy-loop with blocking wait.

Concurrent writes and reads of authToken without synchronization create a data race: the callback handler (running in HTTP server's goroutine) writes to authToken at line 45, while InitiateAuthCode reads it at lines 114 and 116 without locks. Additionally, the wait loop at lines 113–122 busy-spins with only CPU-bound condition checks, consuming resources until the timeout.

Use a mutex to guard authToken access and replace the tight polling loop with a blocking select statement using time.Timer and time.Ticker.

Proposed fix
 var (
 	conf      *oauth2.Config
 	ctx       context.Context
 	verifier  string
 	authToken string
+	authMu    sync.RWMutex
 )
@@
-	authToken = tok.RefreshToken
+	authMu.Lock()
+	authToken = tok.RefreshToken
+	authMu.Unlock()
@@
-	fiveMinTimer := time.Now().Local().Add(time.Minute * 5)
+	timeout := time.NewTimer(5 * time.Minute)
+	defer timeout.Stop()
+	ticker := time.NewTicker(100 * time.Millisecond)
+	defer ticker.Stop()
@@
-	for {
-		if authToken != "" {
-			shutdown(server)
-			return authToken, nil
-		}
-		if time.Now().After(fiveMinTimer) {
-			shutdown(server)
-			return authToken, fmt.Errorf("time expired")
-		}
-	}
+	for {
+		select {
+		case <-timeout.C:
+			shutdown(server)
+			return "", fmt.Errorf("time expired")
+		case <-ticker.C:
+			authMu.RLock()
+			token := authToken
+			authMu.RUnlock()
+			if token != "" {
+				shutdown(server)
+				return token, nil
+			}
+		}
+	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@authentication/auth.go` around lines 22 - 23, Protect the shared variable
authToken with a sync.Mutex (or sync.RWMutex): add a mutex (e.g., authMu) and
use authMu.Lock()/Unlock() around every write to authToken in the HTTP callback
handler and use authMu.RLock()/RUnlock() around every read in InitiateAuthCode
to eliminate the race. Replace the tight polling loop in InitiateAuthCode with a
blocking select: create a time.Timer for the overall timeout and a time.Ticker
(or a channel signaled by the callback) to wait for authToken to be set; on each
tick (or when the callback signals) acquire the read lock and check authToken,
and return when set or when the timer fires. Ensure the callback signals the
waiting goroutine (if using a channel) after writing authToken while holding the
mutex to avoid races.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@authentication/auth.go`:
- Around line 22-23: Protect the shared variable authToken with a sync.Mutex (or
sync.RWMutex): add a mutex (e.g., authMu) and use authMu.Lock()/Unlock() around
every write to authToken in the HTTP callback handler and use
authMu.RLock()/RUnlock() around every read in InitiateAuthCode to eliminate the
race. Replace the tight polling loop in InitiateAuthCode with a blocking select:
create a time.Timer for the overall timeout and a time.Ticker (or a channel
signaled by the callback) to wait for authToken to be set; on each tick (or when
the callback signals) acquire the read lock and check authToken, and return when
set or when the timer fires. Ensure the callback signals the waiting goroutine
(if using a channel) after writing authToken while holding the mutex to avoid
races.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a0b7849-4031-4036-9154-5867c5366951

📥 Commits

Reviewing files that changed from the base of the PR and between 0735bcc and 98a85ec.

⛔ Files ignored due to path filters (2)
  • examples/go.sum is excluded by !**/*.sum
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • authentication/auth.go
  • examples/go.mod
  • go.mod
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/go.mod

@openshift-ci openshift-ci bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 2, 2026
Replace github.com/skratchdot/open-golang with github.com/cli/browser.

On OpenURL failure (headless, SSH, WSL, etc.), log the auth URL and
keep waiting for the callback instead of returning early. That avoids
leaving the local HTTP server running and still allows manual login.

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
@mikelolasagasti mikelolasagasti force-pushed the switch-to-cli-browser branch from 98a85ec to 3c12c46 Compare April 2, 2026 13:57
@openshift-ci openshift-ci bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 2, 2026
@openshift-ci
Copy link
Copy Markdown

openshift-ci bot commented Apr 2, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mikelolasagasti
Once this PR has been reviewed and has the lgtm label, please assign robpblake for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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.

1 participant