Why do I get the error "InvalidStateError: Context has stopped" when calling write() on FileSystemFileHandle?

32 Views Asked by At

In a Web Worker I have code to write to the browser FileSystem, and it usually works. However sometimes I see the write() call throws the error "InvalidStateError: Context has stopped" and I cannot find why.

// This is running in a worker thread
const dir = await navigator.storage.getDirectory();
const handle = await dir.getFileHandle('file.txt', {create: true});
const syncHandle = await handle.createSyncAccessHandle();

syncHandle.truncate(0);
syncHandle.flush();

const encoder = new TextEncoder();
const encodedMessage = encoder.encode("some text");
syncHandle.write(encodedMessage, { at: 0 });

0

There are 0 best solutions below