[WGPUFuture] Make the wire return a bool when tracking events.

- We should be skipping command serialization if the client is already
  disconnected.

Bug: dawn:2052
Change-Id: I7db09675ffd11329165994712ee58ca9aa5880b3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/152620
Commit-Queue: Loko Kung <lokokung@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/wire/client/EventManager.cpp b/src/dawn/wire/client/EventManager.cpp
index fa80b6d..f567078 100644
--- a/src/dawn/wire/client/EventManager.cpp
+++ b/src/dawn/wire/client/EventManager.cpp
@@ -26,12 +26,13 @@
 
 EventManager::EventManager(Client* client) : mClient(client) {}
 
-FutureID EventManager::TrackEvent(WGPUCallbackMode mode, EventCallback&& callback) {
+std::pair<FutureID, bool> EventManager::TrackEvent(WGPUCallbackMode mode,
+                                                   EventCallback&& callback) {
     FutureID futureID = mNextFutureID++;
 
     if (mClient->IsDisconnected()) {
         callback(EventCompletionType::Shutdown);
-        return futureID;
+        return {futureID, false};
     }
 
     mTrackedEvents.Use([&](auto trackedEvents) {
@@ -40,7 +41,7 @@
         DAWN_ASSERT(inserted);
     });
 
-    return futureID;
+    return {futureID, true};
 }
 
 void EventManager::ShutDown() {
diff --git a/src/dawn/wire/client/EventManager.h b/src/dawn/wire/client/EventManager.h
index e2aae83..2260b73 100644
--- a/src/dawn/wire/client/EventManager.h
+++ b/src/dawn/wire/client/EventManager.h
@@ -18,6 +18,7 @@
 #include <cstddef>
 #include <functional>
 #include <unordered_map>
+#include <utility>
 
 #include "dawn/common/FutureUtils.h"
 #include "dawn/common/MutexProtected.h"
@@ -43,7 +44,9 @@
     explicit EventManager(Client*);
     ~EventManager() = default;
 
-    FutureID TrackEvent(WGPUCallbackMode mode, EventCallback&& callback);
+    // Returns a pair of the FutureID and a bool that is true iff the event was successfuly tracked,
+    // false otherwise. Events may not be tracked if the client is already disconnected.
+    std::pair<FutureID, bool> TrackEvent(WGPUCallbackMode mode, EventCallback&& callback);
     void ShutDown();
     void SetFutureReady(FutureID futureID);
     void ProcessPollEvents();
diff --git a/src/dawn/wire/client/Queue.cpp b/src/dawn/wire/client/Queue.cpp
index 26f662b..ad7c7e5 100644
--- a/src/dawn/wire/client/Queue.cpp
+++ b/src/dawn/wire/client/Queue.cpp
@@ -58,13 +58,16 @@
     DAWN_ASSERT(callbackInfo.nextInChain == nullptr);
 
     Client* client = GetClient();
-    FutureID futureIDInternal = client->GetEventManager()->TrackEvent(
+    auto [futureIDInternal, tracked] = client->GetEventManager()->TrackEvent(
         callbackInfo.mode, [=](EventCompletionType completionType) {
             WGPUQueueWorkDoneStatus status = completionType == EventCompletionType::Shutdown
                                                  ? WGPUQueueWorkDoneStatus_Unknown
                                                  : WGPUQueueWorkDoneStatus_Success;
             callbackInfo.callback(status, callbackInfo.userdata);
         });
+    if (!tracked) {
+        return {futureIDInternal};
+    }
 
     struct Lambda {
         Client* client;