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

[BUG] Background Service Worker loses states on hot reload #151

Closed
2 tasks done
belarusrulez opened this issue Aug 24, 2022 · 17 comments · Fixed by #255
Closed
2 tasks done

[BUG] Background Service Worker loses states on hot reload #151

belarusrulez opened this issue Aug 24, 2022 · 17 comments · Fixed by #255
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@belarusrulez
Copy link

What happened?

Hello.
Background script keeps loose state due to reloads.
I save some data in static vars or in classes parameters but because of reload it get's lost.
If I use production build everything works. With dev I have problems.

Is there a way to disable hot reload?
Thank you.

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

(OPTIONAL) Contact Details

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
@belarusrulez belarusrulez added the bug Something isn't working label Aug 24, 2022
@belarusrulez
Copy link
Author

image
public static readonly csReadyListeners: Set = new Set;
so basically it get's reloaded in the middle of logic and looses it's state
if I don't use dev env and use prod logic works and state is preserved

@belarusrulez
Copy link
Author

belarusrulez commented Aug 25, 2022

image

One more thing. If I kill pnpm dev it still reloads.

image

and that's how log looks if I use extension using prod build.

@ColdSauce
Copy link
Contributor

Hi @belarusrulez, thanks for your detailed report! Apologies for the delay, it's been quite a busy week for us.

Are you using MV2 or MV3? With MV3 you shouldn't have any expectations that the background service worker will stay alive and therefore it should be stateless.

Even without HMR, the background service worker has a fixed lifetime and will die when you don't expect it to, in production.

However, this isn't the case with MV2, so we'll need to figure out a good way to deal with that.

For the time being, if you don't want HMR, please continue to use the prod bundle.

We'll likely build a feature to disable HMR in dev but don't have that in yet. If you'd like to send a PR to add that functionality into plasmo, we'd greatly appreciate it. Something like plasmo build --no-hmr

@belarusrulez
Copy link
Author

belarusrulez commented Aug 25, 2022

Hey. Its MV3. It actually dies in the middle of code. It’s not idle when it’s get reloaded. Is there any way to build prod code readable under debug?

@ColdSauce
Copy link
Contributor

Oh, that's not great. Thanks for the context. We'll take a look soon

@belarusrulez
Copy link
Author

Thank you. I would like to make PR but have no idea where to start. too new in js/typescript. if you have in mind some kind example prs that can give me clue please send me link ill try to figure it out.

@belarusrulez
Copy link
Author

belarusrulez commented Aug 26, 2022

image

I found if I just remove selected "auto-reload.12a1e065.js" from manifest I don't have problems.

BTW I was trying to get the code and fix it contribution guidelines https://github.com/PlasmoHQ/plasmo/blob/main/.github/CONTRIBUTING.md and I was not able to pull. I got the code in zip and was not able to build it using guidelines.

@louisgv
Copy link
Contributor

louisgv commented Aug 27, 2022

image

I found if I just remove selected "auto-reload.12a1e065.js" from manifest I don't have problems.
BTW I was trying to get the code and fix it contribution guidelines https://github.com/PlasmoHQ/plasmo/blob/main/.github/CONTRIBUTING.md and I was not able to pull. I got the code in zip and was not able to build it using guidelines.

Can you paste the command you used to clone? To build the whole project, you will need to get the sub-module as well (since we hoist all our lockfiles to the top)

@louisgv louisgv changed the title [BUG] Background script keeps loose state due to reloads. [BUG] background script reloads losing states Aug 27, 2022
@belarusrulez
Copy link
Author

belarusrulez commented Aug 28, 2022

I tried
git clone [email protected]:belarusrulez/plasmo.git --recurse-submodules
and
git clone [email protected]:PlasmoHQ/plasmo.git --recurse-submodules
It gives

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.```

@belarusrulez
Copy link
Author

also I tried the same with https:

Failed to clone 'packages/permission-ui'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/prettier-plugin-sort-imports'...
remote: Enumerating objects: 1584, done.
remote: Counting objects: 100% (403/403), done.
remote: Compressing objects: 100% (194/194), done.
remote: Total 1584 (delta 270), reused 301 (delta 206), pack-reused 1181
Receiving objects: 100% (1584/1584), 2.12 MiB | 13.11 MiB/s, done.
Resolving deltas: 100% (964/964), done.
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/puro'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/puro.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/packages/puro' failed
Failed to clone 'packages/puro'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/rps'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/rps.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/packages/rps' failed
Failed to clone 'packages/rps'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/storage'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/storage.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/packages/storage' failed
Failed to clone 'packages/storage'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/use-hashed-state'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/use-hashed-state.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/packages/use-hashed-state' failed
Failed to clone 'packages/use-hashed-state'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/packages/utils'...
remote: Enumerating objects: 60, done.
remote: Counting objects: 100% (60/60), done.
remote: Compressing objects: 100% (46/46), done.
remote: Total 60 (delta 26), reused 47 (delta 13), pack-reused 0
Receiving objects: 100% (60/60), 11.24 KiB | 1.61 MiB/s, done.
Resolving deltas: 100% (26/26), done.
Cloning into '/Users/belarusrulez/Downloads/plasmo/templates/qtt'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/qtt.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/templates/qtt' failed
Failed to clone 'templates/qtt'. Retry scheduled
Cloning into '/Users/belarusrulez/Downloads/plasmo/examples'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/examples.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/examples' failed
Failed to clone 'examples' a second time, aborting```

@light-star
Copy link

If the src of the iframe in the tab is changed, the background will also be reload.
The content of the current page will become invalid.
The reason is also caused by autoreload.js built automatically.
npm run build no problem
image

@louisgv louisgv added the help wanted Extra attention is needed label Sep 8, 2022
@louisgv
Copy link
Contributor

louisgv commented Sep 8, 2022

also I tried the same with https:

...
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of '[email protected]:PlasmoHQ/examples.git' into submodule path '/Users/belarusrulez/Downloads/plasmo/examples' failed
Failed to clone 'examples' a second time, aborting```

BTW sorry for late reply - basically you will need to setup a SSH keypair and associate it with ur github account, as our git submodules uses ssh: https://docs.github.com/en/authentication/connecting-to-github-with-ssh

@louisgv
Copy link
Contributor

louisgv commented Sep 8, 2022

Regarding the losing state and invalidated context of this issue - do people really want their background sw to be static thorough development, or do they want the logic to change? - the idea of invalidating the context on development is so that folk can test their new background script logic, otherwise you would have to uninstall/re-install.

On production, it will be a single-spawned background sw which won't be reloaded at runtime. But note that it will get killed on idle.

While evaluating this issue, I'm not sure how to fix it completely. Basically during development there're times when the bgsw need to be invalidated for new instance and require hard reset of extension. What's the expected DX for background service worker?

@louisgv louisgv changed the title [BUG] background script reloads losing states [BUG] Background Service Worker loses states on hot reload Sep 8, 2022
@louisgv
Copy link
Contributor

louisgv commented Sep 8, 2022

Note that in MV3, it's now background service worker. As a worker, it's prone to die (after 5mins or so) - thus, extension developer must account for the nature of worker and make it short-lived and ensure there's an "entry" logic to the bgsw - e.g who's invoking these background services? - otherwise, do not treat them as permanent, long-living worker as in prod they're not afaik - chrome kills them after certain amount of time see #3

@louisgv louisgv closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2022
@belarusrulez
Copy link
Author

belarusrulez commented Sep 14, 2022

I have hotfix that works for me perfectly. I just delete auto-reload.js file from manifest and refresh the extension. (It doesn't affect auto-reload if code is changed and works as expected)
it would be ok if it would die in 5 minutes of inactivity but it refreshes immediately after message to popup or context was sent and code is doing nothing but waiting for response.

@louisgv louisgv reopened this Sep 22, 2022
@louisgv
Copy link
Contributor

louisgv commented Sep 22, 2022

@belarusrulez I see, this would likely go hand-in-hand with #215

@louisgv
Copy link
Contributor

louisgv commented Oct 8, 2022

resolved in #255

@louisgv louisgv closed this as completed Oct 8, 2022
@louisgv louisgv mentioned this issue Oct 8, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants