You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Constrain OffscreenCanvas with a placeholder canvas element #10112
agenda+To be discussed at a triage meetingtopic: canvasCanvas, OffscreenCanvas, the 2D and bitmaprenderer contexts, and ImageBitmaptopic: event loopThe event loop: tasks, microtasks, task sources and updating the renderingtopic: serialize and transferStructured cloning, serialization and transfertopic: workers and workletsDedicated and shared workers, worklets, and their global scopes
In Event loop processing model about workers at https://html.spec.whatwg.org/#event-loop-processing-model:worker-event-loop-2, Dedicated Worker is the only type of workers that get special treatment for having its rendering updated. I believe Shared Worker and Service Worker should be also be included so that specs like WebGPU that have support for all workers can plumb into Update the rendering of that ${type} worker section. See gpuweb/gpuweb#4465 (comment)
The specs currently explicitely disable rAF in SharedWorker and in ServiceWorker by checking at the substep 1. of your link if the global object is a supportedDedicatedWorkerGlobalScope.
So not only is this reserved to windows and dedicated workers, but it even excludes the dedicated workers that have been created from a SharedWorker.
I guess there is a bug in that we do expose OffscreenCanvas in all Worker scopes though.
Actually it does make sense to expose it in other scopes, but I'm wondering why you do need to hook to update the rendering? Currently all it does is to update the content of a placeholder <canvas> element. But in a SharedWorker or a ServiceWorker you will never have such a placeholder <canvas>.
[edit]: I now realize there is nothing preventing the transfer of such an OffscreenCanvas gotten from transferControlToOffscreen() using serviceWorker.postMessage(data, [canvas]) or sharedWorker.postMessage()... However from my quick tests it seems only Chrome does support doing so with ServiceWorkers, while Firefox and Chrome support it for SharedWorkers. So indeed, if we do allow transferring such DOM-weak-linked OffscreenCanvas, we should probably expose rAF there. But maybe we don't want to allow these in such contexts? Is there an use case for it?
Maybe someone from @whatwg/canvas may want to chime in.
@beaufortfrancois did you look into blame to figure out why things are constrained the way they are? Service workers in particular do not seem like a place that should have to care about rendering at all. Shared workers is also fraught I think as they can be associated with multiple documents each of which can have their own refresh rate. What should the refresh rate of the shared worker be?
I think the change to WebGPU should be reverted and we should look into potentially constraining OffscreenCanvas more.
We've seen many reasonable use cases of OffscreenCanvas in ServiceWorkers, like folks trying to build resource icons or pre-loaded images on the fly ("hash default images", etc).
Regarding them being transferred, it does seem to me that the current set of constraints (Windows or Workers that are "children of a Document") seems reasonable to follow (i.e., you can only transfer if it's created from a Document, therefore those are the only ones that need some rendering update notion).
Apart from "easy to plug in on the spec", is there any reason why WebGPU content can't be updated without the worker having a "render update"?
WebGPU is useful without a canvas, and we definitely want it in ServiceWorker/SharedWorker regardless. And if OffscreenCanvas is going to stay in these workers (I think it should), it secondarily validates that choice.
That to say, I think we can fix-forward in the WebGPU spec, because the error is very small (and has an inline issue attached to it already). We're already mostly correct in that we only define an interaction with rAF if the OffscreenCanvas is a linked one.
If indeed transferControlToOffscreen isn't valid to send to these workers, then we just have to remove the two hooks and leave a note explaining why only dedicated workers get this behavior. Which I agree makes sense.
Constraining transferControlToOffscreen() and its output seems like a reasonable solution to me. Having a weak reference to an element across process boundaries seems very sketchy.
We should probably require OffscreenCanvas with a placeholder canvas element to stay within the same agent cluster, just like SharedArrayBuffer.
changed the title [-]Event loop processing model doesn't say anything about Service Worker and Shared Worker[/-][+]Constrain `OffscreenCanvas` with a placeholder `canvas` element[/+]on Feb 2, 2024
changed the title [-]Constrain `OffscreenCanvas` with a placeholder `canvas` element[/-][+]Constrain OffscreenCanvas with a placeholder canvas element[/+]on Feb 2, 2024
There was something like it, but it was not as well understood and had some issues ("unit of related similar-origin browsing contexts"). OffscreenCanvas was added in 2016 and SharedArrayBuffer / agent cluster integration was added in 2017 (and then adjusted in 2020 for Spectre purposes).
I don't have much background on Chromium's agent cluster limiting decisions, but if I were to guess it's purely historical: OffscreenCanvas transfer predates the IsLockedToAgentCluster mechanism and likely never got updated. Maybe @junov has more info.
FWIW Chromium handles this quite poorly: we assume that a placeholder-linked OffscreenCanvas stays in the same renderer process, and attempting cross-process drawing (like your wpt#63540) triggers a browser-side bad Mojo message that kills the receiving renderer process. Constraining should address that.
I was not aware of IsLockedToAgentCluster prior to reading this. In theory we shouldn't need to impose such a limitation on OffscreenCanvas, but the practical reality is that the current implementation in Chromium is limited to not transferring OffscreenCanvas across process boundaries and the limitation is not handled gracefully. If there's not enough demand or motivation to fix this properly, then we should probably at least make it fail gracefully, and perhaps codify the limitation in the spec.
@junov I was only suggesting imposing it when there's a placeholder canvas as "holding a reference" to an element cross-process seems wild and goes against the suggested scope of serialization and deserialization.
Right, but transferring an OffscreenCanvas that doesn't have a placeholder canvas is not useful: it just serializes basic metadata (width, height, locale, text direction). So we could keep the rule simple by locking all OffscreenCanvases to their agent clusters, and the platform would not lose any meaningful functionality. The risk is that it could theoretically break some websites, but I doubt it (could be validated via telemetry).
Side note: IMHO, holding a reference cross process is not that wild. In fact Chromium's implementation of OffscreenCanvas was meant to allow it by coordinating all communication between OffscreenCanvases and placeholder canvases using a PlaceholderCanvasID and a registry that maps these IDs to objects. It was meant as an IPC-friendly alternative to weak pointers. This was half implemented and abandoned due to lack of web developer interest. The main parts that are missing are the IPC services for sync'ing with out-of-process OffscreenCanvases.
It's definitely possible of course, just a bit unprecedented for postMessage(). I wouldn't mind restricting OffscreenCanvas in its entirety, if everyone will commit to shipping that. I can do the spec/test/WebKit work.
agenda+To be discussed at a triage meetingtopic: canvasCanvas, OffscreenCanvas, the 2D and bitmaprenderer contexts, and ImageBitmaptopic: event loopThe event loop: tasks, microtasks, task sources and updating the renderingtopic: serialize and transferStructured cloning, serialization and transfertopic: workers and workletsDedicated and shared workers, worklets, and their global scopes
Activity
Kaiido commentedon Jan 31, 2024
The specs currently explicitely disable rAF in SharedWorker and in ServiceWorker by checking at the substep 1. of your link if the global object is a supported
DedicatedWorkerGlobalScope.And to be supported means
So not only is this reserved to windows and dedicated workers, but it even excludes the dedicated workers that have been created from a SharedWorker.
I guess there is a bug in that we do exposeOffscreenCanvasin allWorkerscopes though.Actually it does make sense to expose it in other scopes, but I'm wondering why you do need to hook to update the rendering? Currently all it does is to update the content of a placeholder
<canvas>element. But in a SharedWorker or a ServiceWorker you will never have such a placeholder<canvas>.[edit]: I now realize there is nothing preventing the transfer of such an
OffscreenCanvasgotten fromtransferControlToOffscreen()usingserviceWorker.postMessage(data, [canvas])orsharedWorker.postMessage()... However from my quick tests it seems only Chrome does support doing so with ServiceWorkers, while Firefox and Chrome support it for SharedWorkers. So indeed, if we do allow transferring such DOM-weak-linked OffscreenCanvas, we should probably expose rAF there. But maybe we don't want to allow these in such contexts? Is there an use case for it?Maybe someone from @whatwg/canvas may want to chime in.
annevk commentedon Feb 1, 2024
@beaufortfrancois did you look into blame to figure out why things are constrained the way they are? Service workers in particular do not seem like a place that should have to care about rendering at all. Shared workers is also fraught I think as they can be associated with multiple documents each of which can have their own refresh rate. What should the refresh rate of the shared worker be?
I think the change to WebGPU should be reverted and we should look into potentially constraining
OffscreenCanvasmore.fserb commentedon Feb 1, 2024
What @Kaiido concluded seems right to me.
We've seen many reasonable use cases of
OffscreenCanvasin ServiceWorkers, like folks trying to build resource icons or pre-loaded images on the fly ("hash default images", etc).Regarding them being transferred, it does seem to me that the current set of constraints (Windows or Workers that are "children of a Document") seems reasonable to follow (i.e., you can only transfer if it's created from a Document, therefore those are the only ones that need some rendering update notion).
Apart from "easy to plug in on the spec", is there any reason why WebGPU content can't be updated without the worker having a "render update"?
kainino0x commentedon Feb 1, 2024
WebGPU is useful without a canvas, and we definitely want it in ServiceWorker/SharedWorker regardless. And if OffscreenCanvas is going to stay in these workers (I think it should), it secondarily validates that choice.
That to say, I think we can fix-forward in the WebGPU spec, because the error is very small (and has an inline issue attached to it already). We're already mostly correct in that we only define an interaction with rAF if the OffscreenCanvas is a linked one.
If indeed transferControlToOffscreen isn't valid to send to these workers, then we just have to remove the two hooks and leave a note explaining why only dedicated workers get this behavior. Which I agree makes sense.
annevk commentedon Feb 2, 2024
Constraining
transferControlToOffscreen()and its output seems like a reasonable solution to me. Having a weak reference to an element across process boundaries seems very sketchy.We should probably require
OffscreenCanvaswith a placeholdercanvaselement to stay within the same agent cluster, just likeSharedArrayBuffer.[-]Event loop processing model doesn't say anything about Service Worker and Shared Worker[/-][+]Constrain `OffscreenCanvas` with a placeholder `canvas` element[/+][-]Constrain `OffscreenCanvas` with a placeholder `canvas` element[/-][+]Constrain OffscreenCanvas with a placeholder canvas element[/+]fserb commentedon Feb 2, 2024
This makes sense.
Just out of curiosity, did we even have the notion of agent clusters back when we added
OffscreenCanvas?annevk commentedon Feb 3, 2024
There was something like it, but it was not as well understood and had some issues ("unit of related similar-origin browsing contexts").
OffscreenCanvaswas added in 2016 andSharedArrayBuffer/ agent cluster integration was added in 2017 (and then adjusted in 2020 for Spectre purposes).kainino0x commentedon Feb 5, 2024
I did not realize that
SharedArrayBufferhad such a constraint. Matching that definitely makes sense!annevk commentedon Oct 12, 2026
I created two draft PRs with tests here, one assuming we do want a same-agent-cluster limitation on placeholder canvas and one where we don't:
Based on this I think we can add the limitation.
It also seems that Chrome already has an agent cluster limitation for various features such as
VideoFrame. Why wasn't it added here? Does anyone know?fmalita commentedon Oct 15, 2026
I don't have much background on Chromium's agent cluster limiting decisions, but if I were to guess it's purely historical:
OffscreenCanvastransfer predates theIsLockedToAgentClustermechanism and likely never got updated. Maybe @junov has more info.FWIW Chromium handles this quite poorly: we assume that a placeholder-linked
OffscreenCanvasstays in the same renderer process, and attempting cross-process drawing (like your wpt#63540) triggers a browser-side bad Mojo message that kills the receiving renderer process. Constraining should address that.junov commentedon Oct 16, 2026
I was not aware of IsLockedToAgentCluster prior to reading this. In theory we shouldn't need to impose such a limitation on OffscreenCanvas, but the practical reality is that the current implementation in Chromium is limited to not transferring OffscreenCanvas across process boundaries and the limitation is not handled gracefully. If there's not enough demand or motivation to fix this properly, then we should probably at least make it fail gracefully, and perhaps codify the limitation in the spec.
annevk commentedon Oct 16, 2026
@junov I was only suggesting imposing it when there's a placeholder canvas as "holding a reference" to an element cross-process seems wild and goes against the suggested scope of serialization and deserialization.
junov commentedon Oct 17, 2026
Right, but transferring an OffscreenCanvas that doesn't have a placeholder canvas is not useful: it just serializes basic metadata (width, height, locale, text direction). So we could keep the rule simple by locking all OffscreenCanvases to their agent clusters, and the platform would not lose any meaningful functionality. The risk is that it could theoretically break some websites, but I doubt it (could be validated via telemetry).
Side note: IMHO, holding a reference cross process is not that wild. In fact Chromium's implementation of OffscreenCanvas was meant to allow it by coordinating all communication between OffscreenCanvases and placeholder canvases using a
PlaceholderCanvasIDand a registry that maps these IDs to objects. It was meant as an IPC-friendly alternative to weak pointers. This was half implemented and abandoned due to lack of web developer interest. The main parts that are missing are the IPC services for sync'ing with out-of-process OffscreenCanvases.annevk commentedon Oct 17, 2026
It's definitely possible of course, just a bit unprecedented for
postMessage(). I wouldn't mind restrictingOffscreenCanvasin its entirety, if everyone will commit to shipping that. I can do the spec/test/WebKit work.annevk commentedon Nov 20, 2026
agenda+ was removed here, but it's not clear to me we have agreement on my post from October 17?