Skip to content

Commit d055e37

Browse files
committed
Reattach CPU capture to fresh game workers after restart
1 parent bfb9628 commit d055e37

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎engine/cpu-capture.test.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,55 @@ test('capture reuses one attachment and submits only the fixed read-only express
7272
const calls:any[]=[];
7373
const session={send:async (...args:any[])=>{
7474
calls.push(args);
75-
if(args[0]==='Target.getTargets') return {result:{targetInfos:[{targetId:'game-worker',type:'worker',url:'http://localhost:5174/src/worker/emulator.worker.ts?worker_file&type=module'}]}};
75+
if(args[0]==='Target.getTargetInfo') return {result:{targetInfo:{targetId:'game-page',type:'page',url:'http://localhost:5174/?game=dev'}}};
76+
if(args[0]==='Target.getTargets') return {result:{targetInfos:[{targetId:'game-worker',parentId:'game-page',type:'worker',url:'http://localhost:5174/src/worker/emulator.worker.ts?worker_file&type=module'}]}};
7677
if(args[0]==='Target.attachToTarget') return {result:{sessionId:'worker-session'}};
7778
return {result:{result:{value:fixture()}}};
7879
}};
7980
const capture=new CpuCapture();
8081
await capture.capture(session); await capture.capture(session);
8182
expect(calls.filter(call=>call[0]==='Target.attachToTarget')).toHaveLength(1);
83+
expect(calls.filter(call=>call[0]==='Target.setAutoAttach')).toEqual([
84+
['Target.setAutoAttach',{autoAttach:true,waitForDebuggerOnStart:false,flatten:true}]]);
8285
expect(calls.filter(call=>call[0]==='Target.setDiscoverTargets')).toHaveLength(1);
8386
expect(calls.filter(call=>call[0]==='Runtime.enable')).toEqual([['Runtime.enable',{},'worker-session']]);
84-
expect(calls.slice(0,4).map(call=>call[0])).toEqual([
85-
'Target.setDiscoverTargets','Target.getTargets','Target.attachToTarget','Runtime.enable']);
87+
expect(calls.slice(0,6).map(call=>call[0])).toEqual([
88+
'Target.setAutoAttach','Target.setDiscoverTargets','Target.getTargetInfo','Target.getTargets','Target.attachToTarget','Runtime.enable']);
8689
const evaluations=calls.filter(call=>call[0]==='Runtime.evaluate');
8790
expect(evaluations).toHaveLength(2);
8891
for(const call of evaluations) {
8992
expect(call[1]).toEqual({expression:CPU_FRAME_EXPRESSION,returnByValue:true,awaitPromise:false});
9093
expect(call[2]).toBe('worker-session');
9194
}
9295
});
96+
97+
test('fresh blank-URL worker attaches only through its current page parent and reset rediscovers', async () => {
98+
const attached:string[]=[];
99+
let generation=1;
100+
const session={send:async (command:string,args:any)=>{
101+
if(command==='Target.getTargetInfo') return {result:{targetInfo:{targetId:'game-page',type:'page',url:'http://localhost:5174/?game=dev'}}};
102+
if(command==='Target.getTargets') return {result:{targetInfos:[
103+
{targetId:`emulator-${generation}`,parentId:'game-page',type:'worker',url:''},
104+
{targetId:'nested',parentId:`emulator-${generation}`,type:'worker',url:''},
105+
{targetId:'other-tab-worker',parentId:'other-page',type:'worker',url:'http://localhost:5174/src/worker/emulator.worker.ts?worker_file'},
106+
]}};
107+
if(command==='Target.attachToTarget') {attached.push(args.targetId);return {result:{sessionId:`session-${generation}`}};}
108+
return {result:{result:{value:fixture()}}};
109+
}};
110+
const capture=new CpuCapture();
111+
await capture.capture(session); generation=2; capture.reset(); await capture.capture(session);
112+
expect(attached).toEqual(['emulator-1','emulator-2']);
113+
});
114+
115+
test('capture refuses ambiguous direct workers and non-BottleShip parent pages', async () => {
116+
let pageUrl='http://localhost:5174/?game=dev';
117+
const session={send:async (command:string)=>{
118+
if(command==='Target.getTargetInfo') return {result:{targetInfo:{targetId:'game-page',type:'page',url:pageUrl}}};
119+
if(command==='Target.getTargets') return {result:{targetInfos:[1,2].map(i=>({targetId:`w${i}`,parentId:'game-page',type:'worker',url:''}))}};
120+
if(command==='Target.attachToTarget') throw Error('Must not attach to an ambiguous or unrelated worker');
121+
return {};
122+
}};
123+
await expect(new CpuCapture().capture(session)).rejects.toThrow('ambiguous');
124+
pageUrl='https://example.com/';
125+
await expect(new CpuCapture().capture(session)).rejects.toThrow('local BottleShip page');
126+
});

‎engine/cpu-capture.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,20 @@ export class CpuCapture {
7777
if (!this.workerSession) {
7878
// Follow BottleShip's worker transport initialization explicitly, so the
7979
// first capture does not depend on an earlier diagnostic attachment.
80+
await session.send('Target.setAutoAttach',{autoAttach:true,waitForDebuggerOnStart:false,flatten:true});
8081
await session.send('Target.setDiscoverTargets',{discover:true});
82+
const current = await session.send('Target.getTargetInfo');
83+
const page = current.result?.targetInfo;
84+
if (page?.type !== 'page' || typeof page.targetId !== 'string'
85+
|| !/^http:\/\/localhost:5174\/(?:\?|$)/.test(page.url ?? ''))
86+
throw Error('Capture requires the local BottleShip page');
8187
const targets = await session.send('Target.getTargets');
8288
const candidates = (targets.result?.targetInfos ?? []).filter((target: any) =>
83-
target.type === 'worker' && target.url.startsWith('http://localhost:5174/src/worker/emulator.worker.ts?'));
89+
// Chrome can leave a fresh worker's URL/title empty until attached.
90+
// Parent ownership still distinguishes the page's emulator from its
91+
// nested workers and workers belonging to other tabs.
92+
target.type === 'worker' && target.parentId === page.targetId
93+
&& (target.url === '' || target.url?.startsWith('http://localhost:5174/src/worker/emulator.worker.ts?')));
8494
if (candidates.length !== 1) throw Error('Emulator worker is ambiguous or unavailable');
8595
const attached = await session.send('Target.attachToTarget',{targetId:candidates[0].targetId,flatten:true});
8696
this.workerSession = attached.result?.sessionId;

0 commit comments

Comments
 (0)