-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
usePermission
creates memory leaks
#4149
Comments
useClipboard
(with useMounted
being the underlying culprit?) creates memory leaksuseClipboard
(with useMounted
being the underlying culprit) creates memory leaks
useClipboard
(with useMounted
being the underlying culprit) creates memory leaksuseClipboard
(with useSupported
being the underlying culprit) creates memory leaks
I noticed you mentioned the issue about the effect callback in |
@Onion-L I'm not sure that is the intended use; in fact, in the computed body If the intention is to make the ref read only, I think the appropriate thing would be to return |
@kiroushi Yes, you are right. I ran the test and observed a few failures🤯. I'll see if I can fix them. |
export function useSupported(callback: () => unknown) {
const isMounted = useMounted()
const isSupported = ref(false)
watch(isMounted, () => {
isSupported.value = Boolean(callback())
}, { immediate: true })
return readonly(isSupported)
} I tried this, and it works and has passed all the tests. I notice that, for the first time, the |
@kiroushi Why you don't force the GC between runs? The effect may be not trivial to dispose and the GC may take more time to kick automatically (but we can force by doing a manual run). I haven't tried your repo yet, so I'm just doing wild guesses as of now (I'm a bit busy these days but would like to help with this sometime next week). CC as well #4154 (comment) |
@ferferga Thank you for your comment. Yes, you are totally right; I didn't consider the garbage collection, and it will be great if you can take a look at this problem. I think I am a bit too hasty, and I am sorry for this. |
@ferferga @Onion-L sorry for not providing the GC forcing step. The memory leak is there, definitely. Recording.2024-08-16.161256.mp4Edit: I think I've narrowed down the issue to another part of the implementation. At this point, I don't know if the GC is being prevented on The pinpoint is definitely around the implementation of the latter in conjunction with the former around this line:
Changing that to just Also in:
Removing the reference to There's a weird interaction between the computed effect in I don't have more time to investigate during the weekend but maybe this might shed some light to anyone trying to help with this. |
useClipboard
(with useSupported
being the underlying culprit) creates memory leaksusePermission
creates memory leaks
Update: Actually I had some more time to investigate today and I think I've traced down the issue, finally. The issue is not on vueuse/packages/core/usePermission/index.ts Lines 82 to 96 in a9b49f5
Changing I will refactor the permission.mp4 |
@Onion-L No problem! This is why reviews exist, because others might have noticed things that you haven't! It could have been the opposite: I might have been in the wrong and you already did the investigation I thought you didn't, just forgot to post about it!. @kiroushi I saw your message yesterday and, after a quick glance at the code, I suspected about it since the beginning, since I also had problems with useEventListener in the past, but I was also wondering why the useSingletonPromise is needed, because I think that's more problematic itself, since Either case, what's more strange, is that I recreated your reproduction in the SFC Playground and I could not reproduce your problem. That makes me wonder if the leak is caused because you're doing the reproductions in development (+ HMR) and not in production? Either way, I think the refactor I'm mentioning makes sense, I'll make a branch in my fork too with my proposed changes. |
@kiroushi Made the changes in this branch, please test them so, if they fix the issue, I can make a PR and get this going 🚀. To test you can either copy the source code to your own project and import from there or build the package (using Codespaces is easiest) and replace the contents from dist in the node_modules of your project. |
@ferferga thanks for your quick turnaround. I'm not sure what could be the reason you cannot reproduce it, but certainly the leak is present in both development with HMR env and prod. build. In any case, your solution seems to be working in an elegant way by getting rid of some of the complexity of the implementation. I am also unsure of the underlying need to use this "singleton promise", but in any case it escapes the purpose of this change. In your branch, I would only suggest one small change, which is to remove the exogenous reference in useEventListener(permissionStatus, 'change', (e) => {
state.value = (e.target as PermissionStatus)?.state ?? 'prompt'
})
const query = createSingletonPromise(async () => {
if (!isSupported.value)
return
if (!permissionStatus.value) {
try {
permissionStatus.value = await navigator!.permissions.query(desc)
state.value = permissionStatus.value.state
}
catch {
state.value = 'prompt'
}
}
if (controls)
return toRaw(permissionStatus.value)
}) Thanks for taking the time to help with this ;) |
@kiroushi I understood why after working in the branch: given further controls are exposed to the user (with the I'll leave your proposed changes as the additional context of the PR so they're discussed in code review since I'm not too convinced of them because:
PR opened at #4157 |
@ferferga sounds good. It is just a nitpick as well. The reasoning behind falling back to https://developer.mozilla.org/en-US/docs/Web/API/PermissionStatus/state If we're falling back to But, as you said, it might be introducing a divergent implementation and might be breaking current uses, so no point on bundling it with the issue at hand. |
Describe the bug
Update:
All of the information below is incorrect. The culprit is not
useClipboard
butusePermission
. Refer to #4149 (comment).Video:
Recording.2024-08-14.105556.mp4
Reproduction
https://github.com/kiroushi/vueuse-clipboard-memory-leak
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: