Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions packages/core/src/shared/utilities/collectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,26 @@ export function createCollectionFromPages<T>(...pages: T[]): AsyncCollection<T>
export function isPresent<T>(value: T | undefined): value is T {
return value !== undefined
}

export class CircularBuffer {
private buffer = new Map<number, boolean>()
private maxSize: number

constructor(size: number) {
this.maxSize = size
}

add(value: number): void {
if (this.buffer.size >= this.maxSize) {
// Map iterates its keys in insertion-order.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
const firstKey = this.buffer.keys().next().value
this.buffer.delete(firstKey)
}
this.buffer.set(value, true)
}

contains(value: number): boolean {
return this.buffer.has(value)
}
}
17 changes: 13 additions & 4 deletions packages/core/src/shared/utilities/processUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as crossSpawn from 'cross-spawn'
import * as logger from '../logger/logger'
import { Timeout, CancellationError, waitUntil } from './timeoutUtils'
import { PollingSet } from './pollingSet'
import { CircularBuffer } from './collectionUtils'

export interface RunParameterContext {
/** Reports an error parsed from the stdin/stdout streams. */
Expand Down Expand Up @@ -73,6 +74,7 @@ export class ChildProcessTracker {
cpu: 50,
}
static readonly logger = logger.getLogger('childProcess')
static readonly #loggedPids = new CircularBuffer(1000)
Copy link
Contributor

@Hweinstock Hweinstock Apr 11, 2025

Choose a reason for hiding this comment

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

Are we concerned about the case where a child process has a problem (ex. memory leak) that causes it to increase its resource usage as time passes? This change would make it hard to catch that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a valid concern, but:

  • by this point the "threshold" was already exceeded, which means we presumably already reached an alarm-state
  • to give visibility into "growing"/leak behavior, we should enhance the logic rather than hoping that someone will puzzle it out through these logs

#processByPid: Map<number, ChildProcess> = new Map<number, ChildProcess>()
#pids: PollingSet<number>

Expand Down Expand Up @@ -100,21 +102,28 @@ export class ChildProcessTracker {

private async checkProcessUsage(pid: number): Promise<void> {
if (!this.#pids.has(pid)) {
ChildProcessTracker.logger.warn(`Missing process with id ${pid}`)
ChildProcessTracker.logOnce(pid, `Missing process with id ${pid}`)
return
}
const stats = this.getUsage(pid)
if (stats) {
ChildProcessTracker.logger.debug(`Process ${pid} usage: %O`, stats)
if (stats.memory > ChildProcessTracker.thresholds.memory) {
ChildProcessTracker.logger.warn(`Process ${pid} exceeded memory threshold: ${stats.memory}`)
ChildProcessTracker.logOnce(pid, `Process ${pid} exceeded memory threshold: ${stats.memory}`)
}
if (stats.cpu > ChildProcessTracker.thresholds.cpu) {
ChildProcessTracker.logger.warn(`Process ${pid} exceeded cpu threshold: ${stats.cpu}`)
ChildProcessTracker.logOnce(pid, `Process ${pid} exceeded cpu threshold: ${stats.cpu}`)
}
}
}

public static logOnce(pid: number, msg: string) {
if (!ChildProcessTracker.#loggedPids.contains(pid)) {
ChildProcessTracker.#loggedPids.add(pid)
ChildProcessTracker.logger.warn(msg)
}
}

public add(childProcess: ChildProcess) {
const pid = childProcess.pid()
this.#processByPid.set(pid, childProcess)
Expand Down Expand Up @@ -147,7 +156,7 @@ export class ChildProcessTracker {
// isWin() leads to circular dependency.
return process.platform === 'win32' ? getWindowsUsage() : getUnixUsage()
} catch (e) {
ChildProcessTracker.logger.warn(`Failed to get process stats for ${pid}: ${e}`)
ChildProcessTracker.logOnce(pid, `Failed to get process stats for ${pid}: ${e}`)
return { cpu: 0, memory: 0 }
}

Expand Down
Loading