-
-
Notifications
You must be signed in to change notification settings - Fork 278
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(macos): Prevent NSExceptions when dropping a webview with registered protocols #1215
Conversation
Is #1214 handling the same issue? I would prefer the fix here and just check if the webview_id is still on the running list. |
No. My PR fixes a panic/mem issue when the InnerWebview struct is dropped while protocols are registered. 1214 fixes an issue when the dev aborts a request to a protocol via AbortHandler for example (a page reload may causes this too). This will make wkwebview send the stopTask event which is not triggered for the issue i'm trying to fix here. |
don't mind the last commits, i'm so done for today 😂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason is is_null
still returns false is that because it is not set to std::ptr::null
, I have seen a similar issue in #1206
I think we should fix it like this:
- pass the webview pointer to the responder closure
- when dropping the webview, we explicitly set the value at the webview ptr address to a null ptr or just a unit (`()``
- check inside the responder if the value pointed by the webview ptr is null
Thank you, i'll see if i can make that work. |
Hmm, i may be doing something wrong, but i can't make it work like this. It also looks like the webview in the start_task and the one in drop point to different addresses. |
cc @wusyong |
@lucasfernog Are you sure it's working? I believe @FabianLars mentioned it didn't work as intended. |
The PR's current implementation works for me. |
Ah sorry for the misunderstanding. |
fixes tauri-apps/tauri#7898
This one was quiet hard to fix, i really tried to not rely on a global variable but always ended up with either nsexceptions or stack traces for invalid memory access. The problem is that when you're dropping the webview, like tauri does on window close, is that protocol handlers may still be running. In Tauri this is even more of an issue because it uses a protocol for the whole IPC implementation so even the close-requested event will cause problems here.
If the webview is dropped before the handler finished, either _webview or task (or both) will be undefined, but in a way that objc's is_null still returns false, but if you check its class it will be returned as
nil
. Relying on that unfortunetely doesn't work because it requires 2 calls to the object / class (first to get the class, second to get the class name) and the second one really often causes memory issues. Also the timing is unpredictable, sometimes it fails on the didReceive call, sometimes on the didFinish call, rarely on the didReceiveResponse call.Since wry consumers can't know when the handler was finished (especially the responder part inside wry) i believe it would be unreasonable to expect users to make sure handlers are done before dropping the webview and instead it's something we should make sure.
P.S. I kinda hope someone comes up with a better solution because i'm not the biggest fan of the global webview vec myself, it's just the only thing i found that works reliably.
P.P.S. Also semi related to #1214 which is why i'm opening this pr right now and not waiting for tomorrow morning 🙃
Edit: Another thing that just came to mind, which i didn't investigate is whether returning early from the function will leak memory? For example the NSData instance when we don't end up calling didReceive. Even if this leaks memory, i'm not sure how to handle that in objc so some input on that topic would be appreciated :)