Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
8ec1742
fix: replace deprecated tailscale n.NetMap usage
skevetter Aug 26, 2026
5955c29
style: update comment
skevetter Aug 26, 2026
a4c19ce
fix: subscribe to NotifyPeerChanges in WatchNetmap
skevetter Aug 28, 2026
b892b10
fix: decouple netmap status fetch from IPN bus drain
skevetter Aug 28, 2026
3969f4d
fix: eliminate test race in burst-drain regression test
skevetter Aug 28, 2026
c3d2040
style: cleanup comments
skevetter Aug 28, 2026
56bde7f
refactor: consolidate workspace_server.go around a runnerClient
skevetter Aug 28, 2026
d4e3a33
fix: resource leaks, TLS-skip inconsistency, and lint debt in pkg/ts
skevetter Aug 28, 2026
9ba8a18
fix: close port-conversion gap missed by independent review
skevetter Aug 28, 2026
01c3af0
style: lowercase log messages; document runner-proxy socket permission
skevetter Aug 28, 2026
fae81a4
refactor: replace hand-rolled retry loop with wait.PollUntilContextTi…
skevetter Aug 28, 2026
b46b766
refactor: collapse CheckWorkspaceReachable to a single poll
skevetter Aug 28, 2026
9041007
style: comment cleanup -- add missing rationale, remove restating com…
skevetter Aug 28, 2026
f21165d
fix: refresh workspace state after desktop-app launch before final error
skevetter Aug 28, 2026
b954b8a
refactor: split workspaceReachabilityCheck by responsibility
skevetter Aug 28, 2026
46d6d0d
style: restore missing rationale on the burst-drain regression test
skevetter Aug 28, 2026
fd445eb
fix: don't abandon reachability poll on a transient GetWorkspace error
skevetter Aug 28, 2026
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
2 changes: 1 addition & 1 deletion cmd/internal/agentcontainer/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ func runNetworkServer(
AccessKey: cmd.Config.Platform.AccessKey,
PlatformHost: ts.RemoveProtocol(cmd.Config.Platform.PlatformHost),
WorkspaceHost: cmd.Config.Platform.WorkspaceHost,
Client: baseClient,
RootDir: RootDir,
Insecure: true,
LogF: func(format string, args ...any) {
log.Infof(format, args...)
},
Expand Down
176 changes: 152 additions & 24 deletions pkg/client/clientimplementation/daemonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,122 @@ import (
sshServer "github.com/devsy-org/devsy/pkg/ssh/server"
"github.com/devsy-org/devsy/pkg/ts"
"golang.org/x/crypto/ssh"
"k8s.io/apimachinery/pkg/util/wait"
"tailscale.com/client/local"
"tailscale.com/tailcfg"
)

// checkWorkspaceReachableTimeout is CheckWorkspaceReachable's overall retry
// budget, covering both the initial reachability check and, if the daemon
// needed restarting, however long the desktop app takes to bring the
// workspace back up.
const checkWorkspaceReachableTimeout = 150 * time.Second

// daemonHealthCheckDelay is how long CheckWorkspaceReachable retries a plain
// dial before it first checks whether the daemon itself is down and, if so,
// launches the desktop app to restart it. Checking immediately would fire on
// every transient dial failure; the delay gives a live daemon a chance to
// become reachable on its own first. Also the minimum interval between
// re-checks after an inconclusive one (see attempt), so a recurring
// transient GetWorkspace error can't turn into a hot loop.
const daemonHealthCheckDelay = 30 * time.Second

// errGiveUpReachingWorkspace stops CheckWorkspaceReachable's poll early once
// retrying can no longer help: either the daemon is confirmed to be running
// and the host is unreachable for some other reason, or launching the
// desktop app itself failed.
var errGiveUpReachingWorkspace = errors.New("give up reaching workspace")

// workspaceDialer checks tailnet reachability only -- no daemon-health or
// desktop-app concerns.
type workspaceDialer struct {
tsClient *local.Client
wAddr ts.Addr
port uint16
}

func (d workspaceDialer) reachable(ctx context.Context) error {
dialCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
conn, err := d.tsClient.DialTCP(dialCtx, d.wAddr.Host(), d.port)
if err != nil {
return err
}
_ = conn.Close()
return nil
}

// daemonRecovery decides, once daemonHealthCheckDelay has passed without a
// reachable host, whether the daemon itself is down and, if so, launches the
// desktop app to restart it -- no network-reachability concerns. attempt is
// idempotent after opened is set: later calls are no-ops until the daemon's
// state needs re-checking (which the caller does once polling ends).
type daemonRecovery struct {
client *client
start time.Time

opened bool
lastCheck time.Time
getWorkspaceErr error
instance *managementv1.DevsyWorkspaceInstance
}

// attempt returns errGiveUpReachingWorkspace once retrying can no longer
// help: GetWorkspace succeeded (so the daemon is up and reported the actual,
// unfixable-by-retrying workspace state), or launching the desktop app
// failed. A GetWorkspace error other than DaemonNotAvailableError (e.g. a
// transient backend error) is inconclusive, not terminal: re-check after
// another daemonHealthCheckDelay instead of giving up.
func (r *daemonRecovery) attempt(ctx context.Context) error {
if r.opened {
return nil
}
now := time.Now()
nextCheck := r.start.Add(daemonHealthCheckDelay)
if !r.lastCheck.IsZero() {
nextCheck = r.lastCheck.Add(daemonHealthCheckDelay)
}
if now.Before(nextCheck) {
return nil
}
r.lastCheck = now

r.instance, r.getWorkspaceErr = r.client.localClient.GetWorkspace(ctx, r.client.workspace.UID)
if r.getWorkspaceErr == nil {
return errGiveUpReachingWorkspace
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
if !daemon.IsDaemonNotAvailableError(r.getWorkspaceErr) {
return nil
}

r.opened = true
if openErr := r.client.openDesktopApp(); openErr != nil {
return errGiveUpReachingWorkspace // inform user about daemon state
}
return nil
}

// workspaceReachabilityCheck is CheckWorkspaceReachable's poll step: dial,
// and on failure hand off to daemonRecovery.
type workspaceReachabilityCheck struct {
dial workspaceDialer
recovery *daemonRecovery
reachErr error
}

func (r *workspaceReachabilityCheck) step(ctx context.Context) (bool, error) {
dialErr := r.dial.reachable(ctx)
if dialErr == nil {
return true, nil
}
r.reachErr = dialErr

if err := r.recovery.attempt(ctx); err != nil {
return false, err
}
return false, nil
}

func New(
devsyConfig *config.Config,
prov *provider.ProviderConfig,
Expand Down Expand Up @@ -132,31 +244,35 @@ func (c *client) CheckWorkspaceReachable(ctx context.Context) error {
if err != nil {
return fmt.Errorf("resolve workspace hostname: %w", err)
}
err = ts.WaitHostReachable(ctx, c.tsClient, wAddr, 5)
if err == nil {
log.Debugf("Host %s is reachable. Proceeding with SSH session", wAddr.Host())
return nil
port, err := wAddr.PortUint16()
if err != nil {
return fmt.Errorf("resolve workspace hostname: %w", err)
}

instance, getWorkspaceErr := c.localClient.GetWorkspace(ctx, c.workspace.UID)
// if we can't reach the daemon try to start the desktop app
if daemon.IsDaemonNotAvailableError(getWorkspaceErr) {
openErr := c.openDesktopApp()
if openErr != nil {
return getWorkspaceErr // inform user about daemon state
}
// give desktop app a chance to start
time.Sleep(2 * time.Second)
check := &workspaceReachabilityCheck{
dial: workspaceDialer{tsClient: c.tsClient, wAddr: wAddr, port: port},
recovery: &daemonRecovery{client: c, start: time.Now()},
}
pollErr := wait.PollUntilContextTimeout(
ctx,
200*time.Millisecond,
checkWorkspaceReachableTimeout,
true,
check.step,
)
if pollErr == nil {
log.Debugf("host %s is reachable, proceeding with SSH session", wAddr.Host())
return nil
}

// let's try again
err = ts.WaitHostReachable(ctx, c.tsClient, wAddr, 20)
if err == nil {
return nil
}
instance, getWorkspaceErr := check.recovery.instance, check.recovery.getWorkspaceErr
// check.recovery's cached state predates the desktop-app launch; re-query
// once the daemon has had a chance to come back so the reported cause is
// current.
if check.recovery.opened || (instance == nil && getWorkspaceErr == nil) {
instance, getWorkspaceErr = c.localClient.GetWorkspace(ctx, c.workspace.UID)
}

return c.workspaceUnreachableError(instance, getWorkspaceErr, err)
return c.workspaceUnreachableError(instance, getWorkspaceErr, check.reachErr)
}

func (c *client) SSHClients(
Expand All @@ -179,15 +295,23 @@ func (c *client) SSHClients(
if err != nil {
return nil, fmt.Errorf("invalid port: %s", addressParts[1])
}
port16, err := ts.ToUint16Port(port)
if err != nil {
return nil, fmt.Errorf("invalid port: %w", err)
}

return c.tsClient.DialTCP(ctx, addressParts[0], uint16(port))
return c.tsClient.DialTCP(ctx, addressParts[0], port16)
}

toolClient, err = ts.WaitForSSHClient(ctx, dial, "tcp", address, "root", time.Second*10)
toolClient, err = ts.WaitForSSHClient(ctx, ts.SSHDialConfig{
Dialer: dial, Network: "tcp", Address: address, User: "root", Timeout: time.Second * 10,
})
if err != nil {
return nil, nil, fmt.Errorf("create SSH tool client: %w", err)
}
userClient, err = ts.WaitForSSHClient(ctx, dial, "tcp", address, user, time.Second*10)
userClient, err = ts.WaitForSSHClient(ctx, ts.SSHDialConfig{
Dialer: dial, Network: "tcp", Address: address, User: user, Timeout: time.Second * 10,
})
if err != nil {
return nil, nil, fmt.Errorf("create SSH user client: %w", err)
}
Expand All @@ -200,7 +324,11 @@ func (c *client) DirectTunnel(ctx context.Context, stdin io.Reader, stdout io.Wr
if err != nil {
return fmt.Errorf("resolve workspace hostname: %w", err)
}
conn, err := c.tsClient.DialTCP(ctx, wAddr.Host(), uint16(wAddr.Port()))
port, err := wAddr.PortUint16()
if err != nil {
return fmt.Errorf("invalid workspace port: %w", err)
}
conn, err := c.tsClient.DialTCP(ctx, wAddr.Host(), port)
if err != nil {
return fmt.Errorf("failed to connect to SSH server in proxy mode: %w", err)
}
Expand Down
14 changes: 3 additions & 11 deletions pkg/daemon/platform/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ package daemon
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"path/filepath"
"time"

"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/log"
"github.com/devsy-org/devsy/pkg/platform/client"
"github.com/devsy-org/devsy/pkg/ts"
"tailscale.com/client/local"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tsnet"
"tailscale.com/types/netmap"
)

type Daemon struct {
Expand Down Expand Up @@ -166,13 +163,8 @@ func (d *Daemon) watchNetmap(ctx context.Context) error {
return err
}

return ts.WatchNetmap(ctx, lc, func(netMap *netmap.NetworkMap) {
nm, err := json.Marshal(netMap)
if err != nil {
log.Errorf("Failed to marshal netmap: %v", err)
} else {
_ = os.WriteFile(filepath.Join(d.rootDir, "netmap.json"), nm, 0o644)
}
return ts.WatchNetmap(ctx, lc, func(status *ipnstate.Status) {
ts.PersistNetmapStatus(d.rootDir, status)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/platform/ts_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newTSServer(
Scheme: ts.GetEnvOrDefault("DEVSY_TSNET_SCHEME", "https"),
Host: ts.RemoveProtocol(host),
}
if err := ts.CheckDerpConnection(ctx, &baseUrl); err != nil {
if err := ts.CheckDerpConnection(ctx, &baseUrl, insecure); err != nil {
return nil, nil, fmt.Errorf("failed to verify DERP connection: %w", err)
}
if insecure {
Expand Down
6 changes: 6 additions & 0 deletions pkg/ts/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ func (a Addr) Host() string {
func (a Addr) Port() int {
return a.port
}

// PortUint16 returns Port bounds-checked for use with APIs that take a
// uint16 port, such as *local.Client.DialTCP.
func (a Addr) PortUint16() (uint16, error) {
return ToUint16Port(a.port)
}
26 changes: 15 additions & 11 deletions pkg/ts/derp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
"time"
)

type ConnTrackingFunc func(address string)

// CheckDerpConnection validates the DERP connection.
func CheckDerpConnection(ctx context.Context, baseUrl *url.URL) error {
newTransport := http.DefaultTransport.(*http.Transport).Clone()
newTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
// CheckDerpConnection validates the DERP connection. insecure skips TLS
// certificate verification, for coordinators known to use self-signed certs.
func CheckDerpConnection(ctx context.Context, baseUrl *url.URL, insecure bool) error {
transport := http.DefaultTransport.(*http.Transport).Clone()
if insecure {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
} // #nosec G402 -- opt-in via explicit insecure flag
}

client := &http.Client{
Transport: newTransport,
Transport: transport,
Timeout: 5 * time.Second,
}

Expand All @@ -34,22 +35,25 @@ func CheckDerpConnection(ctx context.Context, baseUrl *url.URL) error {
}

res, err := client.Do(req)
if err != nil || (res != nil && res.StatusCode != http.StatusOK) {
if err != nil {
return fmt.Errorf("failed to reach the coordinator server: %w", err)
}
defer func() { _ = res.Body.Close() }()

if res.StatusCode != http.StatusOK {
return fmt.Errorf("coordinator server returned status %d", res.StatusCode)
}

return nil
}

// Utility function to get environment variable or default.
func GetEnvOrDefault(envVar, defaultVal string) string {
if val := os.Getenv(envVar); val != "" {
return val
}
return defaultVal
}

// RemoveProtocol removes protocol from URL.
func RemoveProtocol(hostPath string) string {
if _, after, ok := strings.Cut(hostPath, "://"); ok {
return after
Expand Down
Loading
Loading