Intent to Implement: Web Locks API

268 views
Skip to first unread message

Joshua Bell

unread,
Nov 10, 2017, 6:20:26 PM11/10/17
to blink-dev

Contact emails

jsb...@chromium.org (but darin@ should get credit too)


Explainer

https://github.com/inexorabletash/web-locks


Design doc/Spec

API shape still in discussion but:
https://github.com/inexorabletash/web-locks/blob/master/interface.webidl

TAG review requested

Summary

A new web platform primitive API that allows agents (tabs, workers) within an origin to coordinate use of abstract shared resources. This is equivalent to a "mutex" as exposed by many operating systems, with additional optional lock types (exclusive and shared) for common scenarios.


navigator.locks.acquire("my_resource", async lock => {

// can assert that no other tab/worker in this origin

// is using "my_resource" until this async function completes

});


The locks/resources have no meaning themselves outside of the API; it's up to the application to use them to bound the use of other APIs, e.g. to make sure only one tab is fetching a data from the network and writing it to the database.


Motivation

Existing web properties that manage cross-tab behavior use a variety of approximations to achieve this: polling against Indexed DB, tracking clients with Service Workers and BroadcastChannel, or requiring Shared Workers. All of these have significant gaps and complexity.


Risks

Interoperability and Compatibility

New feature; risk is lack of adoption. It does not introduce complexity for other parts of the code base.


Edge/Firefox/Safari: No signals

Web developers: Interest from Google Docs and Gmail.


Ergonomics

Likely to be used with various agent types (e.g. Windows, Service Workers) and to coordinate use of other APIs (e.g. which tab gets to fetch, which tab gets to write to the database, etc). The API is entirely async and can be used from any context.


Feature depends on AbortController/AbortSignal for some functionality. Since that is NYI, we can stage the implementation.


Like other APIs that hold stateful resources and/or resources visible across tabs (BroadcastChannel, Indexed DB, Web Sockets, ...) the interaction with the Web Lifecycle API will require careful specification. (e.g. can we suspend a tab holding a lock?)


Activation

The feature would be usable as-is. Polyfills would be flawed due to limitations in the existing APIs - it would require poling, and/or a service worker, and/or shared workers (but have gaps); developers would likely need to pick the least-worst approach to work around this gap (as they do today); there would not be a simple drop-in.


Debuggability

Based on experience with debugging IndexedDB (etc), the API itself is designed to include debugging support so that web applications can introspect their state (what locks are held? what locks are pending?) and reset that state (potentially controversial, added but based on feedback from likely consumers of the API)


Exposing an inspector for held locks in DevTools might be valuable too.


Will this feature be supported on all six Blink platforms (Windows, Mac, Linux, Chrome OS, Android, and Android WebView)?

Yes.


Is this feature fully tested by web-platform-tests?

In-progress CL has testharness.js-based test coverage. These would land as .tentative tests in WPT during the implementation phase.


Link to entry on the feature dashboard

Will file if/when stuff lands.


Requesting approval to ship?

No. Plan to have the feature available behind a flag until the API is stable, then proceed to "intent to experiment".



Joe Medley

unread,
Nov 13, 2017, 11:00:38 AM11/13/17
to Joshua Bell, blink-dev
Joshua,

Do you at least have a tracking bug I can star?

Joe

Joe Medley | Technical Writer, Chrome DevRel | jme...@google.com | 816-678-7195
If an API's not documented it doesn't exist.

--
You received this message because you are subscribed to the Google Groups "blink-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+unsubscribe@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/CAD649j5Xt3m5hWd3jxqAjHH2XFcud%2BSrJeF-5avKs2FjHPOV_w%40mail.gmail.com.

Joshua Bell

unread,
Nov 13, 2017, 12:26:00 PM11/13/17
to Joe Medley, blink-dev

Joshua Bell

unread,
Dec 7, 2017, 7:27:35 PM12/7/17
to Joe Medley, blink-dev
Chrome Status Entry: https://www.chromestatus.com/feature/5712361335816192

(Also, apologies for the really poor commit comment for the initial CL which just landed.)

st...@dekorte.com

unread,
Jun 22, 2018, 5:53:22 PM6/22/18
to blink-dev
AFAICS, this locking feature is desperately needed to ensure consistency when using IndexedDB from multiple tabs. A synchronous API for IndexedDB would also solve this problem and others.
Reply all
Reply to author
Forward
0 new messages