← All reports

StreamConnectionWorkQueue drains work dispatched during stop; WebGPU Instance teardown waits for Metal

Component: WebKit GPU Process | b3c6189

Source/WebKit/Platform/IPC/StreamConnectionWorkQueue.cpp

- if (m_shouldQuit)
+ if (m_isStopped)
return;
...
- processStreams();
WTF::Function<void()> cleanup = nullptr;
- {
+ for (;;) {
+ processStreams();
Locker locker { m_lock };
- cleanup = WTF::move(m_cleanupFunction);
+ if (m_functions.isEmpty()) {
+ m_isStopped = true;
+ cleanup = WTF::move(m_cleanupFunction);
+ break;
+ }
}

Source/WebGPU/WebGPU/Instance.mm

+void Instance::destroy()
+{
+ for (; m_startedAsyncCompileCount; --m_startedAsyncCompileCount)
+ m_asyncCompileCompletions->wait();
+ waitForCommandBufferCompletions();
+}

WebGPU in WebKit runs in the GPU process. The web process or a worker sends IPC over a stream connection, and a StreamConnectionWorkQueue thread dispatches the messages to RemoteGPU and its objects (devices, queues, buffers, pipelines), which are held in an ObjectHeap. Metal completion handlers run on Metal-owned threads and may hold references to the WebGPU Instance. Completion handlers such as onSubmittedWorkDone() must always be called, or the IPC reply is lost.

The work queue now keeps running all work dispatched while it is stopping, including work dispatched during the drain itself, until the queue is empty. Only then does it run the cleanup function, and anything dispatched after that point is dropped. RemoteGPU::workQueueUninitialize() now destroys every live device, tracked weakly by the ObjectHeap, and clears the heaps. It then exchanges out the backing instance and calls the new Instance::destroy(), which waits on a shared semaphore once per started async pipeline compile and then waits for in-flight Metal command buffers. The instance reference is therefore dropped on the work queue thread. The cleanup lambda clears the object heaps a second time to catch objects created during the drain.

Terminating a WebGPU worker from web content could crash the GPU process via dropped completion handlers and an ~Instance racing the device's command encoder map on a Metal thread. The commit also restores the Metal-completion wait that 320662@main added and 323962@main bypassed.

The trigger here, worker termination, is fully content-controlled, and the surrounding code has now produced a cross-thread destructor race, dropped completion handlers, and a regression. That makes neighboring stop/teardown paths worth hunting.

Running work while stopping changes the invariants for every stream message handler. Any handler that assumes the connection or heap is still live, or that work dispatched from other threads or from the cleanup function is dropped, deserves a re-read, along with the transition between "stopping" and "cleanup ran". Device-owned objects kept alive by in-flight work are a use-after-destroy candidate when drain-time work touches them after the weak device list was destroyed. Work that creates objects during the drain, such as async pipeline creation, runs while the heaps are being torn down.

The async-compile accounting has its own risks. m_startedAsyncCompileCount is a plain uint64_t with no lock or atomic. Check which threads call didStartAsyncCompile() versus destroy(), look for compiles started concurrently with or after destroy(), and look for mismatches between signal count and wait count.

Elsewhere, look for remaining places where the instance's last reference can still be released on a Metal thread, such as other completion handlers holding the instance or device. For fuzzing, terminate workers at varied points during heavy createShaderModule, writeBuffer and onSubmittedWorkDone traffic, and vary pipeline creation types and timing.