Skip to content

Commit

Permalink
Longer extension service worker lifetimes. (GoogleChrome#5033)
Browse files Browse the repository at this point in the history
* Longer extension service worker lifetimes.

* Update site/en/blog/longer-esw-lifetimes/index.md

* Update site/en/blog/longer-esw-lifetimes/index.md

* Add blog ink to 'What\'s new'.

* Update site/en/blog/longer-esw-lifetimes/index.md

Co-authored-by: amysteamdev <[email protected]>

* Update site/en/blog/longer-esw-lifetimes/index.md

* fix: add missing translation for chrome110 tag

---------

Co-authored-by: amysteamdev <[email protected]>
Co-authored-by: Matthias Rohmer <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2023
1 parent aa71375 commit e26b936
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions site/_data/i18n/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ chrome109:
chrome110:
en: Chrome 110

chrome110:
en: Chrome 110

beta:
en: Beta

Expand Down
45 changes: 45 additions & 0 deletions site/en/blog/longer-esw-lifetimes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: 'layouts/blog-post.njk'
title: Longer extension service worker lifetimes
description: >
Extension service workers can now stay alive as long as they're receiving events. This increases the reliability of extension services workers, but has a pitfall you should avoid.
subhead: >
Extension service workers can now stay alive as long as they're receiving events. This increases the reliability of extension services workers, but has a pitfall you should avoid.
date: 2023-01-25
authors:
- joemedley
tags:
- extensions-news
- chrome110
hero: image/sQ51XsLqKMgSQMCZjIN0B7hlBO02/iND6W98T4Ve3pWoj4Xq5.jpeg
alt: >
A picture of an hourglass.
---

Starting in Chrome 110 (in beta as of February 7, 2023), extension service workers stay alive as long as they're receiving events. This corrects a timing problem in the previous implementation of extension service workers. It was possible for timeouts to occur when new events were in the event queue and for the timeouts to truncate asynchronous work. This improvement removes the hard five-minute maximum lifetime for extension service workers.

This article describes how these behaviors have changed.

## Background

Extension service workers mostly behave like web service workers, but in addition to [service worker events](https://developer.mozilla.org/docs/Web/API/ServiceWorkerGlobalScope#events), extension service workers can also listen to extension events. While normal service worker events extend the service worker's lifetime, before the release of 110 only a few extension platform events kept an extension service worker alive.

Normally, Chromium terminates a service worker after one of the following conditions is met:

* The service worker has not received an event for over thirty seconds and there are no outstanding long running tasks in progress. If a service worker received an event during that time, the idle timer was removed.
* A long running task has taken over five minutes to complete and no events have been received in the past thirty seconds.

New service worker events received before the idle timer or long running task timer expire would reset the timers and extend the service worker's lifetime.

Unfortunately, this behavior did not apply to extension events. Extension events could wake an extension service worker, and keep it alive until the event completes, but it could not extend the thirty second idle timer. This effectively meant that extension service workers could be terminated any time after the last extension event completed, even if the browser had just dispatched a new event to the extension.

## What's changed

As of Chrome 110, all events reset the lidle timer and the idle timeout will not occur if there are pending events. In other words, assuming there are no unexpected interruptions, extension service workers will now stay alive as long as they are actively processing events.

While this change removes the five minute limit on extension service worker lifetimes, you are still strongly encouraged to design your extensions to be event-based and guard against unexpected termination. Not only should you refrain from keeping your extensions alive indefinitely (which we consider an anti-pattern), you should test your extensions to ensure that they're not doing this unintentionally.

In a future release, we may change how specific events are processed to prevent keeping service workers alive indefinitely, but without shutting down long-running work. In the meantime, make sure your extensions yield when possible and prepare for termination by persisting extension states.

<p>Photo by <a href="https://unsplash.com/@pguerreiro?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Paula Guerreiro</a> on <a href="https://unsplash.com/photos/W2atfIRHDIk?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></p>

4 changes: 2 additions & 2 deletions site/en/docs/extensions/whatsnew/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: 'layouts/doc-post.njk'
title: What's new in Chrome extensions
description: 'Recent changes to the Chrome extensions platform, documentation, and policy'
date: 2021-02-25
updated: 2023-01-05
updated: 2023-01-27
tags:
- extensions-news

Expand All @@ -20,7 +20,7 @@ and related policy or other changes.

January 5, 2023

Previously, an extension service worker would frequently shut down at the five minute mark. We've changed this behavior to more closely resemble service worker lifetime's on the web. An extension service worker will be shut down after either thirty seconds of inactivity or if a single activity takes longer than 5 minutes to process. Watch this space for a blogpost with details about extension service worker lifetimes.
Previously, an extension service worker would frequently shut down at the five minute mark. We've changed this behavior to more closely resemble service worker lifetime's on the web. An extension service worker will be shut down after either thirty seconds of inactivity or if a single activity takes longer than 5 minutes to process. For more information, see [Longer extension service worker lifetimes](/blog/longer-esw-lifetimes).

### Post: Pausing Manifest V2 phase-out

Expand Down

0 comments on commit e26b936

Please sign in to comment.