Worker detach event fires sporadically

RESOLVED FIXED

Status

Add-on SDK
General
P2
normal
RESOLVED FIXED
4 years ago
4 years ago

People

(Reporter: willmarquardt, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

4 years ago
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0 (Beta/Release)
Build ID: 20130910160258

Steps to reproduce:

Attached a content script to a page via tabs.activeTab.attach, then loaded a new page. Created an on detach listener.


Actual results:

Depending on the website, sometimes the content script was detached both on location change and on closing the tab, but at other times it was only detached when the tab was closed, not when the location changed.


Expected results:

The content script should have been detached when the location changed every time, as the documentation says should happen: "Workers generate a detach event when their associated document is closed: that is, when the tab is closed or the tab's location changes."
Jordan, since you've just done some looking at tab.attach can you see what is going on here too?
Flags: needinfo?(jsantell)
(Reporter)

Comment 2

4 years ago
To clarify, this is happening in any PageMod, whether using the standard PageMod.include or using tab.attach.
Looking into this; Will, I'm seeing some similar behaviour with tab-based worker usage -- do you have any examples were the behaviour is consistent? Are you only attaching one worker to a tab once?
Flags: needinfo?(jsantell) → needinfo?(willmarquardt)
(Reporter)

Comment 4

4 years ago
(In reply to Jordan Santell [:jsantell] [@jsantell] from comment #3)
> Looking into this; Will, I'm seeing some similar behaviour with tab-based
> worker usage -- do you have any examples were the behaviour is consistent?
> Are you only attaching one worker to a tab once?

Hi Jordan,

When I go to androidcentral.com, then load a worker to the tab once (use Tabs.activeTab.attach), then I go to google.com, the worker detaches. This is consistent and desired outcome.

On the other hand, if I go to a wikipedia article and load a worker into the tab, then go to google.com, the worker does not detach.

This is true for PageMod.pageMod({}) as well. Whether it works or not seems website dependent rather than module dependent.
Flags: needinfo?(willmarquardt)
(Reporter)

Comment 5

4 years ago
I decided to create my own fix by using the $(window).on('unload', function (e) {}) event listener, and found something quite interesting.

Simply creating the listener solves the problem. You don't need to do anything in the handler function.

Updated

4 years ago
Priority: -- → P2

Updated

4 years ago
Flags: needinfo?(dtownsend+bugmail)
If you navigate to a new page and then close the tab does the detach event fire? I believe that this is because the previous page is still held in the cache for quick retrieval (if you go back) and so that is why we don't detach. Adding an unload event listener makes the page ineligible for caching.
Flags: needinfo?(dtownsend+bugmail)
This sounds like we could use some documentation clarification then
(In reply to Dave Townsend (:Mossop) from comment #6)
> If you navigate to a new page and then close the tab does the detach event
> fire? I believe that this is because the previous page is still held in the
> cache for quick retrieval (if you go back) and so that is why we don't
> detach. Adding an unload event listener makes the page ineligible for
> caching.

This is correct, and is expected behavior.  I updated the quote referred to in comment 0 https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-mod
Status: UNCONFIRMED → RESOLVED
Last Resolved: 4 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.