Repository: cashapp/paparazzi
Issue: ConcurrentModificationException when running a lot of tests at once

**Description**
We have a lot of modules, so we run our tests in parallel all at once. We have about 10 modules using paparazzi. Occasionally, the tests for Paparazzi will fail with:

```
java.util.ConcurrentModificationException
	at java.base/java.util.WeakHashMap.forEach(WeakHashMap.java:1031)
	at com.android.layoutlib.bridge.util.HandlerMessageQueue.extractFirst(HandlerMessageQueue.java:73)
	at android.os.Handler_Delegate.executeCallbacks(Handler_Delegate.java:95)
	at app.cash.paparazzi.Paparazzi.withTime(Paparazzi.kt:330)
	at app.cash.paparazzi.Paparazzi.takeSnapshots(Paparazzi.kt:295)
	at app.cash.paparazzi.Paparazzi.snapshot(Paparazzi.kt:211)
	at app.cash.paparazzi.Paparazzi.snapshot(Paparazzi.kt:204)
	at app.cash.paparazzi.Paparazzi.snapshot$default(Paparazzi.kt:195)
```

Seems as though this line:
```kotlin
Handler_Delegate.executeCallbacks()
```
Cannot handle multiple calls at once. My best suggestion is to synchronize this block so that it allows the concurrency to succeed.

**Steps to Reproduce**
Can't share my work repo. I imagine a project with a few modules, and running `testDebugUnitTest` on the top level project.

**Expected behavior**
Should successfully run.

**Additional information:**
- Paparazzi Version: 1.0.0
- OS: macOS Monterey 12.6
- Compile SDK: 32
- Gradle Version: 7.5
- Android Gradle Plugin Version: 7.2.1

JDK:
```
openjdk 11.0.16.1 2022-08-12 LTS
OpenJDK Runtime Environment Corretto-11.0.16.9.1 (build 11.0.16.1+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.16.9.1 (build 11.0.16.1+9-LTS, mixed mode)
```



Changed files:
paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt

diff --git a/paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt b/paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
index 90ebd8a5..01fc9c72 100644
--- a/paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
+++ b/paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
@@ -328,9 +328,14 @@ class Paparazzi @JvmOverloads constructor(
     try {
       areCallbacksRunningField.setBoolean(choreographer, true)
 
-      // https://android.googlesource.com/platform/frameworks/layoutlib/+/d58aa4703369e109b24419548f38b422d5a44738/bridge/src/com/android/layoutlib/bridge/BridgeRenderSession.java#171
-      // BridgeRenderSession.executeCallbacks aggressively tears down the main Looper and BridgeContext, so we call the static delegates ourselves.
-      Handler_Delegate.executeCallbacks()
+      // Avoid ConcurrentModificationException in
+      // RenderAction.currentContext.sessionInteractiveData.handlerMessageQueue.runnablesMap which is a WeakHashMap
+      // https://android.googlesource.com/platform/tools/adt/idea/+/c331c9b2f4334748c55c29adec3ad1cd67e45df2/designer/src/com/android/tools/idea/uibuilder/scene/LayoutlibSceneManager.java#1558
+      synchronized(this) {
+        // https://android.googlesource.com/platform/frameworks/layoutlib/+/d58aa4703369e109b24419548f38b422d5a44738/bridge/src/com/android/layoutlib/bridge/BridgeRenderSession.java#171
+        // BridgeRenderSession.executeCallbacks aggressively tears down the main Looper and BridgeContext, so we call the static delegates ourselves.
+        Handler_Delegate.executeCallbacks()
+      }
       val currentTimeMs = SystemClock_Delegate.uptimeMillis()
       val choreographerCallbacks =
         RenderAction.getCurrentContext().sessionInteractiveData.choreographerCallbacks