Skip to content

Commit f73aca3

Browse files
committed
refactor(exec): review polish — drop the duplicated release comment, pin every forwarded signal's cleanup
- one copy of the leave-a-stale-claim note in releaseExecDeviceClaim (the unattributable branch below already carries it). - the SIGTERM case now snapshots process.listenerCount for every FORWARDED_SIGNALS entry and asserts all of them return to baseline, so a leaked SIGQUIT or SIGHUP handler fails here too. Rebase note: main's claim schema v3 (#3345) made the owner kind a required claimOwnerKind discriminant, so the wrapper kind rides that field instead of a parallel ownerKind, and the supersession guard checks claimOwnerKind !== 'wrapper'.
1 parent ab28ac5 commit f73aca3

5 files changed

Lines changed: 19 additions & 16 deletions

File tree

‎CONTEXT.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ A mutual-exclusion guard for a platform helper process. It is not remote client
6969
_Avoid_: Device lease, process lease
7070

7171
**Device claim**:
72-
Host-global exclusive ownership of one local device by an open session, a sessionless mutating
73-
command, or an `exec` wrapper for the lifetime of its child process.
72+
Host-global exclusive ownership of one local device: open session, sessionless command, or exec
73+
wrapper.
7474

7575
**Device-claim policy**:
7676
A command's observation, ownership, or exclusive-mutation rule.

‎packages/daemon-contracts/src/device-claim-record.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ export const DEVICE_CLAIM_SCHEMA_VERSION = 3;
2020
* `wrapper` names a plain CLI process holding the device for the lifetime of a child it runs
2121
* (#3263); it is not a daemon, so the daemon-supersession proof never reads against its record.
2222
*/
23-
export type DeviceClaimOwnerKind =
24-
| 'open-session'
25-
| 'transient-command'
26-
| 'wrapper'
27-
| 'unstated';
23+
export type DeviceClaimOwnerKind = 'open-session' | 'transient-command' | 'wrapper' | 'unstated';
2824

2925
const DEVICE_CLAIM_OWNER_KINDS: readonly DeviceClaimOwnerKind[] = [
3026
'open-session',

‎packages/daemon-contracts/src/device-claims.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ async function claimHeldDevice(params: {
247247
const { deviceKey, identity } = params;
248248
const owner = readCurrentOwnerIdentity();
249249
const claimOwnerKind: DeviceClaimAcquirerKind =
250-
params.claimOwnerKind ?? (params.transientCommand === undefined ? 'open-session' : 'transient-command');
250+
params.claimOwnerKind ??
251+
(params.transientCommand === undefined ? 'open-session' : 'transient-command');
251252
const existing = await resolveExistingClaim({
252253
...params,
253254
owner,

‎src/cli/commands/__tests__/exec.test.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@agent-device/daemon-contracts/device-claims';
1414
import { runCmdBackground } from '@agent-device/host-kit/command';
1515
import type { CliFlags } from '@agent-device/contracts/command';
16-
import { runExecCommand } from '../exec.ts';
16+
import { FORWARDED_SIGNALS, runExecCommand } from '../exec.ts';
1717

1818
// #3263: `exec` takes the host-global device claim on its own process identity,
1919
// runs the child, and releases on every exit path. The claim assertions read the
@@ -183,7 +183,7 @@ test('records the wrapper claim with its owner kind under an exec state dir', as
183183
await running.inventoryReady();
184184
await waitFor(() => claimEntry()?.claim?.session === 'exec:sh');
185185
const stored = storedClaim(dirs.claimsDir);
186-
assert.equal(stored?.ownerKind, 'wrapper');
186+
assert.equal(stored?.claimOwnerKind, 'wrapper');
187187
assert.equal(stored?.stateDir, path.join(dirs.stateDir, 'exec'));
188188
assert.equal(claimEntry()?.classification, 'live');
189189
} finally {
@@ -301,8 +301,12 @@ test.skipIf(IS_WINDOWS)(
301301
const grandchildGotSignal = path.join(dirs.stateDir, 'grandchild-got-term.txt');
302302
const childStarted = path.join(dirs.stateDir, 'child-started.txt');
303303
// An in-place emit reaches every SIGTERM listener in this worker, so pin that
304-
// the wrapper starts from the worker's own baseline and returns to it.
305-
const sigtermListenersBefore = process.listenerCount('SIGTERM');
304+
// the wrapper starts from the worker's own baseline and returns to it — for
305+
// every signal the wrapper forwards, so a leaked SIGQUIT or SIGHUP handler
306+
// fails here too.
307+
const signalListenersBefore = new Map(
308+
FORWARDED_SIGNALS.map((name) => [name, process.listenerCount(name)]),
309+
);
306310
const running = runExec(
307311
[
308312
'sh',
@@ -328,7 +332,11 @@ test.skipIf(IS_WINDOWS)(
328332
// also proves the group signal left this worker.
329333
process.emit('SIGTERM');
330334
assert.equal(await running.promise, 128 + 15);
331-
assert.equal(process.listenerCount('SIGTERM'), sigtermListenersBefore);
335+
// The wrapper registered a handler for every forwarded signal even though
336+
// only SIGTERM arrived, so every one of them must be gone again.
337+
for (const [name, before] of signalListenersBefore) {
338+
assert.equal(process.listenerCount(name), before);
339+
}
332340
assert.equal(fs.readFileSync(gotSignal, 'utf8').trim(), 'got');
333341
await waitFor(() => fs.existsSync(grandchildGotSignal));
334342
assert.equal(fs.readFileSync(grandchildGotSignal, 'utf8').trim(), 'got');

‎src/cli/commands/exec.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ type SignalForwarding = Readonly<{
191191
// deliberately absent: stopping the wrapper with the child left running is the
192192
// shell's own semantics, and job control here would need a controlling
193193
// terminal the detached child does not share.
194-
const FORWARDED_SIGNALS = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'] as const;
194+
export const FORWARDED_SIGNALS = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'] as const;
195195

196196
function attachSignalForwarding(): SignalForwarding {
197197
let first: NodeJS.Signals | null = null;
@@ -241,8 +241,6 @@ async function releaseExecDeviceClaim(ownership: DeviceClaimSessionOwnership): P
241241
error instanceof Error ? error.message : String(error)
242242
}\n`,
243243
);
244-
// The wrapper is about to exit, so a claim left behind names a process that
245-
// is gone and every acquirer reconciles it as stale.
246244
return;
247245
}
248246
// The wrapper is about to exit, so a claim left behind names a process that is

0 commit comments

Comments
 (0)