Kotlin: fix intermittent crashing in webGPU.close()

I fixed a bug where the isClosing flag wasn't set (this can stop
crashing in the event polling loop).

Currently if device.close() (which calls wgpuDeviceRelease) is called
this is kills the thread. I don't know why.

On the other hand, if I don't call wgpuDeviceRelease I am able to bring
down the surface, the instance and the adapter, which is an
improvement.

Bug: 428866400
Test: Run the demo at
https: //dawn-android-samples.googlesource.com/dawn-demo/+/refs/heads/main
Change-Id: I58cbbd5fbb090699c618cea49eb2082cdfca24a9
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/257155
Commit-Queue: Jim Blackler <jimblackler@google.com>
Reviewed-by: Alfie Baxter <alfiebaxter@google.com>
Reviewed-by: Loko Kung <lokokung@google.com>
diff --git a/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt b/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
index f067239..f3573f3 100644
--- a/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
+++ b/tools/android/webgpu/src/main/java/androidx/webgpu/helper/WebGpu.kt
@@ -64,7 +64,7 @@
     val adapter = requestAdapter(instance, requestAdapterOptions)
     val device = requestDevice(adapter, requiredFeatures)
 
-    val isClosing = false
+    var isClosing = false
     // Long-running event poller for async methods. Can be removed when
     // https://issues.chromium.org/issues/323983633 is fixed.
     val handler = Handler(Looper.getMainLooper())
@@ -86,10 +86,11 @@
         override val device = device
 
         override fun close() {
-            device.close()
-            device.destroy();
-//          webgpuSurface.close()  // TODO(b/428866400): Remove when fixed
-//          instance.close()  // TODO(b/428866400): Remove when fixed
+            isClosing = true
+            //device.close() // TODO(b/428866400): Uncomment when fixed.
+            webgpuSurface?.close()
+            instance.close()
+            adapter.close()
         }
     }
 }