playwright-aws-lambda
playwright-aws-lambda copied to clipboard
Keep browser open between Lambda invocations
Whenever I try to keep the browser open between invocations, it seems like the browser gets closed/killed.
This is the error I'm getting after the first invocation:
{
"name": "Error",
"stack": [
": Protocol error (Target.createTarget): Browser closed.",
"==================== Browser output: ====================",
"[pid=25][err] #2 0x5652544896c5 (/tmp/chromium+0x48e46c4)",
"[pid=25][err] #3 0x7fa34c9137e0 (/usr/lib64/libpthread-2.26.so+0x117df)",
"[pid=25][err] #4 0x7fa34bbf0c20 __GI_raise",
"[pid=25][err] #5 0x7fa34bbf20c8 __GI_abort",
"[pid=25][err] #6 0x565254488625 (/tmp/chromium+0x48e3624)",
"[pid=25][err] #7 0x56525440a1c2 (/tmp/chromium+0x48651c1)",
"[pid=25][err] #8 0x5652534b3c78 (/tmp/chromium+0x390ec77)",
"[pid=25][err] #9 0x56525358908d (/tmp/chromium+0x39e408c)",
"[pid=25][err] #10 0x56525331b28b (/tmp/chromium+0x377628a)",
"[pid=25][err] #11 0x56525331b1b6 (/tmp/chromium+0x37761b5)",
"[pid=25][err] #12 0x5652535cdd21 (/tmp/chromium+0x3a28d20)",
"[pid=25][err] #13 0x5652535c2ec4 (/tmp/chromium+0x3a1dec3)",
"[pid=25][err] #14 0x5652535c2bc4 (/tmp/chromium+0x3a1dbc3)",
"[pid=25][err] #15 0x565255b3dba9 (/tmp/chromium+0x5f98ba8)",
"[pid=25][err] #16 0x565254583287 (/tmp/chromium+0x49de286)",
"[pid=25][err] #17 0x565255b49c8a (/tmp/chromium+0x5fa4c89)",
"[pid=25][err] #18 0x565255b49e09 (/tmp/chromium+0x5fa4e08)",
"[pid=25][err] #19 0x5652529f9b2d (/tmp/chromium+0x2e54b2c)",
"[pid=25][err] #20 0x5652529f5db8 (/tmp/chromium+0x2e50db7)",
"[pid=25][err] #21 0x565253e289b3 (/tmp/chromium+0x42839b2)",
"[pid=25][err] #22 0x565255b482f8 (/tmp/chromium+0x5fa32f7)",
"[pid=25][err] #23 0x565255b47598 (/tmp/chromium+0x5fa2597)",
"[pid=25][err] #24 0x5652532236ae (/tmp/chromium+0x367e6ad)",
"[pid=25][err] #25 0x5652532232b6 (/tmp/chromium+0x367e2b5)",
"[pid=25][err] #26 0x56525321f780 (/tmp/chromium+0x367a77f)",
"[pid=25][err] #27 0x565254438eeb (/tmp/chromium+0x4893eea)",
"[pid=25][err] #28 0x56525444982e (/tmp/chromium+0x48a482d)",
"[pid=25][err] #29 0x5652544495c0 (/tmp/chromium+0x48a45bf)",
"[pid=25][err] #30 0x5652544a4629 (/tmp/chromium+0x48ff628)",
"[pid=25][err] #31 0x565254449db5 (/tmp/chromium+0x48a4db4)",
"[pid=25][err] #32 0x565254426dae (/tmp/chromium+0x4881dad)",
"[pid=25][err] #33 0x5652531a3dd4 (/tmp/chromium+0x35fedd3)",
"[pid=25][err] #34 0x5652531a57d2 (/tmp/chromium+0x36007d1)",
"[pid=25][err] #35 0x565255b45fc6 (/tmp/chromium+0x5fa0fc5)",
"[pid=25][err] #36 0x56525439c794 (/tmp/chromium+0x47f7793)",
"[pid=25][err] #37 0x56525439c417 (/tmp/chromium+0x47f7416)",
"[pid=25][err] #38 0x565255996641 (/tmp/chromium+0x5df1640)",
"[pid=25][err] #39 0x565254395801 (/tmp/chromium+0x47f0800)",
"[pid=25][err] #40 0x5652543f549d (/tmp/chromium+0x485049c)",
"[pid=25][err] #41 0x5652543f51bc (/tmp/chromium+0x48501bb)",
"[pid=25][err] #42 0x7fa34bbde0ba __libc_start_main",
"[pid=25][err] #43 0x56525257fa2a _start",
"[pid=25][err] r8: 0000000000000000 r9: 00007ffe792bfa70 r10: 0000000000000008 r11: 0000000000000246",
"[pid=25][err] r12: 00007ffe792c0e50 r13: 00007ffe792bfd10 r14: 00007ffe792c0e58 r15: aaaaaaaaaaaaaaaa",
"[pid=25][err] di: 0000000000000002 si: 00007ffe792bfa70 bp: 00007ffe792bfcc0 bx: 0000000000000006",
"[pid=25][err] dx: 0000000000000000 ax: 0000000000000000 cx: 00007fa34bbf0c20 sp: 00007ffe792bfa70",
"[pid=25][err] ip: 00007fa34bbf0c20 efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000",
"[pid=25][err] trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000",
"[pid=25][err] [end of stack trace]",
"[pid=25][err] Calling _exit(1). Core file will not be generated.",
" at captureStackTrace (/var/task/node_modules/playwright-core/lib/utils/stackTrace.js:60:17)",
" at BrowserContext._wrapApiCall (/var/task/node_modules/playwright-core/lib/client/channelOwner.js:101:58)",
" at BrowserContext.newPage (/var/task/node_modules/playwright-core/lib/client/browserContext.js:237:17)",
" at Runtime.exports.handler (/var/task/index.js:31:30)"
]
}
This is roughly the code I'm using:
const browser = require('playwright-aws-lambda').launchChromium()
exports.handler = async (event, options) => {
options.callbackWaitsForEmptyEventLoop = false
const context = await (await browser).newContext()
const page = await context.newPage()
await page.goto(event.url || 'https://example.com')
console.log('Page title: ', await page.title())
}
Do you know if this is a limitation of Lambda, or is there something else going on here? 🤔
You might try increasing the timeout of the lambda function.
In my case, I do use serverless framework, so I have a config file like this
functions:
playwright:
handler: handler.playwright
events:
- httpApi:
path: /playwright
method: get
timeout: 120
serverless.yml
You might try increasing the timeout of the lambda function.
I don't think that this is a Lambda timeout issue. I'm getting the indicated response right away, and it has a stack trace that points into playwright-core.
Anyone has a solution? I have the same problem.
We ended up restarting the browser for every call, but I'm very interested in a proper solution!
There's no guarantee that a Lambda will maintain state from one execution to the next. It may, but it is impossible to control. There's a lot of factors that go into this.
If you need have more control over this, you might want to use a long-lived service such as one in ECS.