Skip to content

fix: ensure agent websocket only removes its own conn #5828

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 4 commits into from
Jan 23, 2023
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
42 changes: 31 additions & 11 deletions tailnet/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewCoordinator() Coordinator {
return &coordinator{
closed: false,
nodes: map[uuid.UUID]*Node{},
agentSockets: map[uuid.UUID]net.Conn{},
agentSockets: map[uuid.UUID]idConn{},
agentToConnectionSockets: map[uuid.UUID]map[uuid.UUID]net.Conn{},
}
}
Expand All @@ -123,12 +123,19 @@ type coordinator struct {
// nodes maps agent and connection IDs their respective node.
nodes map[uuid.UUID]*Node
// agentSockets maps agent IDs to their open websocket.
agentSockets map[uuid.UUID]net.Conn
agentSockets map[uuid.UUID]idConn
// agentToConnectionSockets maps agent IDs to connection IDs of conns that
// are subscribed to updates for that agent.
agentToConnectionSockets map[uuid.UUID]map[uuid.UUID]net.Conn
}

type idConn struct {
// id is an ephemeral UUID used to uniquely identify the owner of the
// connection.
id uuid.UUID
conn net.Conn
}

// Node returns an in-memory node by ID.
// If the node does not exist, nil is returned.
func (c *coordinator) Node(id uuid.UUID) *Node {
Expand Down Expand Up @@ -224,7 +231,7 @@ func (c *coordinator) handleNextClientMessage(id, agent uuid.UUID, decoder *json
return xerrors.Errorf("marshal nodes: %w", err)
}

_, err = agentSocket.Write(data)
_, err = agentSocket.conn.Write(data)
if err != nil {
if errors.Is(err, io.EOF) {
return nil
Expand Down Expand Up @@ -268,20 +275,33 @@ func (c *coordinator) ServeAgent(conn net.Conn, id uuid.UUID) error {
c.mutex.Lock()
}

// If an old agent socket is connected, we close it
// to avoid any leaks. This shouldn't ever occur because
// we expect one agent to be running.
// This uniquely identifies a connection that belongs to this goroutine.
unique := uuid.New()

// If an old agent socket is connected, we close it to avoid any leaks. This
// shouldn't ever occur because we expect one agent to be running, but it's
// possible for a race condition to happen when an agent is disconnected and
// attempts to reconnect before the server realizes the old connection is
// dead.
oldAgentSocket, ok := c.agentSockets[id]
if ok {
_ = oldAgentSocket.Close()
_ = oldAgentSocket.conn.Close()
}
c.agentSockets[id] = idConn{
id: unique,
conn: conn,
}
c.agentSockets[id] = conn
c.mutex.Unlock()
defer func() {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.agentSockets, id)
delete(c.nodes, id)

// Only delete the connection if it's ours. It could have been
// overwritten.
if idConn := c.agentSockets[id]; idConn.id == unique {
delete(c.agentSockets, id)
delete(c.nodes, id)
}
}()

decoder := json.NewDecoder(conn)
Expand Down Expand Up @@ -349,7 +369,7 @@ func (c *coordinator) Close() error {
for _, socket := range c.agentSockets {
socket := socket
go func() {
_ = socket.Close()
_ = socket.conn.Close()
wg.Done()
}()
}
Expand Down