Skip to content

The agent that grows with you

style(desktop): satisfy lint on the singleton-lock additions

Brooklyn Nicholson3cee485bc2e7
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index c63831c9527..baf22f286b0 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -204,7 +204,6 @@ import type { RosterProfileMetadata } from './connection-registry'
import { liveWindowState, overlayWindowState } from './connection-window-state'
import { describeCrashReason, installCrashForensics } from './crash-forensics'
import { adoptServedDashboardToken, resolveServedDashboardToken } from './dashboard-token'
-import { removeStaleSingletonLock } from './singleton-lock'
import { resolveDesktopHermesHome, resolveDesktopUserData } from './data-paths'
import { loadOrCreateInstallationId, sshOwnershipId } from './desktop-installation'
import { formatDesktopLogLine, formatLogStamp } from './desktop-log-line'
@@ -512,6 +511,7 @@ import {
SESSION_WINDOW_MIN_WIDTH
} from './session-windows'
import { ensureLoginShellPath } from './shell-path'
+import { removeStaleSingletonLock } from './singleton-lock'
import { createSourcePythonBackend, resolveSourceInstallationBackend, type SourceBackend } from './source-backend'
import { resolveSourcePython } from './source-python'
import { createBootstrapCoordinator, sshConfigFingerprint } from './ssh-bootstrap-coordinator'
@@ -951,6 +951,7 @@ if (INSTALL_STAMP) {
}
const DESKTOP_PROFILE_CONFIG_PATH: string = path.join(app.getPath('userData'), 'active-profile.json')
+
// Only the lock-owning destination may adopt a workspace or start a backend.
// #78101: on Linux/X11 a zombie/defunct Electron process leaves the
// SingletonLock symlink behind with a PID that still answers kill(pid, 0),
@@ -958,11 +959,15 @@ const DESKTOP_PROFILE_CONFIG_PATH: string = path.join(app.getPath('userData'), '
// app silently exits. Clear a provably-dead owner and retry once; always log
// when the lock is legitimately lost so the exit is diagnosable.
function acquireSingleInstanceLock(): boolean {
- if (app.requestSingleInstanceLock()) return true
+ if (app.requestSingleInstanceLock()) {
+ return true
+ }
const stalePid = removeStaleSingletonLock(app.getPath('userData'))
+
if (stalePid !== null) {
console.error(`[hermes] removed stale SingletonLock (owner ${stalePid} dead); retrying launch`)
+
return app.requestSingleInstanceLock()
}
diff --git a/apps/desktop/electron/singleton-lock.test.ts b/apps/desktop/electron/singleton-lock.test.ts
index 2ca97bfaeb5..fcb00de11e6 100644
--- a/apps/desktop/electron/singleton-lock.test.ts
+++ b/apps/desktop/electron/singleton-lock.test.ts
@@ -43,6 +43,7 @@ test('a zombie or missing owner is stale; kill(pid,0) alone would say alive', ()
test('removeStaleSingletonLock unlinks only a provably-dead owner', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-singleton-lock-'))
+
try {
const lockPath = path.join(root, 'SingletonLock')
diff --git a/apps/desktop/electron/singleton-lock.ts b/apps/desktop/electron/singleton-lock.ts
index a1378f6c3b7..3498459277f 100644
--- a/apps/desktop/electron/singleton-lock.ts
+++ b/apps/desktop/electron/singleton-lock.ts
@@ -15,13 +15,22 @@ export function parseSingletonLockPid(
linkTarget: string | null | undefined,
hostname: string
): number | null {
- if (typeof linkTarget !== 'string' || linkTarget === '') return null
+ if (typeof linkTarget !== 'string' || linkTarget === '') {
+ return null
+ }
const prefix = `${hostname}-`
- if (!linkTarget.startsWith(prefix)) return null
+
+ if (!linkTarget.startsWith(prefix)) {
+ return null
+ }
const tail = linkTarget.slice(prefix.length)
const pid = Number.parseInt(tail, 10)
+
// Reject `123junk` / `+4`-style targets: only a bare decimal PID counts.
- if (!Number.isInteger(pid) || pid <= 0 || String(pid) !== tail) return null
+ if (!Number.isInteger(pid) || pid <= 0 || String(pid) !== tail) {
+ return null
+ }
+
return pid
}
@@ -33,11 +42,16 @@ export function parseSingletonLockPid(
*/
export function parseProcStateField(statLine: string): string | null {
const close = statLine.lastIndexOf(')')
- if (close === -1) return null
+
+ if (close === -1) {
+ return null
+ }
+
const state = statLine
.slice(close + 2)
.split(' ')[0]
.trim()
+
return state === '' ? null : state
}
@@ -51,7 +65,10 @@ export function parseProcStateField(statLine: string): string | null {
* `/proc` entry at all) is a dead owner and equally stale.
*/
export function isStaleSingletonLockOwner(procState: string | null | undefined): boolean {
- if (procState === null || procState === undefined) return true
+ if (procState === null || procState === undefined) {
+ return true
+ }
+
return procState.trim() === 'Z'
}
@@ -80,11 +97,14 @@ export function removeStaleSingletonLock(
readProcState?: (pid: number) => string | null
} = {}
): number | null {
- if ((deps.platform ?? process.platform) !== 'linux') return null
+ if ((deps.platform ?? process.platform) !== 'linux') {
+ return null
+ }
const hostname = deps.hostname ?? os.hostname()
const readProcState = deps.readProcState ?? readLinuxProcState
let target: string
+
try {
target = fs.readlinkSync(path.join(userDataDir, 'SingletonLock'))
} catch {
@@ -93,14 +113,20 @@ export function removeStaleSingletonLock(
}
const pid = parseSingletonLockPid(target, hostname)
- if (pid === null) return null
- if (!isStaleSingletonLockOwner(readProcState(pid))) return null
+ if (pid === null) {
+ return null
+ }
+
+ if (!isStaleSingletonLockOwner(readProcState(pid))) {
+ return null
+ }
try {
fs.unlinkSync(path.join(userDataDir, 'SingletonLock'))
} catch {
return null
}
+
return pid
}