store notification metadata in individual keys to avoid races #1002
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Whenever notifier generates a bunch of notifications all at once, the background page has to create a bunch of metadata objects for them all at the same time. Unfortunately since all the metadata objects are held under the same key, and storage calls are asynchronous, we don't have a great way to ensure that the object containing all the keys hasn't had anything else added to it between when we read it and when we write it back with a new key. This results in metadata objects getting overwritten and not existing for some portion of the notifications that are created. If a metadata object doesn't exist for a notification, we assume it's already been cleared, so this results in a bunch of notifications from the group becoming un-dismissable until the page is reloaded.
To fix this, each notification now gets its own top-level key in extension storage for its own metadata. This avoids unsafe modification of a shared storage area. I also moved this data into the
session
storage area, since we don't care about persisting notification state across browser restarts and having the browser automatically clean up after us in case we miss something is kinda nice.