Repository: electron
Issue: [Bug]: GPU process crashes intermittently when using webContents.beginFrameSubscription() with a resized browser window
Labels: platform/windows,platform/linux,bug :beetle:,status/confirmed,has-repro-gist,23-x-y,24-x-y

### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

23.1.1

### What operating system are you using?

Ubuntu

### Operating System Version

22.04.2 LTS

### What arch are you using?

x64

### Last Known Working Electron version

_No response_

### Expected Behavior

`beginFrameSubscription()` is expected to work in a stable manner irrespective of any preceding or following calls to `setSize()` on the containing window.

### Actual Behavior

When running the following testcase I observe intermittent GPU process crashes both on Wayland and X11: `[1536720:0225/191522.620766:ERROR:gpu_process_host.cc(953)] GPU process exited unexpectedly: exit_code=11`
```JavaScript
const { app, BrowserWindow, crashReporter } = require('electron')
const path = require('path')

app.disableHardwareAcceleration();
app.commandLine.appendSwitch("disable-gpu");

console.log(`Crash Dumps path: ${app.getPath('crashDumps')}`)
crashReporter.start({ submitURL: '', uploadToServer: false })

async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

app.whenReady().then(async () => {

  // Create a new window
  let win = new BrowserWindow({
    webPreferences: {
      offscreen: true
    }
  });

  // Load a URL in the window
  win.loadURL('chrome://gpu');

  const startTime = Date.now()

  function calculateNewSize() {
    const elapsedTime = Date.now() - startTime

    // Calculate the new size of the window using the `Math.sin` function
    const width = 200 + 800 * (Math.sin(elapsedTime / 100) + 1) / 2
    const height = 100 + 400 * (Math.sin(elapsedTime / 100) + 1) / 2

    return { width: Math.round(width), height: Math.round(height) }
  }

  // Set the interval to resize the window every 100 milliseconds (.1 second)
  setInterval(() => {
    try {
      const { width, height } = calculateNewSize()
      win.setSize(width, height)
    } catch (e) {
      console.log(`Error: ${e}`);
    }
  }, 100)

  // Get the web contents of the window
  let contents = win.webContents;

  for (let i = 0; i < 1000; i++) {

    // Subscribe to frame updates with a callback function
    contents.beginFrameSubscription(function (image, dirtyRect) {
      console.log(`Received a frame with size = ${JSON.stringify(image.getSize())}`)
      // Unsubscribe from frame updates
      contents.endFrameSubscription();
    });

    // Sleep for 100 milliseconds
    await sleep(100);
    console.log(`Loop ${i}`);

  }

  app.quit();

})
```

After that frame subscription stops working and Electron stops rendering anything.
```
Loop 133
Received a frame with size = {"width":583,"height":292}
Loop 134
Received a frame with size = {"width":255,"height":127}
Loop 135
Received a frame with size = {"width":247,"height":124}
Loop 136
Received a frame with size = {"width":568,"height":284}
Loop 137
Received a frame with size = {"width":918,"height":459}
Loop 138
Received a frame with size = {"width":943,"height":472}
Loop 139
Received a frame with size = {"width":613,"height":306}
[1536720:0225/191522.620766:ERROR:gpu_process_host.cc(953)] GPU process exited unexpectedly: exit_code=11
Loop 140
Loop 141
Loop 142
Loop 143
Loop 144
```


### Testcase Gist URL

https://gist.github.com/briskycat/7017cfe319172f8eda837f1379fd1129

### Additional Information

I believe this Linux issue is the same as the one reported in https://github.com/electron/electron/issues/31549 for Windows, which hasn't been fixed ever since (though the bug has been closed).