Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Improve agent connection tracking when agent is closed #5253

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

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Revert "Avoid double assign of a.network"
This reverts commit 6f9b319.
  • Loading branch information
mafredri committed Dec 2, 2022
commit a0de5a05527a886bfe5cc9dd110d3203a49d6798
19 changes: 9 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ func (a *agent) run(ctx context.Context) error {
a.closeMutex.Unlock()
if network == nil {
a.logger.Debug(ctx, "creating tailnet")
err = a.createTailnet(ctx, metadata.DERPMap)
network, err = a.createTailnet(ctx, metadata.DERPMap)
if err != nil {
return xerrors.Errorf("create tailnet: %w", err)
}
a.closeMutex.Lock()
a.network = network
a.closeMutex.Unlock()
} else {
// Update the DERP map!
network.SetDERPMap(metadata.DERPMap)
Expand Down Expand Up @@ -242,10 +245,9 @@ func (a *agent) trackConnGoroutine(fn func()) error {
return nil
}

func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (err error) {
func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (network *tailnet.Conn, err error) {
a.closeMutex.Lock()
if a.isClosed() {
a.network = nil
a.closeMutex.Unlock()
return nil, xerrors.New("closed")
}
Expand All @@ -262,9 +264,6 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (er
defer func() {
if err != nil {
network.Close()
a.closeMutex.Lock()
a.network = nil
a.closeMutex.Unlock()
}
}()
a.network = network
Expand All @@ -288,7 +287,7 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (er
go a.sshServer.HandleConn(conn)
}
}); err != nil {
return err
return nil, err
}

reconnectingPTYListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.TailnetReconnectingPTYPort))
Expand Down Expand Up @@ -328,7 +327,7 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (er
go a.handleReconnectingPTY(ctx, msg, conn)
}
}); err != nil {
return err
return nil, err
}

speedtestListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.TailnetSpeedtestPort))
Expand Down Expand Up @@ -356,7 +355,7 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (er
}
}
}); err != nil {
return err
return nil, err
}

statisticsListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.TailnetStatisticsPort))
Expand Down Expand Up @@ -387,7 +386,7 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (er
a.logger.Critical(ctx, "serve statistics HTTP server", slog.Error(err))
}
}); err != nil {
return err
return nil, err
}

return network, nil
Expand Down