From 95580ccea72f8da8de845e0529813d565d5b8283 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Tue, 9 Jan 2024 16:02:51 -0800 Subject: [PATCH 1/2] core(target-manager): warn on errors with worker targets --- core/gather/driver/target-manager.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/gather/driver/target-manager.js b/core/gather/driver/target-manager.js index 6e1eef5adf05..146810df67d9 100644 --- a/core/gather/driver/target-manager.js +++ b/core/gather/driver/target-manager.js @@ -125,9 +125,11 @@ class TargetManager extends ProtocolEventEmitter { async _onSessionAttached(cdpSession) { const newSession = new ProtocolSession(cdpSession); + let targetType; + try { const {targetInfo} = await newSession.sendCommand('Target.getTargetInfo'); - const targetType = targetInfo.type; + targetType = targetInfo.type; // TODO: should detach from target in this case? // See pptr: https://github.com/puppeteer/puppeteer/blob/733cbecf487c71483bee8350e37030edb24bc021/src/common/Page.ts#L495-L526 @@ -168,6 +170,13 @@ class TargetManager extends ProtocolEventEmitter { // Sometimes targets can be closed before we even have a chance to listen to their network activity. if (/Target closed/.test(err.message)) return; + // Worker targets can be a bit fickle and we only enable them for diagnostic purposes. + // We shouldn't throw a fatal error if there were issues attaching to them. + if (targetType === 'worker') { + log.warn('target-manager', `Issue attaching to worker target: ${err}`); + return; + } + throw err; } finally { // Resume the target if it was paused, but if it's unnecessary, we don't care about the error. From d69dc3405c08b267d72c622aceef91aed5ea44b5 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Tue, 9 Jan 2024 16:08:40 -0800 Subject: [PATCH 2/2] test --- core/test/gather/driver/target-manager-test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/test/gather/driver/target-manager-test.js b/core/test/gather/driver/target-manager-test.js index ff492d909902..c74a5f2508ec 100644 --- a/core/test/gather/driver/target-manager-test.js +++ b/core/test/gather/driver/target-manager-test.js @@ -108,6 +108,23 @@ describe('TargetManager', () => { expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(4); }); + it('should ignore errors while attaching to worker targets', async () => { + targetInfo.type = 'worker'; + sendMock + .mockResponse('Target.getTargetInfo', {targetInfo}) + .mockResponse('Network.enable', () => { + throw new Error('Cannot use Network.enable'); + }) + .mockResponse('Target.setAutoAttach'); + await targetManager.enable(); + + const invocations = sendMock.findAllInvocations('Target.setAutoAttach'); + expect(invocations).toHaveLength(0); + + // Should still be resumed. + expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1); + }); + it('should ignore targets that are not frames or web workers', async () => { targetInfo.type = 'service_worker'; sendMock