Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix execution context setting for iframes #1254

Merged
merged 2 commits into from
Apr 25, 2024
Merged

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Mar 21, 2024

What?

This fixes a problem with setting the execution context when testing a website that uses iframes. Without this fix it could end up in an infinite loop when waitForExecutionContext is called.

Why?

There is a race condition when it comes to attaching iframes and the execution context that apply to these frames. What usually occurs is:

  1. An exec context for about:blank is first set;
  2. A new set event is received for exec context for the url pointing to the actual destination for the iframe;
  3. Finally the execution context for about:blank is destroyed, not for the second execution context.

This is the order of events when iframes are in use on a site, and so it is safe to nil the original execution context and overwrite it with the second one.

The exec context destroyed event will not remove the new exec context since the ids do not match.

If we didn't overwrite the first execCtx with the new one, then waitForExecutionContext could end up waiting indefinitely since all execCtx were destroyed.

Reference: Playwright

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates: #1252

Since this fixes an interaction with iframes, we now see more panic: GoError: dispose: canceled errors, which there is an open issue for: #1089.

@ankur22 ankur22 marked this pull request as draft March 21, 2024 17:29
@ankur22 ankur22 marked this pull request as ready for review March 27, 2024 13:31
@ankur22 ankur22 requested a review from inancgumus March 27, 2024 13:31
@ankur22 ankur22 marked this pull request as draft March 27, 2024 17:08
@ankur22 ankur22 marked this pull request as ready for review April 5, 2024 16:34
Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I still see the same error (after running the user's script with this branch) 😭 The reason you might not see the error is probably because of a race condition since the script uses a lot of sleeps.

@ankur22 ankur22 force-pushed the fix/exec-context-session branch 2 times, most recently from a752709 to 349c9e0 Compare April 25, 2024 08:48
@ankur22
Copy link
Collaborator Author

ankur22 commented Apr 25, 2024

Unfortunately, I still see the same error (after running the user's script with this branch) 😭 The reason you might not see the error is probably because of a race condition since the script uses a lot of sleeps.

Yep, you are correct, the same error that was posted in the original post still occurs 😢, when ran in headless mode:

panic: GoError: dispose: disposing element with ID -28511178728990825.29.1: Cannot find context with specified id (-32000)

However this same issue doesn't occur in headful mode with:

K6_BROWSER_HEADLESS=false k6 run e2e/test.js

Without this fix, in headful mode, the test ends up in an infinite loop when waitForExecutionContext is called. I think it's still a valid fix, but I will need to go back to the post and correct my assumption. I'll then look at resolving the original error 👍

@inancgumus
Copy link
Member

inancgumus commented Apr 25, 2024

However this same issue doesn't occur in headful mode with:

K6_BROWSER_HEADLESS=false k6 run e2e/test.js

Without this fix, in headful mode, the test ends up in an infinite loop when waitForExecutionContext is called. I think it's still a valid fix, but I will need to go back to the post and correct my assumption. I'll then look at resolving the original error 👍

On the main branch, VUs get stuck after entering the email address. Yeah, this definitely fixes the "infinite loop" issue in headful mode 👏

On this fix's branch, when I run this fix in headful mode, I receive the following error. I also noticed that the Chromium processes didn't shut down.

panic: GoError: dispose: canceled

goroutine 403 [running]:
go.k6.io/k6/js/common.Throw(...)
        go.k6.io/k6@v0.50.0/js/common/util.go:20
github.com/grafana/xk6-browser/k6ext.Panic.func1(0x140028ee008, {0x14005e6cdb0?, 0x10220d601?, 0x14004670f50?})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:35 +0x7c
github.com/grafana/xk6-browser/k6ext.sharedPanic({0x102511488, 0x14006e26fa0}, 0x14006df5a40, {0x14005e6cdb0, 0x1, 0x1})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:64 +0x20c
github.com/grafana/xk6-browser/k6ext.Panic({0x102511488?, 0x14006e26fa0?}, {0x101d97046?, 0x102222420?}, {0x14005e6cdb0?, 0x102222420?, 0x140067e6660?})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:37 +0x58
github.com/grafana/xk6-browser/common.(*BaseJSHandle).Dispose(0x1400896d590)
        github.com/grafana/xk6-browser@v1.4.3/common/js_handle.go:83 +0xa0
github.com/grafana/xk6-browser/common.(*Frame).detach(0x1400e63a540)
        github.com/grafana/xk6-browser@v1.4.3/common/frame.go:243 +0x100
github.com/grafana/xk6-browser/common.(*FrameManager).removeFramesRecursively(0x14007104300, 0x1400e63a540)
        github.com/grafana/xk6-browser@v1.4.3/common/frame_manager.go:419 +0x1a8
github.com/grafana/xk6-browser/common.(*FrameManager).frameDetached(0x14007104300, {0x14004215aa0, 0x20}, {0x101d8eb21, 0x6})
        github.com/grafana/xk6-browser@v1.4.3/common/frame_manager.go:185 +0x110
github.com/grafana/xk6-browser/common.(*FrameSession).onFrameDetached(0x1400713c160, {0x14004215aa0, 0x20}, {0x101d8eb21, 0x6})
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:748 +0x11c
github.com/grafana/xk6-browser/common.(*FrameSession).initEvents.func1()
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:254 +0x664
created by github.com/grafana/xk6-browser/common.(*FrameSession).initEvents in goroutine 187
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:220 +0x180

What do you suggest about the next steps for issue #1252? I mean we can merge this PR, but keep the issue open. Or?

@ankur22
Copy link
Collaborator Author

ankur22 commented Apr 25, 2024

On this fix's branch, when I run this fix in headful mode, I receive the following error. I also noticed that the Chromium processes didn't shut down.

panic: GoError: dispose: canceled

goroutine 403 [running]:
go.k6.io/k6/js/common.Throw(...)
        go.k6.io/k6@v0.50.0/js/common/util.go:20
github.com/grafana/xk6-browser/k6ext.Panic.func1(0x140028ee008, {0x14005e6cdb0?, 0x10220d601?, 0x14004670f50?})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:35 +0x7c
github.com/grafana/xk6-browser/k6ext.sharedPanic({0x102511488, 0x14006e26fa0}, 0x14006df5a40, {0x14005e6cdb0, 0x1, 0x1})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:64 +0x20c
github.com/grafana/xk6-browser/k6ext.Panic({0x102511488?, 0x14006e26fa0?}, {0x101d97046?, 0x102222420?}, {0x14005e6cdb0?, 0x102222420?, 0x140067e6660?})
        github.com/grafana/xk6-browser@v1.4.3/k6ext/panic.go:37 +0x58
github.com/grafana/xk6-browser/common.(*BaseJSHandle).Dispose(0x1400896d590)
        github.com/grafana/xk6-browser@v1.4.3/common/js_handle.go:83 +0xa0
github.com/grafana/xk6-browser/common.(*Frame).detach(0x1400e63a540)
        github.com/grafana/xk6-browser@v1.4.3/common/frame.go:243 +0x100
github.com/grafana/xk6-browser/common.(*FrameManager).removeFramesRecursively(0x14007104300, 0x1400e63a540)
        github.com/grafana/xk6-browser@v1.4.3/common/frame_manager.go:419 +0x1a8
github.com/grafana/xk6-browser/common.(*FrameManager).frameDetached(0x14007104300, {0x14004215aa0, 0x20}, {0x101d8eb21, 0x6})
        github.com/grafana/xk6-browser@v1.4.3/common/frame_manager.go:185 +0x110
github.com/grafana/xk6-browser/common.(*FrameSession).onFrameDetached(0x1400713c160, {0x14004215aa0, 0x20}, {0x101d8eb21, 0x6})
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:748 +0x11c
github.com/grafana/xk6-browser/common.(*FrameSession).initEvents.func1()
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:254 +0x664
created by github.com/grafana/xk6-browser/common.(*FrameSession).initEvents in goroutine 187
        github.com/grafana/xk6-browser@v1.4.3/common/frame_session.go:220 +0x180

This shouldn't happen since the fix was merged into main and main was rebased to this fix branch. Looking at the stack trace it seems that you may be running an older version of the branch?

> github.com/grafana/xk6-browser/common.(*BaseJSHandle).Dispose(0x1400896d590)
>         github.com/grafana/xk6-browser@v1.4.3/common/js_handle.go:83 +0xa0

In the latest version this points to a comment and not to a call to panic. Do you mind rebasing/pulling and trying again?

@inancgumus
Copy link
Member

inancgumus commented Apr 25, 2024

🤦 You're right. It somehow cached the version. I should have checked the versions in the stack trace. Apologies for the noise. WDYT about this since the fix only applies to the headful version?

What do you suggest about the next steps for the issue #1252? I mean, we can merge this PR but keep the issue open. Or?

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

@ankur22
Copy link
Collaborator Author

ankur22 commented Apr 25, 2024

🤦 You're right. It somehow cached the version. I should have checked the versions in the stack trace. Apologies for the noise. WDYT about this?

🙂 👍

What do you suggest about the next steps for the issue #1252? I mean, we can merge this PR but keep the issue open. Or?

I believe that this PR does fix the issue it is attached to (#1252), and so we can close that issue once this PR is merged.

Unfortunately it doesn't fix the original issue from the community post, but there is an open issue for that already #1144. I plan to work on that next.

It doesn't make sense for this operation to continue when the frame is
not present, so return early. If the frame is present continue as
normal.
The events are as follows when an iframe is attached:
1. execCtx for about:blank for new frame is set;
2. execCtx for navigated frame with url is set;
3. original about:blank execCtx is destroyed.

If we only work with the original execCtx, then it will be destroyed by
the 3rd event. To prevent this we overwrite the original one with the
new one in the 2nd event.

What this means is that we do not end up in an infinite loop when
waitForExecutionContext is called.
@ankur22 ankur22 merged commit 64c825d into main Apr 25, 2024
17 checks passed
@ankur22 ankur22 deleted the fix/exec-context-session branch April 25, 2024 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants