Skip to content

Commit fe1ee77

Browse files
committed
fix: protect well-known session types from counter caps
Known-family types keep their own counter at the agent cap, the over-cap warn fires on the actual normalized count, and both caps share one constant. Adds coverage for known-first cap ordering and child-row created_at copies.
1 parent 6c120ce commit fe1ee77

8 files changed

Lines changed: 113 additions & 66 deletions

File tree

‎agent/agentssh/agentssh.go‎

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ type Server struct {
156156

157157
config *Config
158158

159-
// connCounts tracks active sessions per session type, created on demand.
160-
connCounts syncmap.Map[string, *atomic.Int64]
159+
// sessionCounts tracks active sessions per session type.
160+
sessionCounts *syncmap.Map[string, *atomic.Int64]
161161

162162
metrics *sshServerMetrics
163163
}
@@ -200,13 +200,14 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
200200

201201
metrics := newSSHServerMetrics(prometheusRegistry)
202202
s := &Server{
203-
Execer: execer,
204-
listeners: make(map[net.Listener]struct{}),
205-
fs: fs,
206-
conns: make(map[net.Conn]struct{}),
207-
sessions: make(map[ssh.Session]struct{}),
208-
processes: make(map[*os.Process]struct{}),
209-
logger: logger,
203+
Execer: execer,
204+
listeners: make(map[net.Listener]struct{}),
205+
fs: fs,
206+
conns: make(map[net.Conn]struct{}),
207+
sessions: make(map[ssh.Session]struct{}),
208+
processes: make(map[*os.Process]struct{}),
209+
sessionCounts: syncmap.New[string, *atomic.Int64](),
210+
logger: logger,
210211

211212
config: config,
212213

@@ -228,7 +229,7 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
228229
},
229230
}
230231

231-
jetbrainsCounter := s.getOrCreateConnCounter(MagicSessionTypeJetBrains)
232+
jetbrainsCounter := s.getOrCreateSessionCounter(MagicSessionTypeJetBrains)
232233
srv := &ssh.Server{
233234
ChannelHandlers: map[string]ssh.ChannelHandler{
234235
"direct-tcpip": func(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx ssh.Context) {
@@ -325,35 +326,38 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
325326
return s, nil
326327
}
327328

328-
// maxSessionTypes bounds distinct client-supplied session-type counters;
329-
// overflow shares the unknown counter, mirroring the server-side cap.
330-
const maxSessionTypes = 64
331-
332-
// getOrCreateConnCounter returns the active session counter for the given
333-
// session type, creating it on first use.
334-
func (s *Server) getOrCreateConnCounter(sessionType MagicSessionType) *atomic.Int64 {
329+
// getOrCreateSessionCounter returns the active session counter for the
330+
// given session type, creating it on first use. Known-family types always
331+
// get their own counter; unrecognized types past the cap share the unknown
332+
// counter, mirroring the server-side cap.
333+
func (s *Server) getOrCreateSessionCounter(sessionType MagicSessionType) *atomic.Int64 {
335334
key := string(sessionType)
336-
if counter, ok := s.connCounts.Load(key); ok {
335+
if counter, ok := s.sessionCounts.Load(key); ok {
337336
return counter
338337
}
339-
var size int
340-
s.connCounts.Range(func(string, *atomic.Int64) bool {
341-
size++
342-
return true
343-
})
344-
// Racing first-uses can slightly overshoot the cap.
345-
if size >= maxSessionTypes {
346-
key = idemetadata.AppNameUnknown
338+
if idemetadata.Family(key) == idemetadata.AppNameUnknown {
339+
var size int
340+
s.sessionCounts.Range(func(string, *atomic.Int64) bool {
341+
size++
342+
return true
343+
})
344+
// Racing first-uses can slightly overshoot the cap.
345+
if size >= idemetadata.MaxSessionCountEntries {
346+
s.logger.Debug(context.Background(), "session type counter cap reached, counting under unknown",
347+
slog.F("session_type", key),
348+
)
349+
key = idemetadata.AppNameUnknown
350+
}
347351
}
348-
counter, _ := s.connCounts.LoadOrStore(key, &atomic.Int64{})
352+
counter, _ := s.sessionCounts.LoadOrStore(key, &atomic.Int64{})
349353
return counter
350354
}
351355

352356
// SessionCounts returns a snapshot of active sessions per session type,
353357
// omitting idle types.
354358
func (s *Server) SessionCounts() map[string]int64 {
355359
stats := make(map[string]int64)
356-
s.connCounts.Range(func(key string, value *atomic.Int64) bool {
360+
s.sessionCounts.Range(func(key string, value *atomic.Int64) bool {
357361
if count := value.Load(); count > 0 {
358362
stats[key] = count
359363
}
@@ -483,7 +487,7 @@ func (s *Server) sessionHandler(session ssh.Session) {
483487
// channel.
484488
reportSession = false
485489
} else {
486-
counter := s.getOrCreateConnCounter(magicType)
490+
counter := s.getOrCreateSessionCounter(magicType)
487491
counter.Add(1)
488492
defer counter.Add(-1)
489493
}

‎agent/agentssh/sessiontype_internal_test.go‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/require"
8+
"go.uber.org/atomic"
89

910
"github.com/coder/coder/v2/coderd/idemetadata"
11+
"github.com/coder/coder/v2/coderd/util/syncmap"
1012
)
1113

1214
func TestExtractMagicSessionType(t *testing.T) {
@@ -43,16 +45,16 @@ func TestExtractMagicSessionType(t *testing.T) {
4345
}
4446
}
4547

46-
func TestConnCounts(t *testing.T) {
48+
func TestSessionCounts(t *testing.T) {
4749
t.Parallel()
4850

49-
s := &Server{}
51+
s := &Server{sessionCounts: syncmap.New[string, *atomic.Int64]()}
5052

5153
// Counters are created dynamically per session type, including types
5254
// unknown to this build of the agent.
53-
s.getOrCreateConnCounter(MagicSessionTypeSSH).Add(1)
54-
s.getOrCreateConnCounter(MagicSessionTypeVSCode).Add(1)
55-
s.getOrCreateConnCounter(MagicSessionType("Cursor")).Add(2)
55+
s.getOrCreateSessionCounter(MagicSessionTypeSSH).Add(1)
56+
s.getOrCreateSessionCounter(MagicSessionTypeVSCode).Add(1)
57+
s.getOrCreateSessionCounter(MagicSessionType("Cursor")).Add(2)
5658
require.Equal(t, map[string]int64{
5759
"ssh": 1,
5860
"vscode": 1,
@@ -61,30 +63,35 @@ func TestConnCounts(t *testing.T) {
6163

6264
// The same counter instance is reused per type, and idle types are
6365
// omitted from the snapshot.
64-
s.getOrCreateConnCounter(MagicSessionType("Cursor")).Add(-2)
66+
s.getOrCreateSessionCounter(MagicSessionType("Cursor")).Add(-2)
6567
require.Equal(t, map[string]int64{
6668
"ssh": 1,
6769
"vscode": 1,
6870
}, s.SessionCounts())
6971
}
7072

71-
func TestConnCountsCapped(t *testing.T) {
73+
func TestSessionCountsCapped(t *testing.T) {
7274
t.Parallel()
7375

74-
s := &Server{}
75-
for i := range maxSessionTypes {
76-
s.getOrCreateConnCounter(MagicSessionType(fmt.Sprintf("ide-%03d", i))).Add(1)
76+
s := &Server{sessionCounts: syncmap.New[string, *atomic.Int64]()}
77+
for i := range idemetadata.MaxSessionCountEntries {
78+
s.getOrCreateSessionCounter(MagicSessionType(fmt.Sprintf("ide-%03d", i))).Add(1)
7779
}
78-
// Once the cap is reached, new session types share the unknown counter.
79-
s.getOrCreateConnCounter(MagicSessionType("one-too-many")).Add(1)
80-
s.getOrCreateConnCounter(MagicSessionType("two-too-many")).Add(1)
80+
// Once the cap is reached, new unrecognized session types share the
81+
// unknown counter.
82+
s.getOrCreateSessionCounter(MagicSessionType("one-too-many")).Add(1)
83+
s.getOrCreateSessionCounter(MagicSessionType("two-too-many")).Add(1)
8184

8285
counts := s.SessionCounts()
83-
require.Len(t, counts, maxSessionTypes+1)
86+
require.Len(t, counts, idemetadata.MaxSessionCountEntries+1)
8487
require.EqualValues(t, 2, counts[idemetadata.AppNameUnknown])
8588
require.NotContains(t, counts, "one-too-many")
8689

90+
// Known-family types bypass the cap and keep their own counter.
91+
s.getOrCreateSessionCounter(MagicSessionTypeSSH).Add(1)
92+
require.EqualValues(t, 1, s.SessionCounts()["ssh"])
93+
8794
// Existing counters keep working at the cap.
88-
s.getOrCreateConnCounter(MagicSessionType("ide-000")).Add(1)
95+
s.getOrCreateSessionCounter(MagicSessionType("ide-000")).Add(1)
8996
require.EqualValues(t, 2, s.SessionCounts()["ide-000"])
9097
}

‎cli/ssh_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,8 +1668,8 @@ func TestSSH(t *testing.T) {
16681668
expectedCounts: map[string]int64{"vscode": 1},
16691669
},
16701670
{
1671-
// Arbitrary app names pass through to the server and are
1672-
// stored raw, so new IDEs are tracked without CLI changes.
1671+
// Arbitrary app names pass through raw (normalized at
1672+
// ingestion), so new IDEs need no CLI changes.
16731673
name: "ArbitraryNamePassthrough",
16741674
experiment: true,
16751675
usageAppName: "SomeFutureIDE",

‎coderd/idemetadata/idemetadata.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
// names are truncated before storage.
1414
const MaxAppNameLength = 64
1515

16+
// MaxSessionCountEntries bounds distinct app names per stats report;
17+
// overflow aggregates under AppNameUnknown. Applied by both agent and
18+
// server.
19+
const MaxSessionCountEntries = 64
20+
1621
// Canonical app names for Coder's built-in session types.
1722
const (
1823
AppNameVSCode = "vscode"

‎coderd/workspacestats/batcher.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/coder/coder/v2/coderd/database"
1818
"github.com/coder/coder/v2/coderd/database/dbauthz"
1919
"github.com/coder/coder/v2/coderd/database/dbtime"
20+
"github.com/coder/coder/v2/coderd/idemetadata"
2021
)
2122

2223
const (
@@ -142,14 +143,15 @@ func (b *DBBatcher) Add(
142143
usage bool,
143144
) {
144145
// Normalize outside the lock; Add serializes every stats report.
145-
sessionCounts := SessionCountsFromProto(st)
146-
if len(st.GetSessionCounts()) > maxSessionCountEntries {
146+
sessionCounts := normalizedSessionCounts(st)
147+
if len(sessionCounts) > idemetadata.MaxSessionCountEntries {
147148
b.log.Warn(context.Background(), "agent reported too many distinct session types, overflow aggregated under unknown",
148149
slog.F("agent_id", agentID),
149-
slog.F("reported", len(st.GetSessionCounts())),
150-
slog.F("max", maxSessionCountEntries),
150+
slog.F("reported", len(sessionCounts)),
151+
slog.F("max", idemetadata.MaxSessionCountEntries),
151152
)
152153
}
154+
sessionCounts = capSessionCounts(sessionCounts)
153155

154156
b.mu.Lock()
155157
defer b.mu.Unlock()

‎coderd/workspacestats/batcher_internal_test.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestBatchStats(t *testing.T) {
2727
ctx, cancel := context.WithCancel(context.Background())
2828
t.Cleanup(cancel)
2929
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
30-
store, ps := dbtestutil.NewDB(t)
30+
store, ps, sqlDB := dbtestutil.NewDBWithSQLDB(t)
3131

3232
// Set up some test dependencies.
3333
deps1 := setupDeps(t, store, ps)
@@ -94,6 +94,19 @@ func TestBatchStats(t *testing.T) {
9494
require.EqualValues(t, 2, byAgent[deps2.Agent.ID].SessionCountReconnectingPTY)
9595
require.EqualValues(t, 0, byAgent[deps2.Agent.ID].SessionCountVSCode)
9696

97+
// And: child rows copy the parent's created_at exactly; windowed
98+
// reads join on it, so a divergent copy silently drops rows.
99+
var children, mismatched int
100+
require.NoError(t, sqlDB.QueryRowContext(ctx, `
101+
SELECT
102+
COUNT(*),
103+
COUNT(*) FILTER (WHERE sc.created_at <> s.created_at)
104+
FROM workspace_agent_session_counts sc
105+
JOIN workspace_agent_stats s ON s.id = sc.workspace_agent_stats_id
106+
`).Scan(&children, &mismatched))
107+
require.EqualValues(t, 4, children, "expected one child row per positive session count")
108+
require.Zero(t, mismatched, "child rows must copy the parent created_at")
109+
97110
// Given: a lot of data points are added for both workspaces
98111
// (equal to batch size)
99112
t3 := t2.Add(time.Second)

‎coderd/workspacestats/sessioncounts.go‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import (
99
"github.com/coder/coder/v2/coderd/idemetadata"
1010
)
1111

12-
// maxSessionCountEntries bounds distinct app names per stats report so a bad
13-
// agent cannot fan out child-table rows; overflow aggregates under
14-
// AppNameUnknown.
15-
const maxSessionCountEntries = 64
16-
1712
// SessionCountsFromProto returns an agent's per-app session counts,
18-
// normalized, with non-positive counts dropped. The deprecated fixed fields
19-
// are converted for agents predating the session_counts map (API < 2.11).
13+
// normalized and capped, with non-positive counts dropped.
2014
func SessionCountsFromProto(st *agentproto.Stats) map[string]int64 {
15+
return capSessionCounts(normalizedSessionCounts(st))
16+
}
17+
18+
// normalizedSessionCounts is SessionCountsFromProto without the entry cap,
19+
// so callers can observe overflow. Deprecated fixed fields are converted
20+
// for agents predating the session_counts map (API < 2.11).
21+
func normalizedSessionCounts(st *agentproto.Stats) map[string]int64 {
2122
counts := make(map[string]int64, len(st.GetSessionCounts()))
2223
for app, count := range st.GetSessionCounts() {
2324
if count > 0 {
@@ -40,14 +41,14 @@ func SessionCountsFromProto(st *agentproto.Stats) map[string]int64 {
4041
}
4142
}
4243
}
43-
return capSessionCounts(counts)
44+
return counts
4445
}
4546

46-
// capSessionCounts keeps at most maxSessionCountEntries named entries,
47-
// well-known names first then lexicographic for determinism; the rest sums
48-
// into AppNameUnknown, which can add one entry past the cap.
47+
// capSessionCounts keeps at most idemetadata.MaxSessionCountEntries named
48+
// entries, well-known names first then lexicographic for determinism; the
49+
// rest sums into AppNameUnknown, which can add one entry past the cap.
4950
func capSessionCounts(counts map[string]int64) map[string]int64 {
50-
if len(counts) <= maxSessionCountEntries {
51+
if len(counts) <= idemetadata.MaxSessionCountEntries {
5152
return counts
5253
}
5354
names := slices.Collect(maps.Keys(counts))
@@ -64,12 +65,12 @@ func capSessionCounts(counts map[string]int64) map[string]int64 {
6465
}
6566
return strings.Compare(a, b)
6667
})
67-
capped := make(map[string]int64, maxSessionCountEntries+1)
68-
for _, name := range names[:maxSessionCountEntries] {
68+
capped := make(map[string]int64, idemetadata.MaxSessionCountEntries+1)
69+
for _, name := range names[:idemetadata.MaxSessionCountEntries] {
6970
capped[name] = counts[name]
7071
}
7172
var overflow int64
72-
for _, name := range names[maxSessionCountEntries:] {
73+
for _, name := range names[idemetadata.MaxSessionCountEntries:] {
7374
overflow += counts[name]
7475
}
7576
if overflow > 0 {

‎coderd/workspacestats/sessioncounts_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ func TestSessionCountsFromProto(t *testing.T) {
8282
require.NotContains(t, got, "zz_ide_199")
8383
})
8484

85+
t.Run("CapKeepsWellKnownNames", func(t *testing.T) {
86+
t.Parallel()
87+
// Junk names sort before "ssh", so only the known-first
88+
// ordering keeps the canonical entry under the cap.
89+
sessionCounts := map[string]int64{"ssh": 5}
90+
for i := range 100 {
91+
sessionCounts[fmt.Sprintf("0ide-%03d", i)] = 1
92+
}
93+
got := workspacestats.SessionCountsFromProto(&agentproto.Stats{SessionCounts: sessionCounts})
94+
require.Len(t, got, 65)
95+
require.EqualValues(t, 5, got["ssh"])
96+
require.EqualValues(t, 37, got["unknown"])
97+
require.NotContains(t, got, "0ide_099")
98+
})
99+
85100
t.Run("Empty", func(t *testing.T) {
86101
t.Parallel()
87102
require.Empty(t, workspacestats.SessionCountsFromProto(&agentproto.Stats{}))

0 commit comments

Comments
 (0)